在 bloc 模式上打开抽屉
opening Drawer on bloc pattern
几天后解决了这个问题,我不能
我在我的应用程序上实现了简单的块模式,我想通过按 FloatingActionButton
或 Icons.menu
图标打开 Drawer
,但我的代码为
Scaffold.of(context).openDrawer();
不工作
我的代码:
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';
}
用 BLoC 模式打开抽屉过于复杂。您需要使用构建器小部件包装 FloatingActionButton
,该小部件将为您打开抽屉提供正确的上下文,并且无需使用 Bloc 模式即可打开。
使用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();
},
)),
);
几天后解决了这个问题,我不能
我在我的应用程序上实现了简单的块模式,我想通过按 FloatingActionButton
或 Icons.menu
图标打开 Drawer
,但我的代码为
Scaffold.of(context).openDrawer();
不工作
我的代码:
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';
}
用 BLoC 模式打开抽屉过于复杂。您需要使用构建器小部件包装 FloatingActionButton
,该小部件将为您打开抽屉提供正确的上下文,并且无需使用 Bloc 模式即可打开。
使用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();
},
)),
);