实现效果图
文章图片
主要代码
class _MyHomePageState extends State {
@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: [
//非隐藏菜单
new IconButton(
icon: new Icon(Icons.add_alarm),
tooltip: 'Add Alarm',
onPressed: () {},
),
//隐藏菜单
new PopupMenuButton(
itemBuilder: (BuildContext context) => >[
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: [
Text(
'Hi',
),
],
),
),
);
}getNewMenuItem(IconData icon, String text, String id) {
return new PopupMenuItem(
value: id,
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
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 {
@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: [
//非隐藏菜单
new IconButton(
icon: new Icon(Icons.add_alarm),
tooltip: 'Add Alarm',
onPressed: () {},
),
//隐藏菜单
new PopupMenuButton(
itemBuilder: (BuildContext context) => 【Flutter AppBar入门使用】>[
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: [
Text(
'Hi',
),
],
),
),
);
}getNewMenuItem(IconData icon, String text, String id) {
return new PopupMenuItem(
value: id,
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
new Icon(
icon,
color: Colors.blue,
),
new Text(text)
],
),
);
}
}
推荐阅读
- 【Android进阶】Android使用 flutter
- Android 开发 3 年,被要求转 Flutter ,Flutter 真的很香吗
- Flutter主题切换 flutter redux
- flutter-样式
- 跨平台新潮,写给程序员的Flutter完整开发实战详解,带你玩转Flutter
- android studio flutter代理设置问题
- 适用于 Flutter 的 Google 移动广告 SDK 正式版现已发布
- 写给程序员的Flutter完整开发实战详解,轻轻松松解决跨平台烦恼
- flutter添加到android原生