Flutter:集团,如何显示警报对话框

编程入门 行业动态 更新时间:2024-10-26 22:28:48
本文介绍了Flutter:集团,如何显示警报对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是集团模式和流媒体方面的新手.我想在按下按钮时显示一个警报对话框,但是找不到解决方法.其实我的代码是:

I´m new in the bloc pattern and stream stuff. I want to show up an alert dialog when I press a button, but I can´t find a way to do it. Actually my code is:

Widget button() { return RaisedButton( child: Text('Show alert'), color: Colors.blue[700], textColor: Colors.white, onPressed: () { bloc.submit(); }); } return Scaffold( appBar: AppBar( title: Text("Title"), ), body: StreamBuilder( stream: bloc.getAlert, builder: (context, snapshot) { if (snapshot.hasData) { return Text("I have Dataaaaaa ${snapshot.data}"); } else return ListView( children: <Widget>[ Container( button() ) ...

还有BLoC:

final _submissionController = StreamController(); Stream get submissionStream=> _submissionController.stream; Sink get submissionSink=> _submissionController.sink;

我试图做类似的事情:

Widget button() { return StreamBuilder( stream: submissionStream builder: (context, snapshot){ if (snapshot.hasData){ return showDialog(...) }else return RaisedButton( child: Text('Show alert'), color: Colors.blue[700], textColor: Colors.white, onPressed: () { bloc.submit(); }); }

但是,当然,它没有用.

But, of course, it didn´t work.

推荐答案

构建工作时无法显示对话框.当您有新数据时,就可以创建一个新的小部件.在这种情况下,可能不使用流可能会更好,但是如果需要,您应该使用

You can't show a dialog when build working. When you have new data, then you create a new widget. Probably better for you will be not using the stream in this case, but if it necessary you should use

WidgetsBinding.instance.addPostFrameCallback((_)=> yourFunction(context));

WidgetsBinding.instance.addPostFrameCallback((_) => yourFunction(context));

Future.microtask(()=> showDialogFunction(context));

Future.microtask(() => showDialogFunction(context));

在您的情况下

if(snapshot.hasData){WidgetsBinding.instance.addPostFrameCallback((_)=> showDialogFunction(context));}

此代码将在构建方法后启动,因此将立即显示对话框.

This code will be launched after build method, so dialog will show immediately.

Bloc函数始终返回小部件,因此在流中有数据时始终返回button()或其他wiget

Bloc function always return widget, so always return button() or different wiget when stream has data

更多推荐

Flutter:集团,如何显示警报对话框

本文发布于:2023-11-27 16:52:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638826.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   对话框   集团   Flutter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!