一个典型的AppBar,带有标题、操作和溢出的下拉菜单。

    Android screenshot

    一个应用程序有六种操作选项,选项由图标和标题组成。两个最常用的选项可用作操作按钮,其余选项包含在溢出下拉菜单中。

    通过flutter create命令创建一个新项目,并用下面的代码替换lib/main.dart的内容来尝试运行一下。

    1. // Copyright 2017 The Chromium Authors. All rights reserved.
    2. // Use of this source code is governed by a BSD-style license that can be
    3. // found in the LICENSE file.
    4. import 'package:flutter/material.dart';
    5. // This app is a stateful, it tracks the user's current choice.
    6. class BasicAppBarSample extends StatefulWidget {
    7. @override
    8. _BasicAppBarSampleState createState() => new _BasicAppBarSampleState();
    9. }
    10. class _BasicAppBarSampleState extends State<BasicAppBarSample> {
    11. Choice _selectedChoice = choices[0]; // The app's "state".
    12. void _select(Choice choice) {
    13. setState(() { // Causes the app to rebuild with the new _selectedChoice.
    14. _selectedChoice = choice;
    15. });
    16. }
    17. @override
    18. Widget build(BuildContext context) {
    19. return new MaterialApp(
    20. home: new Scaffold(
    21. appBar: new AppBar(
    22. title: const Text('Basic AppBar'),
    23. actions: <Widget>[
    24. new IconButton( // action button
    25. icon: new Icon(choices[0].icon),
    26. onPressed: () { _select(choices[0]); },
    27. ),
    28. new IconButton( // action button
    29. icon: new Icon(choices[1].icon),
    30. onPressed: () { _select(choices[1]); },
    31. ),
    32. new PopupMenuButton<Choice>( // overflow menu
    33. onSelected: _select,
    34. itemBuilder: (BuildContext context) {
    35. return choices.skip(2).map((Choice choice) {
    36. return new PopupMenuItem<Choice>(
    37. value: choice,
    38. child: new Text(choice.title),
    39. );
    40. }).toList();
    41. },
    42. ),
    43. ],
    44. ),
    45. body: new Padding(
    46. padding: const EdgeInsets.all(16.0),
    47. child: new ChoiceCard(choice: _selectedChoice),
    48. ),
    49. ),
    50. );
    51. }
    52. }
    53. class Choice {
    54. const Choice({ this.title, this.icon });
    55. final String title;
    56. final IconData icon;
    57. }
    58. const List<Choice> choices = const <Choice>[
    59. const Choice(title: 'Car', icon: Icons.directions_car),
    60. const Choice(title: 'Bicycle', icon: Icons.directions_bike),
    61. const Choice(title: 'Boat', icon: Icons.directions_boat),
    62. const Choice(title: 'Bus', icon: Icons.directions_bus),
    63. const Choice(title: 'Train', icon: Icons.directions_railway),
    64. const Choice(title: 'Walk', icon: Icons.directions_walk),
    65. ];
    66. class ChoiceCard extends StatelessWidget {
    67. const ChoiceCard({ Key key, this.choice }) : super(key: key);
    68. final Choice choice;
    69. @override
    70. Widget build(BuildContext context) {
    71. final TextStyle textStyle = Theme.of(context).textTheme.display1;
    72. return new Card(
    73. color: Colors.white,
    74. child: new Center(
    75. child: new Column(
    76. mainAxisSize: MainAxisSize.min,
    77. crossAxisAlignment: CrossAxisAlignment.center,
    78. children: <Widget>[
    79. new Icon(choice.icon, size: 128.0, color: textStyle.color),
    80. new Text(choice.title, style: textStyle),
    81. ],
    82. ),
    83. ),
    84. );
    85. }
    86. }
    87. void main() {
    88. runApp(new BasicAppBarSample());
    89. }

    也可以看看: