如何打开PopupMenuButton?

Dog*_*ion 10 dart flutter

如何从第二个小部件打开弹出菜单?

final button = new PopupMenuButton(
    itemBuilder: (_) => <PopupMenuItem<String>>[
          new PopupMenuItem<String>(
              child: const Text('Doge'), value: 'Doge'),
          new PopupMenuItem<String>(
              child: const Text('Lion'), value: 'Lion'),
        ],
    onSelected: _doSomething);

final tile = new ListTile(title: new Text('Doge or lion?'), trailing: button);
Run Code Online (Sandbox Code Playgroud)

我想button通过点击打开菜单tile.

Vis*_*ngh 19

我认为这样做会更好,而不是显示一个 PopupMenuButton

void _showPopupMenu() async {
  await showMenu(
    context: context,
    position: RelativeRect.fromLTRB(100, 100, 100, 100),
    items: [
      PopupMenuItem<String>(
          child: const Text('Doge'), value: 'Doge'),
      PopupMenuItem<String>(
          child: const Text('Lion'), value: 'Lion'),
    ],
    elevation: 8.0,
  );
}
Run Code Online (Sandbox Code Playgroud)

有时您希望 在按下按钮位置显示_showPopupMenu 使用 GestureDetector

final tile = new ListTile(
  title: new Text('Doge or lion?'),
  trailing: GestureDetector(
    onTapDown: (TapDownDetails details) {
      _showPopupMenu(details.globalPosition);
    },
    child: Container(child: Text("Press Me")),
  ),
);
Run Code Online (Sandbox Code Playgroud)

然后 _showPopupMenu 会像

_showPopupMenu(Offset offset) async {
    double left = offset.dx;
    double top = offset.dy;
    await showMenu(
    context: context,
    position: RelativeRect.fromLTRB(left, top, 0, 0),
    items: [
      ...,
    elevation: 8.0,
  );
}
Run Code Online (Sandbox Code Playgroud)

  • 完美的答案,我只需要用其他值替换 0:`RelativeRect.fromLTRB(left, top, left+1, top+1),` (3认同)
  • 如何在您的方法中使用 PopUpMenuButton 的 onSelected 属性?举例来说,如果选择了 Doge 值,我想导航到 Doge.dart 页面 (2认同)

Eri*_*del 11

这是有效的,但是不够优雅(并且与Rainer的解决方案具有相同的显示问题:

class _MyHomePageState extends State<MyHomePage> {
  final GlobalKey _menuKey = new GlobalKey();

  @override
  Widget build(BuildContext context) {
    final button = new PopupMenuButton(
        key: _menuKey,
        itemBuilder: (_) => <PopupMenuItem<String>>[
              new PopupMenuItem<String>(
                  child: const Text('Doge'), value: 'Doge'),
              new PopupMenuItem<String>(
                  child: const Text('Lion'), value: 'Lion'),
            ],
        onSelected: (_) {});

    final tile =
        new ListTile(title: new Text('Doge or lion?'), trailing: button, onTap: () {
          // This is a hack because _PopupMenuButtonState is private.
          dynamic state = _menuKey.currentState;
          state.showButtonMenu();
        });
    return new Scaffold(
      body: new Center(
        child: tile,
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

我怀疑你实际要求的是https://github.com/flutter/flutter/issues/254https://github.com/flutter/flutter/issues/8277跟踪的内容-将标签与控件相关联并使标签可单击的能力 - 这是Flutter框架中缺少的功能.


Cop*_*oad 10

截屏:

在此输入图像描述


完整代码:

class MyPage extends StatelessWidget {
  final GlobalKey<PopupMenuButtonState<int>> _key = GlobalKey();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: [
          PopupMenuButton<int>(
            key: _key,
            itemBuilder: (context) {
              return <PopupMenuEntry<int>>[
                PopupMenuItem(child: Text('0'), value: 0),
                PopupMenuItem(child: Text('1'), value: 1),
              ];
            },
          ),
        ],
      ),
      body: RaisedButton(
        onPressed: () => _key.currentState.showButtonMenu(),
        child: Text('Open/Close menu'),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)


Rai*_*ann 7

我找到了您问题的解决方案。您可以为 PopupMenuButton 提供一个子项,它可以是包括 ListTile 在内的任何小部件(请参阅下面的代码)。唯一的问题是 PopupMenu 在 ListTile 的左侧打开。

final popupMenu = new PopupMenuButton(
  child: new ListTile(
    title: new Text('Doge or lion?'),
    trailing: const Icon(Icons.more_vert),
  ),
  itemBuilder: (_) => <PopupMenuItem<String>>[
            new PopupMenuItem<String>(
                child: new Text('Doge'), value: 'Doge'),
            new PopupMenuItem<String>(
                child: new Text('Lion'), value: 'Lion'),
          ],
  onSelected: _doSomething,
)
Run Code Online (Sandbox Code Playgroud)