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

前端 未结 5 1503
长情又很酷
长情又很酷 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: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(
          context: context,
          builder: (BuildContext context) {
            return TheBottomSheet();
          },
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: ...
          floatingActionButton: FloatingActionButton(
            onPressed: _openFilterSchedule,
            child: Icon(Icons.filter_list),
          ),
        );
      }
    }
    

提交回复
热议问题