Flutter:bloc,如何显示警报对话框 [英] Flutter: bloc, how to show an alert dialog

查看:31
本文介绍了Flutter:bloc,如何显示警报对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是块模式和流东西的新手.我想在按下按钮时显示警报对话框,但我找不到方法.其实我的代码是:

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() 或不同的小部件

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

这篇关于Flutter:bloc,如何显示警报对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆