实现效果图
次要代码
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(leading: new Icon(Icons.arrow_back_ios),
title: new Text(widget.title),
backgroundColor: Colors.blue,
centerTitle: true,
actions: <Widget>[
// 非暗藏菜单
new IconButton(icon: new Icon(Icons.add_alarm),
tooltip: 'Add Alarm',
onPressed: () {},
),
// 暗藏菜单
new PopupMenuButton<String>(itemBuilder: (BuildContext context) => <PopupMenuItem<String>>[this.getNewMenuItem(Icons.add_a_photo, '菜单 1', '1'),
this.getNewMenuItem(Icons.add_location, '菜单 2', '2'),
this.getNewMenuItem(Icons.audiotrack, '菜单 3', '3'),
],
onSelected: (String action) {switch (action) {
case '1':
{print('菜单 1');
}
break;
case '2':
{print('菜单 2');
}
break;
case '3':
{print('菜单 3');
}
break;
default:
}
},
)
],
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Hi',),
],
),
),
);
}
getNewMenuItem(IconData icon, String text, String id) {
return new PopupMenuItem<String>(
value: id,
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Icon(
icon,
color: Colors.blue,
),
new Text(text)
],
),
);
}
}
残缺代码
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter 标题栏',
theme: ThemeData(primarySwatch: Colors.blue,),
home: MyHomePage(title: 'Flutter 标题栏'),
);
}
}
class MyHomePage extends StatefulWidget {MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(leading: new Icon(Icons.arrow_back_ios),
title: new Text(widget.title),
backgroundColor: Colors.blue,
centerTitle: true,
actions: <Widget>[
// 非暗藏菜单
new IconButton(icon: new Icon(Icons.add_alarm),
tooltip: 'Add Alarm',
onPressed: () {},
),
// 暗藏菜单
new PopupMenuButton<String>(itemBuilder: (BuildContext context) => <PopupMenuItem<String>>[this.getNewMenuItem(Icons.add_a_photo, '菜单 1', '1'),
this.getNewMenuItem(Icons.add_location, '菜单 2', '2'),
this.getNewMenuItem(Icons.audiotrack, '菜单 3', '3'),
],
onSelected: (String action) {switch (action) {
case '1':
{print('菜单 1');
}
break;
case '2':
{print('菜单 2');
}
break;
case '3':
{print('菜单 3');
}
break;
default:
}
},
)
],
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Hi',),
],
),
),
);
}
getNewMenuItem(IconData icon, String text, String id) {
return new PopupMenuItem<String>(
value: id,
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Icon(
icon,
color: Colors.blue,
),
new Text(text)
],
),
);
}
}