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

时间:2019-04-01 01:29:33

标签: dart flutter flutter-layout

我正在尝试使用flutter创建一个类似app的博客。我创建了一个主页,其中显示了一些磁贴,每个磁贴代表一个帖子,单击这些磁贴将带您到博客帖子。

我创建了一个预览图块小部件类,该类将图像,文本,标题和控制器作为参数。然后,这些内容将通过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,
    );
  }
}

我认为在动画过程中数据会以某种方式分离出来,但是我不确定如何修复它。

感谢您的帮助。

谢谢!

1 个答案:

答案 0 :(得分:0)

事实证明,我在PageView窗口小部件上有一个GestureDetector,将其删除了,一切正常。