android - 如何更新 alertDialog 中的按钮颜色变量?
问题描述
我是新来的,我遇到了警报对话框的问题。类型我有一个警报对话框,它有一个选择颜色按钮,并且已经带有预设颜色,这个按钮会导致另一个警报对话框进行颜色选择。选择这个新颜色后,返回第一个警报对话框,按钮应该带有新颜色,但这不会发生。非常感谢。并且不要打电话给英语,因为我正在使用翻译。
Future<Color> selectColor(BuildContext context, Color currentColor){
Color selectedColor = currentColor;
return showDialog(
context: context,
builder: (BuildContext context){
return AlertDialog(
title: Text('Selecionar Cor'),
content: SingleChildScrollView(
child: BlockPicker(
pickerColor: currentColor,
onColorChanged: (Color color){
selectedColor = color;
},
),
),
actions: <Widget>[
FlatButton(
child: Text('OK'),
onPressed: (){
Navigator.pop(context,selectedColor);
},
)
],
);
}
);
}
Future<List> createCategory(BuildContext context) async{
String name = '';
Color selectedColor = BLUE;
return showDialog(
context: context,
builder: (BuildContext context){
return AlertDialog(
title: Text('Nova Categoria'),
content: Row(
children: <Widget>[
Flexible(
flex: 4,
child:
TextField(
maxLength: 30,
decoration: InputDecoration(
labelText: 'Nome'
),
onChanged: (String value){
name = value;
},
)
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 3),
),
Flexible(
flex: 1,
child:
RaisedButton(
color: selectedColor,
onPressed: () async{
selectedColor = await selectColor(context, selectedColor);
}
)
)
],
),
actions: <Widget>[
FlatButton(
child: Text('CANCELAR'),
onPressed: (){
Navigator.pop(context, null);
},
),
FlatButton(
child: Text('OK'),
onPressed: (){
//print('cor '+selectedColor.toString());
Navigator.pop(context,[name,selectedColor]);
},
)
],
);
}
);}
解决方案
After much searching and testing, find out what the problem was. Is that for every alertDialog for data transeference would have to create a new class of type StatefullWidget. I'll leave the code as it was.
//In main
Future<List> createCategory(BuildContext context) async{
return showDialog(
context: context,
builder: (BuildContext context) => CreateCategory()
);
}
class CreateCategory extends StatefulWidget{
CreateCategory ({Key key}) : super (key: key);
_CreateCategoryState createState() => _CreateCategoryState();
}
class _CreateCategoryState extends State<CreateCategory>{
String _name = '';
Color _currentColor = BLUE;
Widget build(BuildContext context){
return AlertDialog(
title: Text('Nova Categoria'),
content: Row(
children: <Widget>[
Flexible(
flex: 4,
child:
TextField(
maxLength: 30,
decoration: InputDecoration(
labelText: 'Nome'
),
onChanged: (String value){
_name = value;
},
)
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 3),
),
Flexible(
child:
FlatButton(
color: _currentColor,
shape: CircleBorder(),
child: null,
onPressed: selectColor,
)
)
],
),
actions: <Widget>[
FlatButton(
child: Text('CANCELAR'),
onPressed: (){
Navigator.pop(context, null);
},
),
FlatButton(
child: Text('OK'),
onPressed: (){
//print('cor '+selectedColor.toString());
Navigator.pop(context,[_name,_currentColor]);
},
)
],
);
}
void selectColor() async{
final selectedColor = await showDialog<Color>(
context: context,
builder: (BuildContext context) => SelectColorPickerDialog(currentColor: _currentColor,)
);
if(selectedColor != null){
setState((){
_currentColor = selectedColor;
});
}
}
}
class SelectColorPickerDialog extends StatefulWidget{
final Color currentColor;
SelectColorPickerDialog ({Key key, this.currentColor}): super (key: key);
@override
_SelectColorPickerDialogState createState() => _SelectColorPickerDialogState();
}
class _SelectColorPickerDialogState extends State<SelectColorPickerDialog>{
Color _color;
@override
void initState(){
super.initState();
_color = widget.currentColor;
}
Widget build(BuildContext context){
return AlertDialog(
title: Text('Selecionar Cor'),
content: SingleChildScrollView(
child: BlockPicker(
pickerColor: _color,
onColorChanged: (Color color){
setState(() {
_color = color;
});
},
),
),
actions: <Widget>[
FlatButton(
child: Text('OK'),
onPressed: (){
Navigator.pop(context,_color);
},
)
],
);
}
}
推荐阅读
- python - 在应用运行时更改 Flask 应用的配置变量?
- r - 在ggplot中编辑悬停标签文本以显示百分比
- java - Why tested class based autowire annotation throw null exception?
- java - 基于java MYSQL中表上的列合并两个表
- python - 如何在熊猫中将特定值从一列复制到另一列
- python - Python检查字符串列表是否等于另一个忽略大小写的字符串列表
- pyspark - EMR集群中pytest的使用
- html - Firebase 默认 URL 显示“找不到站点”
- jquery - 点击按钮,根据内容量计算打开高度。按钮区域有问题
- android - Kotlin flow 结合了不同类型的流(stateflow 和 flow,或者 sharedflow 和 flow)