【问题标题】:Custom drawer icon flutter自定义抽屉图标颤动
【发布时间】:2023-03-13 23:57:01
【问题描述】:

我正在尝试使抽屉图标大于默认大小,我正在使用width: MediaQuery.of (context) .size.width * 0.85,但它不起作用,我不知道为什么。然后我想让ListView 来自左侧而不是右侧,但我需要菜单图标位于右侧。我该怎么做?

endDrawer: Container(
        width: MediaQuery.of(context).size.width * 0.85,
        child: Drawer(
          child: ListView(
            padding: EdgeInsets.zero,
            children:  <Widget>[
              DrawerHeader(
                decoration: BoxDecoration(
                  color: Colors.deepOrange,
                ),
                child: Text(
                  'Drawer Header',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 24,
                  ),
                ),
              ),
              ListTile(
                  leading: Icon(Icons.message),
                  title: Text('Messages'),
                  onTap: () {
                    Navigator.push(context,MaterialPageRoute(builder: (context) => StantonPlanet()));
                  }
              ),
              ListTile(
                leading: Icon(Icons.account_circle),
                title: Text('Profile'),
              ),
              ListTile(
                leading: Icon(Icons.settings),
                title: Text('Settings'),
              ),
            ],
          ),
        ),

【问题讨论】:

  • “ListView 来自左侧而不是右侧”是什么意思?
  • 这里侧边栏从右边开始到左边结束,而不是我想从左边开始到右边结束imgur.com/a/GygwllG
  • 明白了,那么抽屉图标呢?你能分享一下你现在拥有的代码吗?
  • 设置右侧抽屉我使用endDrawer(见上文)但是当我按下按钮打开侧边栏时它从右侧打开它,因为我相信它是`endDrawer`的属性而且我不知道如何按住右侧的按钮从左到右“强制”打开。 Whit width: MediaQuery.of(context).size.width * 0.85, 我试图让图标变大,但它不起作用
  • 我明白这一点。你提到了图标大小的问题,但在上面的代码中我看不到使用的图标。

标签: flutter dart flutter-layout


【解决方案1】:
Scaffold(
  appBar: AppBar(
    title:Text('hi'),
    leading: IconButton(
      icon: Icon(Icons.menu),
      onPressed: () => Scaffold.of(context).openDrawer(),
    ),
  ),

【讨论】:

  • 它给了我这个错误:Scaffold.of() called with a context that does not contain a Scaffold.
  • 你也可以使用全局键 `GlobalKey key;'并在脚手架类的关键属性中使用此键。现在您可以使用 key.currentState.openDrawer 代替 Scaffold.of(context).openDrawer()
【解决方案2】:

我找到了解决办法

appBar: AppBar(
    automaticallyImplyLeading: false,
    //backgroundColor: red,
    toolbarHeight: 120,
    centerTitle: false,
    actions: [
      IconButton(
        icon: Icon(Icons.menu_rounded),
        iconSize: 35,
        onPressed: () => _scaffoldKey.currentState!.openDrawer()
      ),
    ],
    leading: IconButton(
      icon: Icon(Icons.arrow_back),
      iconSize: 35,
      onPressed: () => Navigator.of(context).pop(),
    ),
    title: Text('App Bar', style: TextStyle(color: white, fontSize: 30,),),
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.vertical(
        bottom: Radius.circular(30),
      ),
    ),
  ),

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多