gpt4 book ai didi

flutter - 在 bloc 模式上打开抽屉

转载 作者:IT王子 更新时间:2023-10-29 07:19:44 25 4
gpt4 key购买 nike

几天后解决这个问题,我不能

我在我的应用程序上实现了简单的 blok 模式,我想通过按 FloatingActionButtonIcons.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 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 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();
},
)),
);

关于flutter - 在 bloc 模式上打开抽屉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56216296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com