AlertDialog在Function / OnTap中设置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AlertDialog在Function / OnTap中设置相关的知识,希望对你有一定的参考价值。

新的扑扑。我知道如何设置状态警报对话框,但需要点击功能如()=> _createPlayer,它不想重建警报对话框。我想知道当你需要点击它们时如何在警告对话框中设置状态。

 File _image;

    GestureDetector(
                onTap: () => _createPlayer(),

点击后,它将显示如下警告对话框:

_createPlayer() 
    return showDialog(
        context: context,
        builder: (BuildContext context) 
          return AlertDialog(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(32.0))),
            content: Container(
              height: 400,
              width: 300,
              child: Column(
                children: <Widget>[
                  Text('Create Player', style: Theme
                      .of(context)
                      .textTheme
                      .body1),
                  GestureDetector(
                    onTap: _getImageCamera,
                    child: CircleAvatar(
                      radius: 100,
                      backgroundColor: Colors.white,
                      backgroundImage: _image != null ? FileImage(_image) : AssetImage('assets/images/undercover.png'),
                    ),
                  ),
                ],
              ),
            ),
          );
        );
  

_getImageCamera() async
    var image = await ImagePicker.pickImage(source: ImageSource.camera);

    setState(() 
      _image = image;
    );
  

我想在选中时在警告对话框中设置状态/更改图像。任何的想法?

答案

为AlertDialog创建一个单独的Stateful Widget CustomDialog,并在其中移动_getImageCamera函数_image变量,就像这样

_createPlayer() 
    return CustomDialog();

class CustomDialog extends StatefulWidget 
  @override
  State<StatefulWidget> createState() 
    return CustomDialogState();
  



class CustomDialogState extends State<CustomDialog> 
ImageProvider _image;
@override
  void initState() 
    super.initState();

@override
  Widget build(BuildContext context) 
    return AlertDialog(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(32.0))),
            content: Container(
              height: 400,
              width: 300,
              child: Column(
                children: <Widget>[
                  Text('Create Player', style: Theme
                      .of(context)
                      .textTheme
                      .body1),
                  GestureDetector(
                    onTap: _getImageCamera,
                    child: CircleAvatar(
                      radius: 100,
                      backgroundColor: Colors.white,
                      backgroundImage: _image != null ? FileImage(_image) : AssetImage('assets/images/undercover.png'),
                    ),
                  ),
                ],
              ),
            ),
          );
        );



_getImageCamera() async
    var image = await ImagePicker.pickImage(source: ImageSource.camera);

    setState(() 
      _image = image;
    );
  

另一答案

要在showDialog上查看UI更改,您必须创建一个新的StatefulWidget,然后在该类中使用对话框。 Here is the example/sample code

另一答案

您可以使用StatefulBuilder进行对话内部的更改

showDialog(
  context: context,
  builder: (context) 
    String contentText = "Content of Dialog";

    // add StatefulBuilder to return value

    return StatefulBuilder(
      builder: (context, setState) 
        return AlertDialog(
          title: Text("Title of Dialog"),
          content: Text(contentText),
          actions: <Widget>[
            FlatButton(
              onPressed: () => Navigator.pop(context),
              child: Text("Cancel"),
            ),
            FlatButton(
              onPressed: () 
                setState(() 
                  contentText = "Changed Content of Dialog";
                );
              ,
              child: Text("Change"),
            ),
          ],
        );
      ,
    );
  ,
);

以上是关于AlertDialog在Function / OnTap中设置的主要内容,如果未能解决你的问题,请参考以下文章

执行顺序是啥? jQuery: $(document).ready(function () ); $(window).on('load', function () ) [重复]

如何在 Flutter 中将 AlertDialog FlatButton 居中

如何在 Flutter 中刷新 AlertDialog?

在 AlertDialog 中向 LinearLayout 添加多行

如何在Click上修复错误的alertDialog?

jquery - 使用 $(document).on('play', 'video', function()) 时播放不起作用