如何在StreamBuilder之外访问块数据?

时间:2019-07-06 00:40:34

标签: flutter bloc

我的屏幕需要共享按钮才能共享流中加载的当前电影。

问题是,由于我只能访问 StreamBuilder 中的快照,并且共享按钮处于打开状态,所以我不知道如何获取当前电影。 AppBar

我尝试创建一个高范围变量,将其加载到StreamBuilder上并从AppBar访问它,但是它返回null:

class _MoviePageState extends State<MoviePage> {
  MovieModel _movie = null;

  @override
  Widget build(BuildContext context) {
    movieBloc.fetchMovie(widget.movieId);

    return Scaffold(
      body: SafeArea(
        child: Stack(
          children: <Widget>[
            Container(
              color: Colors.transparent,
            ),
            StreamBuilder(
              stream: movieBloc.movie,
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                if (snapshot.hasData) {
                  _movie = snapshot.data as MovieModel;

                  return ListView(
                    children: <Widget>[
                      MovieHeader(movie: _movie),
                      Container(
                        padding: EdgeInsets.only(top: 45, bottom: 15, left: 15, right: 15),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Text('Sinopse:', style: Theme.of(context).textTheme.title),
                            HtmlWidget(
                              _movie.sinopsis,
                              bodyPadding: EdgeInsets.only(top: 15),
                              textStyle: TextStyle(color: Colors.grey),
                            )
                          ],
                        ),
                      )
                    ]
                  );
                }

                return Center(child: CircularProgressIndicator());
              },
            ),
            AppBar(
              backgroundColor: Colors.transparent,
              elevation: 0.0,
              actions: <Widget>[
                PopupMenuButton(
                  icon: Icon(Icons.more_vert),
                  itemBuilder: (BuildContext context) {
                    return <PopupMenuItem>[
                      PopupMenuItem(
                        child: GestureDetector(
                          child: Text('Partilhar'),
                          onTap: () {
                            // Returns null
                            final movieSlug = _movie.slug;

                            Share.share('https://imdb.com/movie/${movieSlug}');
                          },
                        ),
                      )
                    ];
                  },
                )
              ],
            ),
          ]
        ),
      ),
    );
  }
}

如何在StreamBuider之外的流上访问当前电影?

0 个答案:

没有答案