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

前端 未结 5 1461
长情又很酷
长情又很酷 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:14

    You need move Provider to top layer(MaterialApp)

    According to picture, Dialog widget is under MaterialApp, so this is why you using wrong context

    0 讨论(0)
  • 2021-01-02 00:18

    InheritedWidgets, and therefore Providers, are scoped to the widget tree. They cannot be accessed outside of that tree.

    The thing is, using showDialog and similar functions, the dialog is located in a different widget tree – which may not have access to the desired provider.

    It is therefore necessary to add the desired providers in that new widget tree:

    void myShowDialog() {
      final myModel = Provider.of<MyModel>(context, listen: false);
      showDialog(
        context: context,
        builder: (_) {
          return Provider.value(value: myModel, child: SomeDialog());
        },
      );
    }
    
    0 讨论(0)
  • 2021-01-02 00:25

    Provider in showModalBottomSheet (Bottom-Sheet)

    void myBottomSheet() {
      final myModel = Provider.of<MyModel>(context, listen: false);
      showModalBottomShee(
        context: context,
        builder: (_) {
          return ListenableProvider.value(
            value: myModel,
            child: Text(myModel.txtValue),
          );
        },
      );
    }
    
    0 讨论(0)
  • 2021-01-02 00:31

    You should split Scaffold widget and its children, to another StatefulWidget

    From single Widget

    class MainScreen extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return BlocProvider(
          builder: (context) {
            SomeBloc someBloc = SomeBloc();
            someBloc.dispatch(SomeEvent());
            return someBloc;
          },
          child: Scaffold(
            body: ...
            floatingActionButton: FloatingActionButton(
              onPressed: _openFilterSchedule,
              child: Icon(Icons.filter_list),
            ),
          )
        );
      }
    }
    

    Splitted into these two widget

    class MainScreen extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return BlocProvider(
          builder: (context) {
            SomeBloc someBloc = SomeBloc();
            someBloc.dispatch(SomeEvent());
            return someBloc;
          },
          child: Screen(),
        );
      }
    }
    

    and ..

    class Screen extends StatelessWidget {
    
      void _openFilterSchedule() {
        showModalBottomSheet<void>(
          context: context,
          builder: (BuildContext context) {
            return TheBottomSheet();
          },
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: ...
          floatingActionButton: FloatingActionButton(
            onPressed: _openFilterSchedule,
            child: Icon(Icons.filter_list),
          ),
        );
      }
    }
    
    0 讨论(0)
  • 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<Widget> 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<CartProvider>(ctx, listen: 
                                            false).addCart(prod);}),);
        }
      }
    );
    }
    
    0 讨论(0)
提交回复
热议问题