我是新来的人,我的Alertdialog有问题。 类型I的一个Alertdialog具有一个选择颜色按钮,并且已经带有预设的颜色,此按钮会导致另一个Alertdialog进行颜色选择。选择此新颜色后,返回第一个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]);
},
)
],
);
}
);}
答案 0 :(得分:0)
可能会出现问题,因为您未使用setState函数,如果发生某些更改(在这种情况下颜色值正在更改),该函数将更新UI。所以
替换下面的代码
BlockPicker(
pickerColor: currentColor,
onColorChanged: (Color color){
selectedColor = color;
},
),
与此一起
BlockPicker(
pickerColor: currentColor,
onColorChanged: (Color color){
setState(() {
selectedColor = color;
});
},
),
答案 1 :(得分:0)
经过大量搜索和测试后,找出问题所在。是否对于每个用于数据传输的alertDialog都必须创建一个类型为StatefullWidget的新类。我将保持原样。
//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);
},
)
],
);
}
}