flutter - 复选框未检查 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,)
],
);
},
);
});
}
解决方案
代替
StatefulBuilder(
builder: (context, state) {
设置状态
StatefulBuilder(
builder: (context, setState) {
就像setState
需要触发的函数调用一样
StatefulBuilder
创建一个既具有状态又将其构建委托给回调的小部件。
推荐阅读
- javascript - FullCalendar 和 JQuery 事件
- php - 你知道如何防止在 WP 中超过主页的页面被索引吗?
- mongodb - 使用 Google Cloud Key Management Service 加密 Mongodb
- mysql - 从包含大数据的表中导出数据时 MySQL 停止
- ios - 使用 RxSwift Observables 进行递归调用
- mysql - 如何在 mysql 中搜索迭代循环 json 数据?
- svn - 是否可以在本地机器上创建 Tortoise SVN 存储库以与不同地方的另一个人协作?
- linux - 过滤管道输入中的目录
- html - CSS:带有水平滚动和下拉按钮的顶部栏
- php - 我在销售收据 mod 请求时收到错误 - 0x80040400:QuickBooks 在解析提供的 XML 文本流时发现错误