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(),
      ),
    );
  }
}
相关推荐
福兮说1 小时前
前端下载大文件,点了按钮半天没反应:fetch + blob 的三个问题和三种替代写法
前端·javascript·性能优化·文件下载
GreenTea1 小时前
我把 Agent 的 while 循环拆掉了:一种你可能没想到的 Agent 架构
前端·后端·架构
科技观察哨2 小时前
六足平台选型与纳米定位系统集成:HEB-640六自由度位移台在半导体光刻对准中的参数边界与国产替代评估
前端·人工智能
niucloud-admin2 小时前
JAVA V6 多商户商城 开发文档——web前端
前端
颜进强2 小时前
16 · NestJS LifecycleEvents 生命周期事件:五个钩子、三条边界,和 rag-server 的优雅停机
前端·后端·ai编程
今年下半年4 小时前
【VUE】整合腾讯地图、自定义区域边界、村委名称及资产统计(放大显示资产点位)
前端·javascript·vue.js
郑州光合科技余经理4 小时前
同城电商系统:库存变更怎么同步到订单
java·开发语言·前端·后端·uni-app·php·ai编程
前端snow6 小时前
ai agent --- DeepAgents 中间件
前端
data analyse 4566 小时前
报表能自定义的国内统计工具有哪些
前端·数据分析