首页 > 解决方案 > 我想在我的颤振应用程序中添加一个应用程序抽屉,但在脚手架之外

问题描述

问题是我正在向我使用自定义 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>[],
          ),
        ],
      ),
    );
  }
 }

标签: flutterflutter-layout

解决方案


我能想到一个办法,你可以利用GlobalKeywith ScaffoldStateopenDrawer()上有一个方法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。

希望这可以帮助。


推荐阅读