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(),
      ),
    );
  }
}
相关推荐
一起养小猫几秒前
Flutter for OpenHarmony 实战:打造功能完整的记账助手应用
android·前端·flutter·游戏·harmonyos
hbstream海之滨视频网络技术3 分钟前
Google正式上线Gemini In Chrome,国内环境怎样开启。
前端·chrome
一起养小猫3 分钟前
Flutter for OpenHarmony 实战:打造功能完整的云笔记应用
网络·笔记·spring·flutter·json·harmonyos
Lisson 34 分钟前
VF01修改实际开票数量增强
java·服务器·前端·abap
微祎_9 分钟前
Flutter for OpenHarmony:构建一个 Flutter 旋转迷宫游戏,深入解析网格建模、路径连通性检测与交互式解谜设计
javascript·flutter·游戏
一起养小猫10 分钟前
Flutter for OpenHarmony 实战:笔记应用文件操作与数据管理详解
flutter·harmonyos
红色的小鳄鱼14 分钟前
Vue 教程 自定义指令 + 生命周期全解析
开发语言·前端·javascript·vue.js·前端框架·html
一起养小猫15 分钟前
Flutter for OpenHarmony 实战:投票管理系统完整开发指南
flutter
coloma201216 分钟前
COCOS代码动态增加刚体和碰撞体的方法
前端·uv
想逃离铁厂的老铁20 分钟前
Day60 >> 94、城市间货物运输1️⃣ + 95、城市间货物运输 2️⃣ + 96、城市间货物运输 3️⃣
java·服务器·前端