在PageView动画期间,对象为“null”,在动画结束时有效

问题描述 投票:0回答:1

我正在尝试使用flutter创建一个类似app的博客。我创建了一个显示一些图块的主页,每个图块代表一个帖子,点击图块会将你带到博客文章。

我创建了一个预览tile小部件类,它将图像,文本,标题和控制器作为参数。然后通过cupertino页面路由将它们传递到帖子页面。

所以一切都按预期工作,当我点击瓷砖时,它会将我带到我想要的数据页面,但是当我在页面视图中滚动图块时,如果在中间滚动上点击了图块,我收到一个错误

    'package:flutter/src/painting/image_resolution.dart': Failed assertion: 
    line 134 pos 15: 'assetName != null': is not true.

这就是我所拥有的:

class HomeScreen extends StatefulWidget {
  @override
  HomeScreenState createState() => HomeScreenState();
}

class HomeScreenState extends State<HomeScreen>{

  final PageController _controller = PageController(viewportFraction: 0.80, initialPage: 0);

  @override
  Widget build(BuildContext context) {

    Widget horizontalList1 = GestureDetector(
      onTap: () {
        Navigator.of(context).push(ArticlePageRoute());
      },
      child: FadeUp(
          child: Container(
              child: PageView(
                scrollDirection: Axis.horizontal,
                controller:_controller,
                children: <Widget>[
                  PreviewTile(image:"lib/images/tile.jpg",title: "Title 1 - Lorem Ipsum", text: "Lorem Ipsum is simply dummy text of the printing and typesetting industry.",controller: _controller),
                 ...
                ],
              ))),
    );

    Widget home = Container(
        child: MediaQuery.removePadding(
          context: context,
          child: ListView(
            children: <Widget>[
              header,
              horizontalList1,
            ],
          ),
        ));

    return Scaffold(
      resizeToAvoidBottomPadding: false,
      body: home,
    );
  }
}
class ArticlePageRoute extends CupertinoPageRoute {
  ArticlePageRoute() : super(builder: (BuildContext context) => ArticlePage());

  // OPTIONAL IF YOU WISH TO HAVE SOME EXTRA ANIMATION WHILE ROUTING
  @override
  Widget buildPage(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation) {
    return ArticlePage();

  }
}
class PreviewTile extends StatefulWidget {
  final String image;
  final String text;
  final String title;
  final PageController controller;

  PreviewTile(
      {@required this.image,
      @required this.title,
      @required this.text,
      @required this.controller});

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

class _PreviewTileState extends State<PreviewTile> {
  @override
  Widget build(BuildContext context) {
    Widget tile = GestureDetector(
      onTap: () {
        Navigator.of(context).push(ArticlePageRoute(widget.title,widget.text,widget.image));
      },
      child: Container(

        child: Column(
          children: <Widget>[
            Container(

              child: ClipRRect(
                child: ParallaxImage(
                  controller: widget.controller,
                  image: AssetImage(widget.image),
                ),
              ),
            ),
            Container(
                child: Column(
                  children: <Widget>[
                    Material(
                        child: Text(
                          widget.title,
                        )),
                    Material(
                      child: Text(
                        widget.text,
                      ),
                    )
                  ],
                )),
          ],
        ),
      ),
    );
    return tile;
  }
}

class ArticlePageRoute extends CupertinoPageRoute {
  final String titleText;
  final String bodyText;
  final String imageURL;

  ArticlePageRoute(this.titleText, this.bodyText, this.imageURL) : super(builder: (BuildContext context) => ArticlePage(image:imageURL,title:titleText,body:bodyText));

  // OPTIONAL IF YOU WISH TO HAVE SOME EXTRA ANIMATION WHILE ROUTING
  @override
  Widget buildPage(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation) {
    return ArticlePage(image:imageURL,title:titleText,body:bodyText);
  }
}

class ArticlePage extends StatefulWidget {
  final String image;
  final String title;
  final String body;

  ArticlePage({
    this.image,
    this.title,
    this.body});

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

class ArticlePageState extends State<ArticlePage> {
  final ScrollController _controller = ScrollController();

  @override
  Widget build(BuildContext context) {
    Widget header = GestureDetector(
      onTap: () {
        Navigator.pop(context);
      },
      ...
          child: ParallaxImage(
              controller: _controller,
              image: AssetImage(widget.image),
             ...
              child: Container(
              ...
                child: Text(
                  widget.title,
                 ...
                ),
              )),
        ),
      ),
    );
   ...
    Widget home = Container(
        child: MediaQuery.removePadding(
          context: context,
          child: ListView(
            controller: _controller,
            children: <Widget>[header, body],
          ),
        ));
    return Scaffold(
      resizeToAvoidBottomPadding: false,
      body: home,
    );
  }
}

我认为在动画期间数据是以某种方式分离的,但我不确定如何解决它。

任何帮助表示赞赏。

谢谢!

dart flutter flutter-layout
1个回答
0
投票

事实证明我在我的PageView Widget上面有一个GestureDetector,我删除了它,一切都很完美。

© www.soinside.com 2019 - 2024. All rights reserved.