2017-05-09 98 views
1

有沒有辦法讓PopupMenu的項目水平對齊而不僅僅是垂直對齊?PopupMenuEntry的水平對齊

我想添加更多的行爲到一個小部件,並作爲一個PopupMenu的孩子提供除了渲染的所有要求通過。

回答

3

顫振的popup menu有很多內部常量,它要求彈出的軸是垂直的。如果要更改軸並完全控制佈局和大小,您可以複製該文件並開始編輯它。

如果您對佈局不太挑剔,您可以通過將小部件作爲單個彈出式菜單項嵌入來僞裝它。下面是一些代碼證明方法:

screenshot

import 'package:flutter/material.dart'; 

void main() { 
    runApp(new MyApp()); 
} 

class MyApp extends StatelessWidget { 
    @override 
    Widget build(BuildContext context) { 
    return new MaterialApp(
     title: 'Flutter Demo', 
     home: new MyHomePage(), 
    ); 
    } 
} 

/// An arbitrary widget that lives in a popup menu 
class PopupMenuWidget<T> extends PopupMenuEntry<T> { 
    const PopupMenuWidget({ Key key, this.height, this.child }) : super(key: key); 

    @override 
    final Widget child; 

    @override 
    final double height; 

    @override 
    bool get enabled => false; 

    @override 
    _PopupMenuWidgetState createState() => new _PopupMenuWidgetState(); 
} 

class _PopupMenuWidgetState extends State<PopupMenuWidget> { 
    @override 
    Widget build(BuildContext context) => widget.child; 
} 


class MyHomePage extends StatelessWidget { 
    MyHomePage({Key key}) : super(key: key); 

    @override 
    Widget build(BuildContext context) { 
    return new Scaffold(
     appBar: new AppBar(
     actions: <Widget>[ 
      new PopupMenuButton<String>(
      onSelected: (String value) { 
       print("You selected $value"); 
      }, 
      itemBuilder: (BuildContext context) { 
       return [ 
       new PopupMenuWidget(
        height: 40.0, 
        child: new Row(
        children: [ 
         new IconButton(
         icon: new Icon(Icons.add), 
         onPressed:() => Navigator.pop(context, 'add')), 
         new IconButton(
         icon: new Icon(Icons.remove), 
         onPressed:() => Navigator.pop(context, 'remove')), 
        ], 
       ), 
       ), 
       ]; 
      } 
     ), 
     ], 
    ), 
    ); 
    } 
} 
+0

我已經挖成的代碼,我一直在尋找有關最好的方法確認。謝謝@collin。 –