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(),
      ),
    );
  }
}
相关推荐
大数据追光猿7 分钟前
Python中的Flask深入认知&搭建前端页面?
前端·css·python·前端框架·flask·html5
莫忘初心丶9 分钟前
python flask 使用教程 快速搭建一个 Web 应用
前端·python·flask
横冲直撞de40 分钟前
前端接收后端19位数字参数,精度丢失的问题
前端
我是哈哈hh42 分钟前
【JavaScript进阶】作用域&解构&箭头函数
开发语言·前端·javascript·html
摸鱼大侠想挣钱42 分钟前
ActiveX控件
前端
谢尔登44 分钟前
Vue 和 React 响应式的区别
前端·vue.js·react.js
酷酷的阿云1 小时前
Vue3性能优化必杀技:useDebounce+useThrottle+useLazyLoad深度剖析
前端·javascript·vue.js
神明木佑1 小时前
HTML 新手易犯的标签属性设置错误
前端·css·html
老友@1 小时前
OnlyOffice:前端编辑器与后端API实现高效办公
前端·后端·websocket·编辑器·onlyoffice
bin91531 小时前
DeepSeek 助力 Vue 开发:打造丝滑的缩略图列表(Thumbnail List)
前端·javascript·vue.js·ecmascript·deepseek