首页 > 解决方案 > 复选框未检查 showDialog 内颤动

问题描述

我已经设置了我的代码,当我点击列表磁贴时,它应该会弹出一个对话框,在对话框内我有 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,)
                  ],
                );
              },
            );
          });
  }

标签: flutterkotlincheckboxdialog

解决方案


代替

StatefulBuilder(
            builder: (context, state) {

设置状态

StatefulBuilder(
            builder: (context, setState) {

就像setState需要触发的函数调用一样

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


推荐阅读