首页 > 解决方案 > 如何更新 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]);
              },
            )
          ],
        );
      }
  );}

标签: androidflutterandroid-alertdialog

解决方案


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

    );
  }
}

推荐阅读