简体   繁体   中英

opening Drawer on bloc pattern

after some days to resolve this problem, i can't

i implemented simple blok pattern on my app and i want to open Drawer by pressing on FloatingActionButton or on Icons.menu icon, but my code as

Scaffold.of(context).openDrawer();

don't work

my code:

return Scaffold(
  body: BlocBuilder<HomeEvent, HomeState>(
      bloc: _homeBloc,
      builder: (BuildContext context, HomeState state) {
        Scaffold.of(context).openDrawer(); //<---- don't work
        if (state is HandleDrawerMenuClick) {
          _onWidgetDidBuild(() {
            Scaffold.of(context).openDrawer(); //<---- don't work
            _showToast(context); //<---- work fine
          });
        }
        return WillPopScope(
          onWillPop: () {
            customPop(context);
          },
          child: Directionality(
            textDirection: TextDirection.rtl,
            child: Scaffold(
              primary: true,
              appBar: ApplicationToolbar(homeBloc: _homeBloc),
              floatingActionButton: FloatingActionButton(
                child: Icon(Icons.add),
                onPressed: () {
                  Scaffold.of(context).openDrawer(); //<---- don't work
                  _showToast(context); //<---- work fine
                },
              ),
              floatingActionButtonLocation:
              FloatingActionButtonLocation.centerDocked,
              bottomNavigationBar: AppBottomNavigationBar(),
              drawer: AppDrawer(),
              body: _fragments[_currentIndex],
            ),
          ),
        );
      }),
);

HomeEvent class:

class HomeEvent extends Equatable{
  HomeEvent([List props = const []]) : super(props);
}

class OnDrawerMenuClicked extends HomeEvent {

  @override
  String toString() => 'OnDrawerMenuClicked clicked';
}

class OnDrawerMenuItemsClicked extends HomeEvent {
  var onItem = 1;

  OnDrawerMenuItemsClicked({this.onItem});

  @override
  String toString() => 'OnDrawerMenuItemsClicked clicked';
}

HomeState class:

class HomeState extends Equatable{
  HomeState([List props = const[]]):super(props);
}

class HomeInitial extends HomeState{
  @override
  String toString()=>'HomeInitial';
}
class HandleDrawerMenuClick extends HomeState{
  @override
  String toString()=>'HandleDrawerMenuClick';
}

Opening your drawer with BLoC pattern is overly complicated. You need to wrap your FloatingActionButton with a builder widget that will provide the right context for opening the drawer for you and it opens up without the need of using Bloc pattern.

Smaple code for opening drawer with FAB

 return Scaffold(
      appBar:  AppBar(title: Text('Drawer FAB'),),
      drawer: Drawer(child: Text('drawer content'),),
      floatingActionButton: Builder( builder:(context) => 
                          FloatingActionButton(child: Icon(Icons.add), 
                              onPressed: (){ 
                                  Scaffold.of(context).openDrawer();
                              },
                          )),
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM