【问题标题】:I want to add an App Drawer in my flutter app but outside of scaffold我想在我的颤振应用程序中添加一个应用程序抽屉,但在脚手架之外
【发布时间】:2019-12-14 07:34:50
【问题描述】:

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

【问题讨论】:

  • 您的Home 类未使用CustomAppBar CustomAppBar 是否应该成为 Home 类的一部分?
  • 真的很抱歉我犯了一个错误,我不得不再展示一个使用 customappbar 作为 appbar 的课程,我一回到家就会编辑问题。
  • 编辑已完成,我是初学者,如果有一点帮助,将不胜感激,谢谢

标签: flutter flutter-layout


【解决方案1】:

我能想到一个办法,你可以使用GlobalKeyScaffoldStateScaffoldState上有一个方法openDrawer()

  @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。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2021-03-26
    • 2020-08-20
    • 2020-11-28
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 2021-01-13
    相关资源
    最近更新 更多