0

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?

dippas
  • 58,591
  • 15
  • 114
  • 126
notarealgreal
  • 734
  • 16
  • 29

1 Answers1

1

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(
      ....
    )
);
Jason
  • 433
  • 3
  • 7
  • thanks that worked, more than changing the InkWell to a GestureDetector was replacing the method call with an anonymous function, what's the reason? – notarealgreal Jun 10 '20 at 07:33
  • I'm not exactly what does your method receive, but when you do a direct call like that, it does receive some params from its parent widget instead of just the context. You can verify it by using the method print inside your method before using Navigator. – Jason Jun 10 '20 at 17:59