dimanche 25 juillet 2021

Checkbox not check inside showDialog flutter

I have set up my code to when i tap on the list tile , it should pop up a dialog , inside the dialog i have 3 checkboxes , they are working because the value is updating correctly but the check mark dosnt appear at first unless i click first time , reopen dialog , there it appeares , if anyone could help fix my issue , thank you in advance .

  • This is my code

  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,)
                  ],
                );
              },
            );
          });
  }



Aucun commentaire:

Enregistrer un commentaire