flutter - 我想在我的颤振应用程序中添加一个应用程序抽屉,但在脚手架之外
问题描述
问题是我正在向我使用自定义 appBar 的应用程序添加一个应用程序抽屉,它是一个不同的类,我想从我没有脚手架的 CustomAppBar 类调用一个应用程序抽屉(它进入白屏当我添加脚手架时)。
我已经尝试了多种方法,我可以想到用我的 IconButton 的 onPressed 属性来调用它。
这是我的主要课程
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Container(
child: ListView(
children: <Widget>[
FirstHalf(),
SizedBox(
height: 45.0,
),
for (var foodItem in fooditemList.foodItems)
ItemContainer(foodItem: foodItem)
],
),
),
),
);
}
}
这是调用 CustomAppBar 的地方
class FirstHalf extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.fromLTRB(35, 25, 0, 0),
child: Column(
children: <Widget>[
CustomAppBar(), //CustomAppBar
SizedBox(
height: 30.0,
),
title(),
SizedBox(
height: 30.0,
),
searchBar(),
SizedBox(
height: 30.0,
),
categories(),
],
),
);
}
这个类的代码并没有到此结束,但我认为这足以分享而不是分享整个代码
这是我想调用应用抽屉的 CustomAppBar 类
class CustomAppBar extends StatelessWidget {
final CartListBLoc bloc = BlocProvider.getBloc<CartListBLoc>();
CustomAppBar();
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(bottom: 15.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(
icon: Icon(Icons.menu),
onPressed: () {},//want to call app drawer here
),
StreamBuilder(
stream: bloc.listStream,
builder: (context, snapshot) {
List<FoodItem> foodItems = snapshot.data;
int length = foodItems != null ? foodItems.length : 0;
return buildGestureDetector(length, context, foodItems);
},
initialData: <FoodItem>[],
),
],
),
);
}
}
解决方案
我能想到一个办法,你可以利用GlobalKey
with ScaffoldState
。openDrawer()
上有一个方法ScaffoldState
。
@override
Widget build(BuildContext context) {
final scaffoldKey = GlobalKey<ScaffoldState>(); // Define key
return Scaffold(
key: scaffoldKey, // pass scaffoldKey to Scaffold
drawer: Drawer(), // your drawer implementation
body: SafeArea(
....
FirstHalf(scaffoldKey: scaffoldKey), // Pass scaffold key down the path.
...
....
}
class FirstHalf extends StatelessWidget {
final GlobalKey<ScaffoldState> scaffoldKey;
FirstHalf(this.scaffoldKey);
@override
Widget build(BuildContext context) {
....
....
CustomAppBar(scaffoldKey), //CustomAppBar
....
}
class CustomAppBar extends StatelessWidget {
final GlobalKey<ScaffoldState> scaffoldKey;
final CartListBLoc bloc = BlocProvider.getBloc<CartListBLoc>();
CustomAppBar(this.scaffoldKey);
@override
Widget build(BuildContext context) {
....
....
onPressed: () {
this.scaffoldKey.currentState.openDrawer() // This opens drawer.
},
....
}
作为一般做法,我通常会尽量避免使用 CustomAppBar。
希望这可以帮助。
推荐阅读
- mysql - MYSQL:将自动增量值插入几列
- python - IndexError:列表索引超出python动态可变长度的范围
- javascript - Javascript如何使用包装器将节点附加到正确的索引中
- angular - 身份验证拦截器在角度 6 中不起作用
- java - 为什么 .forEach(val -> list.add()) 编译而 .forEach(val -> true) 不编译?
- php - 为什么 store_result() 不返回 num_rows 等效于 mysqli 常规语句?
- ms-access - 在 ms 访问中创建 vba 代码以全局影响表单上的每个组合框,而不是为每个单独的 cbo 框创建代码。
- java - Hibernate 从实体生成表不起作用
- architecture - 从架构的角度来看,在一个微服务中包含规范的所有实现是否很好?
- c# - 集成测试:API 控制器无法通过 ASP.NET Core API 调用另一个控制器