Tabbed AppBar
An AppBar with a TabBar as its bottom widget.(一个AppBar,其TabBar作为底部小部件。)
A TabBar can be used to navigate among the pages displayed in a TabBarView. Although a TabBar is an ordinary widget that can appear anywhere, it’s most often included in the application’s AppBar.
(TabBar可用于在TabBarView中显示的页面之间导航。虽然TabBar是一个可以出现在任何地方的普通小部件,但它通常包含在应用程序的AppBar中。)
通过创建一个新项目flutter create并lib/main.dart使用以下代码替换内容来尝试此应用程序。
import 'package:flutter/material.dart';
class TabbedAppBarSample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: choices.length,
child: Scaffold(
appBar: AppBar(
title: const Text('Tabbed AppBar'),
bottom: TabBar(
isScrollable: true,
tabs: choices.map((Choice choice) {
return Tab(
text: choice.title,
icon: Icon(choice.icon),
);
}).toList(),
),
),
body: TabBarView(
children: choices.map((Choice choice) {
return Padding(
padding: const EdgeInsets.all(16.0),
child: ChoiceCard(choice: choice),
);
}).toList(),
),
),
),
);
}
}
class Choice {
const Choice({this.title, this.icon});
final String title;
final IconData icon;
}
const List<Choice> choices = const <Choice>[
const Choice(title: 'CAR', icon: Icons.directions_car),
const Choice(title: 'BICYCLE', icon: Icons.directions_bike),
const Choice(title: 'BOAT', icon: Icons.directions_boat),
const Choice(title: 'BUS', icon: Icons.directions_bus),
const Choice(title: 'TRAIN', icon: Icons.directions_railway),
const Choice(title: 'WALK', icon: Icons.directions_walk),
];
class ChoiceCard extends StatelessWidget {
const ChoiceCard({Key key, this.choice}) : super(key: key);
final Choice choice;
@override
Widget build(BuildContext context) {
final TextStyle textStyle = Theme.of(context).textTheme.display1;
return Card(
color: Colors.white,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Icon(choice.icon, size: 128.0, color: textStyle.color),
Text(choice.title, style: textStyle),
],
),
),
);
}
}
void main() {
runApp(TabbedAppBarSample());
}
本项目中效果图:
实现代码:
class HomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage>
with SingleTickerProviderStateMixin {
/*
TabController _tabController;
List<StatelessWidget> _homeLists = List(_tabs.length);
/// List of the TabBar's tabs
static const List<Tab> _tabs = const <Tab>[
const Tab(text: '页面1'),
const Tab(text: '页面2'),
const Tab(text: '页面3'),
];
@override
void initState() {
super.initState();
// Tab controller init
_tabController = TabController(
length: _tabs.length,
vsync: this,
initialIndex: 1,
);
// List array init
_homeLists = [
VehicleList(),
LaunchList(Url.upcomingList),
LaunchList(Url.launchesList),
];
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
*/
@override
Widget build(BuildContext context) {
return new DefaultTabController(
length: 9,
child: new Scaffold(
appBar: new AppBar(
title: const Text(
'测试!',
style: TextStyle(fontWeight: FontWeight.bold),
),
centerTitle: true,
actions: <Widget>[
IconButton(
icon: const Icon(Icons.info_outline),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (context) => AboutPage()),
),
),
],
bottom: new TabBar(
labelStyle: TextStyle(
fontFamily: 'ProductSans',
fontWeight: FontWeight.bold,
),
isScrollable: true,
tabs: [
new Tab(
text: "关注",
),
new Tab(
text: "推荐",
),
new Tab(
text: "热搜",
), new Tab(
text: "关注1",
),
new Tab(
text: "推荐1",
),
new Tab(
text: "热搜1",
), new Tab(
text: "关注2",
),
new Tab(
text: "推荐2",
),
new Tab(
text: "热搜2",
)
]),
),
body: new TabBarView(children: [
VehicleList(),
LaunchList(Url.upcomingList),
LaunchList(Url.launchesList),
VehicleList(),
LaunchList(Url.upcomingList),
LaunchList(Url.launchesList),
VehicleList(),
LaunchList(Url.upcomingList),
LaunchList(Url.launchesList),
]),
));
}
/*
return Scaffold(
appBar: AppBar(
title: const Text(
'测试!',
style: TextStyle(fontWeight: FontWeight.bold),
),
centerTitle: true,
actions: <Widget>[
IconButton(
icon: const Icon(Icons.info_outline),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (context) => AboutPage()),
),
),
],
bottom: TabBar(
labelStyle: TextStyle(
fontFamily: 'ProductSans',
fontWeight: FontWeight.bold,
),
controller: _tabController,
tabs: _tabs,
),
),
body: TabBarView(controller: _tabController, children: _homeLists),
);
}
*/
}
参考TabBar class 可以查看 TabBar 详细信息。