简体   繁体   English

如何制作像浮动操作按钮这样的弹出菜单按钮?

[英]How to make a popup Menu Button like Floating Action Button?

I want to have a Button at the appBar that shows me three further buttons when I click on it like a Floating Action with FAB, but which is positioned at the appBar.我想在 appBar 上有一个按钮,当我点击它时会显示另外三个按钮,就像带有 FAB 的浮动操作一样,但它位于 appBar。

like on the top right corner喜欢在右上角

I tried a lot of options like Floating Action buttons, IconButtons, and PopupMenuButton.我尝试了很多选项,例如浮动操作按钮、图标按钮和弹出菜单按钮。

This is my best solution with a PopupMenuButton:这是我使用 PopupMenuButton 的最佳解决方案:

 PopupMenuButton<Choice>( icon: Icon( MyIcon.edit, size: 35, color: Colors.white, ), // child: IconButton( icon: Icon( // MyIcon.edit, // // color: Colors.white, // size: 35, color: Colors.white, // ),onPressed: _buildLayoutContainer, // ), elevation: 0, onSelected: choiceAction, itemBuilder: (BuildContext context) { return choices.map((Choice choice) { if (choice.text == null) { return PopupMenuItem<Choice>( value: choice, child: Row( children: <Widget>[ SizedBox( height: 50, width: 1, ), ], ), ); } else { return PopupMenuItem<Choice>( value: choice, child: Row( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.end, children: <Widget>[ SizedBox( width: 103, ), choice.text, SizedBox( width: choice.width, ), Column( children: <Widget>[ IconButton( icon: choice.icon, onPressed: () => _selectTransaction(context), ), SizedBox( height: choice.height, ) ], ), ], ), ); } }).toList(); }),
I did not find a way to align the icons vertically with the edit button on the top right corner and I could not do a backdrop with an elevation to focus at the three icons like for example in a Drawer Widget or like in picture 1. And this is how it looks: 我没有找到将图标与右上角的编辑按钮垂直对齐的方法,而且我无法制作带有高度的背景以聚焦在三个图标上,例如抽屉小部件或图 1 中的图标。而且这是它的外观:

Is there a more elegant way to make this kind of button, especially solving these two issues with the Backdrop and aligning?有没有更优雅的方法来制作这种按钮,尤其是解决背景和对齐这两个问题?

I would love to hear some advice :)我很想听听一些建议:)

PopupMenuButton support offset , you can use offset to adjust display position PopupMenuButton支持offset ,可以使用offset来调整显示位置
You can reference https://medium.com/flutteropen/widgets-14-popupmenubutton-1f1437bbdce2您可以参考https://medium.com/flutteropen/widgets-14-popupmenubutton-1f1437bbdce2

code snippet代码片段

PopupMenuButton<Choice>(
        offset: Offset(100, 100), 
        icon: Icon(

example from reference document参考文档中的示例

Widget _offsetPopup() => PopupMenuButton<int>(
          itemBuilder: (context) => [
                PopupMenuItem(
                  value: 1,
                  child: Text(
                    "Flutter Open",
                    style: TextStyle(
                        color: TEXT_BLACK, fontWeight: FontWeight.w700),
                  ),
                ),
                PopupMenuItem(
                  value: 2,
                  child: Text(
                    "Flutter Tutorial",
                    style: TextStyle(
                        color: TEXT_BLACK, fontWeight: FontWeight.w700),
                  ),
                ),
              ],
          icon: Icon(Icons.library_add),
          offset: Offset(0, 100),
        );

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

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