flutter刷新一个状态,来刷新控件的状态

1,flutter定义一个状态值,应该放在哪里合适?

1.1, flutter widget的结构

Dart 复制代码
//无状态的,固定widget
class CustomPositionedWidget extends StatelessWidget {

 const CustomPositionedWidget({
    super.key,
  });

  @override
  Widget build(BuildContext context) {

    return Container();
  }
}





//有状态的widget
class CustomPositionedWidget extends StatefulWidget {
 //状态属性写在这里???

  const CustomPositionedWidget({
    super.key,
  });

  @override
  _CustomPositionedWidgetState createState() => _CustomPositionedWidgetState();
}

class _CustomPositionedWidgetState extends State<CustomPositionedWidget> {
  //状态属性写在这里???
  

  @override
  void initState() {
    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

状态属性写在哪里合适?

Dart 复制代码
enum AnswerState { unanswered, correct, incorrect }

class CustomPositionedWidget extends StatefulWidget {
  //这里只是传递了初始值
  final bool initialNeedShowState;
  final AnswerState initialAnswerState;

  const CustomPositionedWidget({
    required this.initialNeedShowState,
    required this.initialAnswerState,
  });

  @override
  _CustomPositionedWidgetState createState() => _CustomPositionedWidgetState();
}

class _CustomPositionedWidgetState extends State<CustomPositionedWidget> {
  //还是写在这里好,其实在这里也设置了初始值(默认值)
  bool _needShowState = false;
  AnswerState _answerState = AnswerState.unanswered;

  @override
  void initState() {
    super.initState();
    //设置 构造函数传递过来的初始值
    _needShowState = widget.initialNeedShowState;
    _answerState = widget.initialAnswerState;
  }

  void updateState(bool needShowState, AnswerState answerState) {
    setState(() {
      //如果这两个值有改变,会重新绘制widget
      _needShowState = needShowState;
      _answerState = answerState;
    });
  }

  String getStr() {
    if (_answerState == AnswerState.correct) {
      return 'correct_image';
    } else if (_answerState == AnswerState.incorrect) {
      return 'incorrect_image';
    } else {
      return 'default_image';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
      right: 22.w,
      top: 0,
      bottom: 0,
      child: Align(
        alignment: Alignment.center,
        child: _needShowState && (_answerState != AnswerState.unanswered)
            ? Image.asset(
                width: 36.w,
                height: 36.h,
                'assets/images/${getStr()}.png',
                fit: BoxFit.cover,
              )
            : Container(),
      ),
    );
  }
}
相关推荐
uhakadotcom23 分钟前
Astro 框架:快速构建内容驱动型网站的利器
前端·javascript·面试
uhakadotcom27 分钟前
了解Nest.js和Next.js:如何选择合适的框架
前端·javascript·面试
uhakadotcom28 分钟前
React与Next.js:基础知识及应用场景
前端·面试·github
uhakadotcom37 分钟前
Remix 框架:性能与易用性的完美结合
前端·javascript·面试
uhakadotcom1 小时前
Node.js 包管理器:npm vs pnpm
前端·javascript·面试
LaoZhangAI2 小时前
2025最全GPT-4o图像生成API指南:官方接口配置+15个实用提示词【保姆级教程】
前端
ONE_Gua2 小时前
chromium魔改——CDP(Chrome DevTools Protocol)检测01
前端·后端·爬虫
LaoZhangAI2 小时前
2025最全Cherry Studio使用MCP指南:8种强大工具配置方法与实战案例
前端
咖啡教室2 小时前
前端开发日常工作每日记录笔记(2019至2024合集)
前端·javascript
溪饱鱼2 小时前
Nuxt3能上生产吗?
前端