Flutter基础-043-PopupMenuButton弹出框

635 阅读1分钟

image.png

image.png

class _MyHomePageState extends State<MyHomePage> {

  String value = "1";
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        //导航栏
        title: Text("Scaffold"),
        actions: <Widget>[
          //导航栏右侧菜单
          IconButton(icon: Icon(Icons.add_alarm), onPressed: () {}),
        ],
      ),
      body: Center(
        child: PopupMenuButton(
            onSelected: (String selectValue){
              setState(() {
                value = selectValue;
              });
            },
            itemBuilder: (BuildContext context) =><PopupMenuItem<String>>[
              new PopupMenuItem(
                  value:"1",
                  child: new Text("11111111",style: TextStyle(color: value=="1"?Colors.red:Colors.grey))
              ),
              new PopupMenuItem(
                  value: "2",
                  child: new Text("22222",style: TextStyle(color: value=="2"?Colors.red:Colors.grey))
              )
            ],
//          initialValue: "1",// 初始值
//          icon: Icon(Icons.change_history), // button显示的icon
          padding: EdgeInsets.all(1),// 内边距  不过好像无效
          child: Text("点击选择",style: TextStyle(backgroundColor: Colors.blue[200],fontSize: 20),),// button显示的文本
        ),
      ),
    );
  }

}