【问题标题】:Checkbox not check inside showDialog flutter复选框未检查 showDialog 内颤动
【发布时间】:2021-07-25 10:39:12
【问题描述】:

我已经设置了我的代码,当我点击列表磁贴时,它应该会弹出一个对话框,在对话框内我有 3 个复选框,它们正在工作,因为值正在正确更新,但复选标记首先出现除非我第一次点击,重新打开对话框,它会出现,如果有人可以帮助解决我的问题,提前谢谢你。

  • 这是我的代码

  void _showTemperatureUnit(BuildContext context){
      showDialog(
          context: context,
          builder: (context){
            return StatefulBuilder(
              builder: (context,state){
                return AlertDialog(
                  title: Text('Pick A Unit',style: TextStyle(fontFamily: 'bebas',
                      fontWeight: FontWeight.bold),),
                  content: Container(
                    height: MediaQuery.of(context).size.height / 5,
                    child: Column(
                      children: [
                        Row(
                          children: [
                            Expanded(child: Text('Celsius ',style: TextStyle(fontFamily: 'bebas',
                                fontWeight: FontWeight.bold),),flex: 9,),
                            Flexible(
                              child: Checkbox(
                                value: _isCelsius,
                                onChanged: (val){
                                  setState(() {
                                    _isCelsius = val!;
                                    _isKelvin = false;
                                    _isFer = false;
                                    _selectedUnit = 'metric';
                                  });
                                },
                              ),
                              flex: 1,
                            )
                          ],
                        ),
                        SizedBox(height: 5,),
                        Row(
                          children: [
                            Expanded(child: Text("Fahrenheit",style: TextStyle(fontFamily: 'bebas',
                                fontWeight: FontWeight.bold),),flex: 9,),
                            Flexible(
                              child: Checkbox(
                                value: _isFer,
                                onChanged: (val){
                                  setState(() {
                                    _isFer = val!;
                                    _isCelsius = false;
                                    _isKelvin = false;
                                    _selectedUnit  = 'standard';
                                  });
                                },
                              ),
                              flex: 1,
                            )
                          ],
                        ),
                        SizedBox(height: 5,),
                        Row(
                          children: [
                            Expanded(child: Text('Kelvin',style: TextStyle(fontFamily: 'bebas',
                                fontWeight: FontWeight.bold),),flex: 9,),
                            Flexible(
                              child: Checkbox(
                                value: _isKelvin,
                                onChanged: (val){
                                  setState(() {
                                    _isKelvin = val!;
                                    _isFer  = false;
                                    _isCelsius = false;
                                    _selectedUnit = 'imperial';
                                  });
                                },
                              ),
                              flex: 1,
                            )
                          ],
                        )
                      ],
                    ),
                  ),
                  actions: [
                    RaisedButton(onPressed: () async {
                      SharedPreferences prefs = await SharedPreferences.getInstance();
                      prefs.setString('unit', _selectedUnit);
                      Navigator.push(context, MaterialPageRoute(builder: (context) => WeatherPage()));
                    },child: Text('Pick',style: TextStyle(color: Colors.white),),color: Colors.blue,),
                    RaisedButton(onPressed: (){
                      Navigator.pop(context);
                    },child: Text('Cancel',style: TextStyle(color: Colors.white),),color: Colors.blue,)
                  ],
                );
              },
            );
          });
  }

【问题讨论】:

    标签: flutter kotlin checkbox dialog


    【解决方案1】:

    替换

    StatefulBuilder(
                builder: (context, state) {
    

    设置状态

    StatefulBuilder(
                builder: (context, setState) {
    

    因为setState是需要触发的函数调用

    StatefulBuilder 创建一个既具有状态又将其构建委托给回调的小部件。

    【讨论】:

      【解决方案2】:

      您正在使用调用_showTemperatureUnit 函数的有状态小部件的setState。但是检查您的StatefulBuilder builder 参数,您正在执行以下操作

      builder: (context,state){
        ...
      }
      

      这意味着如果您需要重建对话框的状态,您需要使用StatefulBuilderstate 函数,而不是调用该函数的有状态小部件的setState

      所以你需要改变你的代码如下:

      onChanged: (val){
          state(() {
             _isFer = val!;
             _isCelsius = false;
             _isKelvin = false;
             _selectedUnit  = 'standard';
           });
          },
      

      您可以将关键字state 转换为您想要的任何关键字

      【讨论】:

        猜你喜欢
        • 2021-03-11
        • 1970-01-01
        • 1970-01-01
        • 2021-05-10
        • 2018-02-09
        • 1970-01-01
        • 2014-04-26
        • 1970-01-01
        • 2021-09-25
        相关资源
        最近更新 更多