翩翩起舞的剑客在拍打中投掷失败的断言。

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

我实现了一个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)));
  }
}

结果出现了以下错误:

enter image description here

setState()或markNeedsBuild()在构建过程中被调用。

'package:fluttersrcwidgetsnavigator.dart'。断言失败:第1762行pos 12:'!_debugLocked':不是true。

我应该如何实现新的 _goToBookPage 方法时 刷卡器 项被拍下?

flutter push swiper navigator
1个回答
1
投票

与其这样做,不如用GestureDetector Widget将整个应该被拍打的Widget包裹起来,然后用一个匿名函数进行方法调用。

GestureDetector(
  onTap: () {
    // Your_method_call_here
  },

  child: Your_Widget(
      ....
    )
);
© www.soinside.com 2019 - 2024. All rights reserved.