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(),
      ),
    );
  }
}
相关推荐
世界哪有真情4 小时前
拿人类意识卡 AI?等于用 bug 验收正式产品
前端·人工智能·后端
Listen·Rain5 小时前
Vue3:setup详解
前端·javascript·vue.js
Patrick_Wilson6 小时前
修好 bug 只是开始:一次由监控需求反向重构日志结构的复盘
前端·监控·数据可视化
kyriewen6 小时前
面试官让我优化一个卡死的表格,我打开 Claude 五秒重写,他放下咖啡问我要不要来当组长
前端·javascript·面试
小粉粉hhh7 小时前
React(二)——dom、组件间通信、useEffect
前端·javascript·react.js
Csvn7 小时前
🤖 AI 生成前端代码的 5 个典型翻车现场及修复指南
前端
IT_陈寒8 小时前
SpringBoot自动装配坑了我一天,原来问题出在这
前端·人工智能·后端
索西引擎8 小时前
【React】key 属性:协调算法中的元素标识机制与最佳实践
前端·javascript·react.js
只会cv的前端攻城狮9 小时前
动态组件架构设计:从配置到事件驱动全链路解析
前端·vue.js
飞天狗9 小时前
Next.js 16 App Router 实战:服务端组件与流式渲染
前端·next.js