简体   繁体   English

在集团模式上打开抽屉

[英]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 我在应用程序上实现了简单的blok模式,我想通过按FloatingActionButtonIcons.menu图标来打开Drawer ,但是我的代码为

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: HomeEvent类:

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: HomeState类:

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. 用BLoC模式打开抽屉过于复杂。 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. 您需要使用构建器小部件包装FloatingActionButton ,该小部件将为打开抽屉提供正确的上下文,并且无需使用Bloc模式即可打开抽屉。

Smaple code for opening drawer with FAB 使用FAB打开抽屉的Smaple代码

 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();
                              },
                          )),
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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