Flutter Swiper on tap throws Failed assertion

947 views Asked by At

I have implemented a Swier widget which shows book information in a card, the next step is when the card is taped to push a new page, here is the code:

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)));
  }
}

The result is the following error:

enter image description here

setState() or markNeedsBuild() called during build.

'package:flutter/src/widgets/navigator.dart': Failed assertion: line 1762 pos 12: '!_debugLocked': is not true.

How I should implement the new _goToBookPage method when a Swiper item is tapped?

1

There are 1 answers

2
Jason On BEST ANSWER

Instead of doing it that way, you may want to wrap the whole Widget which is supposed to be tapped with a GestureDetector Widget and do the method call with an anonymous function.

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

  child: Your_Widget(
      ....
    )
);