basic_app_bar.dart 3.47 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';

7 8 9
// This app is a stateful, it tracks the user's current choice.
class BasicAppBarSample extends StatefulWidget {
  @override
10
  _BasicAppBarSampleState createState() => _BasicAppBarSampleState();
11 12 13 14 15 16 17 18 19 20 21 22 23
}

class _BasicAppBarSampleState extends State<BasicAppBarSample> {
  Choice _selectedChoice = choices[0]; // The app's "state".

  void _select(Choice choice) {
    setState(() { // Causes the app to rebuild with the new _selectedChoice.
      _selectedChoice = choice;
    });
  }

  @override
  Widget build(BuildContext context) {
24 25 26
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
27 28
          title: const Text('Basic AppBar'),
          actions: <Widget>[
29 30
            IconButton( // action button
              icon: Icon(choices[0].icon),
31 32
              onPressed: () { _select(choices[0]); },
            ),
33 34
            IconButton( // action button
              icon: Icon(choices[1].icon),
35 36
              onPressed: () { _select(choices[1]); },
            ),
37
            PopupMenuButton<Choice>( // overflow menu
38 39
              onSelected: _select,
              itemBuilder: (BuildContext context) {
40
                return choices.skip(2).map<PopupMenuItem<Choice>>((Choice choice) {
41
                  return PopupMenuItem<Choice>(
42
                    value: choice,
43
                    child: Text(choice.title),
44 45 46 47 48 49
                  );
                }).toList();
              },
            ),
          ],
        ),
50
        body: Padding(
51
          padding: const EdgeInsets.all(16.0),
52
          child: ChoiceCard(choice: _selectedChoice),
53 54 55 56 57
        ),
      ),
    );
  }
}
58 59 60 61 62 63 64

class Choice {
  const Choice({ this.title, this.icon });
  final String title;
  final IconData icon;
}

65 66 67 68 69 70 71
const List<Choice> choices = <Choice>[
  Choice(title: 'Car', icon: Icons.directions_car),
  Choice(title: 'Bicycle', icon: Icons.directions_bike),
  Choice(title: 'Boat', icon: Icons.directions_boat),
  Choice(title: 'Bus', icon: Icons.directions_bus),
  Choice(title: 'Train', icon: Icons.directions_railway),
  Choice(title: 'Walk', icon: Icons.directions_walk),
72 73 74 75 76 77 78 79 80
];

class ChoiceCard extends StatelessWidget {
  const ChoiceCard({ Key key, this.choice }) : super(key: key);

  final Choice choice;

  @override
  Widget build(BuildContext context) {
81
    final TextStyle textStyle = Theme.of(context).textTheme.headline4;
82
    return Card(
83
      color: Colors.white,
84 85
      child: Center(
        child: Column(
86 87 88
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
89 90
            Icon(choice.icon, size: 128.0, color: textStyle.color),
            Text(choice.title, style: textStyle),
91 92 93 94 95 96 97
          ],
        ),
      ),
    );
  }
}

98
void main() {
99
  runApp(BasicAppBarSample());
100 101
}

102 103
/*
Sample Catalog
104

105
Title: AppBar Basics
106

107
Summary: A basic AppBar with a title, actions, and an overflow dropdown menu.
108

109 110 111
Description:
An app that displays one of a half dozen choices with an icon and a title.
The two most common choices are available as action buttons and the remaining
Josh Soref's avatar
Josh Soref committed
112
choices are included in the overflow dropdown menu.
113 114 115 116 117 118 119 120 121

Classes: AppBar, IconButton, PopupMenuButton, Scaffold

Sample: BasicAppBarSample

See also:
  - The "Layout-Structure" section of the material design specification:
    <https://material.io/guidelines/layout/structure.html#structure-app-bar>
*/