一个以TabBar作为底部widget的AppBar。

    Android screenshot

    TabBar可用于在TabBarView中显示的页面之间导航。虽然TabBar是一个可以出现在任何地方的普通widget,但它通常包含在应用程序的AppBar中。

    通过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. class TabbedAppBarSample extends StatelessWidget {
    6. @override
    7. Widget build(BuildContext context) {
    8. return new MaterialApp(
    9. home: new DefaultTabController(
    10. length: choices.length,
    11. child: new Scaffold(
    12. appBar: new AppBar(
    13. title: const Text('Tabbed AppBar'),
    14. bottom: new TabBar(
    15. isScrollable: true,
    16. tabs: choices.map((Choice choice) {
    17. return new Tab(
    18. text: choice.title,
    19. icon: new Icon(choice.icon),
    20. );
    21. }).toList(),
    22. ),
    23. ),
    24. body: new TabBarView(
    25. children: choices.map((Choice choice) {
    26. return new Padding(
    27. padding: const EdgeInsets.all(16.0),
    28. child: new ChoiceCard(choice: choice),
    29. );
    30. }).toList(),
    31. ),
    32. ),
    33. ),
    34. );
    35. }
    36. }
    37. class Choice {
    38. const Choice({ this.title, this.icon });
    39. final String title;
    40. final IconData icon;
    41. }
    42. const List<Choice> choices = const <Choice>[
    43. const Choice(title: 'CAR', icon: Icons.directions_car),
    44. const Choice(title: 'BICYCLE', icon: Icons.directions_bike),
    45. const Choice(title: 'BOAT', icon: Icons.directions_boat),
    46. const Choice(title: 'BUS', icon: Icons.directions_bus),
    47. const Choice(title: 'TRAIN', icon: Icons.directions_railway),
    48. const Choice(title: 'WALK', icon: Icons.directions_walk),
    49. ];
    50. class ChoiceCard extends StatelessWidget {
    51. const ChoiceCard({ Key key, this.choice }) : super(key: key);
    52. final Choice choice;
    53. @override
    54. Widget build(BuildContext context) {
    55. final TextStyle textStyle = Theme.of(context).textTheme.display1;
    56. return new Card(
    57. color: Colors.white,
    58. child: new Center(
    59. child: new Column(
    60. mainAxisSize: MainAxisSize.min,
    61. crossAxisAlignment: CrossAxisAlignment.center,
    62. children: <Widget>[
    63. new Icon(choice.icon, size: 128.0, color: textStyle.color),
    64. new Text(choice.title, style: textStyle),
    65. ],
    66. ),
    67. ),
    68. );
    69. }
    70. }
    71. void main() {
    72. runApp(new TabbedAppBarSample());
    73. }

    也可以看看: