2017-04-11 1 views
3

2番目のウィジェットからポップアップメニューを開くにはどうすればよいですか?PopupMenuButtonを開くには?

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); 

私はtileをタップしてbuttonのメニューを開きたいです。

答えて

1

これは動作しますが、洗練され(および上記ライナーのソリューションと同じ表示に問題があります。

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, 
    ), 
    ); 
    } 
} 

私はあなたが実際にのために求めていることはhttps://github.com/flutter/flutter/issues/254によって追跡されているもののようなものが何であるかを疑いますかhttps://github.com/flutter/flutter/issues/8277 - ラベルをコントロールに関連付け、クリック可能にする機能 - Flutterフレームワークの欠けている機能です。

+0

ありがとうございます。これらのgithubの問題で説明されているウィジェットは、探していた。 – DogeLion

0

私はこの動作を達成する方法はないと思います。 onTap属性をタイルに付けることはできますが、「外部」からMenuButtonにアクセスすることはできません

ExpansionPanelsは、リストタイルのように見えるため、簡単に変更および編集できるようになっています。

0

私はあなたの質問に対する解決策を見つけました。 ListTileを含む任意のウィジェットにすることができるPopupMenuButtonに子を提供することができます(下記のコードを参照)。 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, 
) 
関連する問題