【问题标题】:Flutter Swiper on tap throws Failed assertionFlutter Swiper on tap 抛出 Failed assertion
【发布时间】:2020-09-29 03:11:50
【问题描述】:

我已经实现了一个 Swier 小部件,它在卡片中显示书籍信息,下一步是当卡片被粘贴以推送新页面时,代码如下:

class CardSwiper extends StatelessWidget {


  final List<Book> books;


  CardSwiper(this.books);

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        backgroundColor: Colors.blueGrey,
        body: new Swiper(
          //containerHeight: 25.0,
          itemBuilder: (BuildContext context, int index) {
            return _getSwiperCardContent(index, context);
          },

          indicatorLayout: PageIndicatorLayout.COLOR,
          autoplay: false,
          itemCount: books.length,
          pagination: null,
          control: null,
          viewportFraction: 0.6,
          scale: 0.9,
        ));
  }

  _getSwiperCardContent(int index, BuildContext context){
    return Card(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(7.0),
        ),
        elevation: 10,
        color: Colors.white,
        child: Column(
          children: <Widget>[
            Flexible(
              child: Align(
                  alignment: Alignment.center,
                  child: InkWell(
                    onTap: _goToBookPage(context),
                    child: Image.network(
                      this.books[index].picture,
                    ),
                  )
              ),
              flex: 6,
            ),

            Flexible(
              child: Align(
                alignment: Alignment.center,
                child: Container(
                  child: Text(
                    this.books[index].title,
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ),
              flex: 1,
            ),


            Flexible(
              child: Align(
                alignment: Alignment.center,
                child: Text(
                  this.books[index].author,
                  style: TextStyle(
                    color: Colors.grey[500],
                  ),
                ),
              ),
              flex: 1,
            )
          ],
        ),

    );
  }

  _goToBookPage(BuildContext context){
    Navigator.of(context)
        .push(MaterialPageRoute(builder: (context) => BookPage("title", this.books)));
  }
}

结果如下错误:

在构建期间调用 setState() 或 markNeedsBuild()。

'package:flutter/src/widgets/navigator.dart':断言失败:第 1762 行 pos 12:'!_debugLocked':不正确。

当点击 Swiper 项目时,我应该如何实现新的 _goToBookPage 方法?

【问题讨论】:

    标签: flutter push swiper navigator


    【解决方案1】:

    您可能希望使用 GestureDetector Widget 包装整个 Widget,并使用匿名函数进行方法调用,而不是这样做。

    GestureDetector(
      onTap: () {
        // Your_method_call_here
      },
    
      child: Your_Widget(
          ....
        )
    );
    

    【讨论】:

    • 感谢成功,不仅仅是将 InkWell 更改为 GestureDetector 而是将方法调用替换为匿名函数,原因是什么?
    • 我不完全是您的方法接收到的内容,但是当您进行这样的直接调用时,它确实会从其父小部件接收一些参数,而不仅仅是上下文。在使用 Navigator 之前,您可以使用方法中的方法 print 来验证它。
    猜你喜欢
    • 1970-01-01
    • 2022-11-11
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 2022-10-17
    相关资源
    最近更新 更多