如何从StreamBuilder中访问快照?

时间:2019-07-11 09:29:21

标签: flutter dart bloc

我正在使用 BLOC 从API提取电影。我可以访问 StreamBuilder 小部件内的块,但是在 AppBar 上也有一个共享按钮,需要 movie.slug 以显示共享选项。但是由于AppBar不在StreamBuilder中,所以我无法共享链接。 AppBar有什么方法可以访问StreamBuilder 快照

这是页面代码:


    No cache could be resolved for 'Builder[public java.lang.String hello.NewKid.whatsYourName(java.lang.String)] caches=[] | key='' | keyGenerator='' | cacheManager='new.kid' | cacheR
    esolver='' | condition='' | unless='' | sync='false'' using resolver 'org.springframework.cache.interceptor.SimpleCacheResolver@171e2c13'. At least one cache should be provided per cache operation.

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您可以摆脱Stack,将StreamBuilder设为Scaffold的父级,这样整个Scaffold(包括应用程序栏)都可以访问快照。

class MoviePage extends StatefulWidget {
  final int movieId;

  MoviePage({this.movieId});

  @override
  _MoviePageState createState() => _MoviePageState();
}

class _MoviePageState extends State<MoviePage> {
  MovieModel _movie;

@override
  void initState() {
    super.initState();
    movieBloc.fetchMovie(widget.movieId);
  }

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

    return StreamBuilder(
      stream: movieBloc.movie,
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (snapshot.hasData) {
          _movie = snapshot.data as MovieModel;

          return Scaffold(
            appBar:  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: () {
                            final movieSLug = _movie.slug;

                            // prints: 'https://cinema.com/movie/null';
                            final movieAddress = 'https://cinema.com/movie/${movieSLug}';

                            Share.share(movieAddress);
                          },
                        ),
                      )
                    ];
                  },
                )
              ],
            ),
            body: SafeArea(
              child: 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());
      },
    );
  }
}