How to access Provided (Provider.of()) value inside showModalBottomSheet?

前端 未结 5 1494
长情又很酷
长情又很酷 2021-01-01 23:50

I have a FloatingActionButton inside a widget tree which has a BlocProvider from flutter_bloc. Something like this:

BlocProvider(
  builder: (co         


        
5条回答
  •  死守一世寂寞
    2021-01-02 00:38

    I found a solution, Just return your showModalBottomSheet with a StatefulBuilder and use the context of your modalsheet builder to pass to your provider. a snippet of my code below:

    Future showModal(int qty, Product product) async {
        return await showModalBottomSheet(
            isScrollControlled: true,
            backgroundColor: Colors.transparent,
            context: context,
            builder: (BuildContext ctx) {
              return StatefulBuilder(builder: (ctx, state) {
                 return Container(
                      child: RaisedButton(
                              onPressed: () {
                                 Product prod = Product(product.id, 
                                             product.sku, product.name, qty);
                                 Provider.of(ctx, listen: 
                                            false).addCart(prod);}),);
        }
      }
    );
    }
    

提交回复
热议问题