简体   繁体   English

Flutter 中的 DropdownButton 溢出

[英]Overflowed By with DropdownButton in Flutter

Background: Hello.背景:你好。 I have just started learning Flutter and I have run into an Overflowed By error when trying to create a dropdown menu in the top left corner of the app I'm building.我刚刚开始学习 Flutter,在我正在构建的应用程序的左上角尝试创建下拉菜单时遇到了溢出错误。

Question: How can I accurately format a dropdown menu in the top left corner of my app.问题:如何准确格式化我的应用左上角的下拉菜单。 I want the menu to be flush with the side of the left screen and the icon in the symmetrical opposite position relative to the rightmost icon as pictured in the attached image.我希望菜单与左侧屏幕的一侧齐平,并且相对于最右边的图标,position 中的图标对称,如图所示。 Before, I had an IconButton instead of a DropdownButton, and the IconButton was itself, automatically positioned correctly.以前,我有一个 IconButton 而不是 DropdownButton,而 IconButton 本身是自动正确定位的。 I would also like to correctly center the menu icon because, as you can see, it is slightly off center-vertical.我还想正确地将菜单图标居中,因为如您所见,它略微偏离中心垂直。

Code:代码:

appBar: AppBar(
      leading: DropdownButton(icon: Icon(Icons.menu, color: colorPalette.chooseColor('black')), items: [DropdownMenuItem(child: Text('Events'),],),
      centerTitle: true,
      title: Text('Some Title', style: TextStyle(color: colorPalette.chooseColor('black'), fontSize: 23),),
      actions: <Widget>[
        IconButton(
          icon: Icon(Icons.map_outlined,  
          ),
          onPressed: () {
            // do something
          },
        )
      ],
    ),

在此处输入图像描述

You just need add drawer property, inside the Scaffold.您只需要在脚手架内添加抽屉属性。 And delete leading from your Appbar.并从您的 Appbar 中删除领先

Scaffold(
  appBar: AppBar(title: Text(title)),
  body: Center(child: Text('My Page!')),
  drawer: Drawer(
  child: ListView(
  children: <Widget>[
  //here you can customize the menu with the widgets you want. Example:
    ListTile(
    title: Text("Item 1"),
    trailing: Icon(Icons.arrow_forward),
       ),
     ],
    ),
  ),

Nice code!不错的代码!

There's no need to use the leading attribute of the AppBar all the time.无需一直使用AppBarleading属性。 We can place the Dropdown in the same position without it overflowing by using title only.我们可以将Dropdown列表放在同一个 position 中,而不会仅使用title溢出。 This way it gives you lots of flexibility in customizing your AppBar widget:通过这种方式,您可以灵活地自定义AppBar小部件:

AppBar(
      leadingWidth: 0,
      automaticallyImplyLeading: false,
      title: Stack(
        alignment: AlignmentDirectional.center,
        children: [
          Positioned(
            left: 0,
            child: DropdownButton(
                // ... other lines
                ),
          ),
          Container(
            width: MediaQuery.of(context).size.width,
            alignment: Alignment.center,
            child: Text(
              'Some Title',
              // ... other lines
            ),
          ),
          Positioned(
            right: 0,
            child: IconButton(icon: Icon(Icons.map_outlined)),
          ),
        ],
      ),
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM