Created
February 4, 2020 13:31
-
-
Save ryanlid/0d6823ae9841801d0045a8407c03c64c to your computer and use it in GitHub Desktop.
Cuptino 导航组件集
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: "Cuptino 导航组件集", | |
// 设置浅色主题 | |
theme: ThemeData.light(), | |
home: MyPage(), | |
); | |
} | |
} | |
class MyPage extends StatefulWidget { | |
@override | |
_MyPageState createState() => _MyPageState(); | |
} | |
class _MyPageState extends State<MyPage> { | |
@override | |
Widget build(BuildContext context) { | |
// 最外层导航选项卡 | |
return CupertinoTabScaffold( | |
// 底部选项卡 | |
tabBar: CupertinoTabBar( | |
// 选项卡背景色 | |
backgroundColor: CupertinoColors.lightBackgroundGray, | |
items: [ | |
// 选项卡项,包括文字图标 | |
BottomNavigationBarItem( | |
icon: Icon(CupertinoIcons.home), | |
title: Text('主页'), | |
), | |
BottomNavigationBarItem( | |
icon: Icon(CupertinoIcons.conversation_bubble), | |
title: Text('聊天'), | |
), | |
], | |
), | |
tabBuilder: (context, index) { | |
// 选项卡绑定的视图 | |
return CupertinoTabView( | |
builder: (context) { | |
switch (index) { | |
case 0: | |
return HomePage(); | |
break; | |
case 1: | |
return ChatPage(); | |
break; | |
default: | |
return Container(); | |
} | |
}, | |
); | |
}, | |
); | |
} | |
} | |
// 主页 | |
class HomePage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return CupertinoPageScaffold( | |
navigationBar: CupertinoNavigationBar( | |
middle: Text("主页"), | |
), | |
child: Center( | |
child: Text( | |
"主页", | |
style: Theme.of(context).textTheme.button, | |
), | |
), | |
); | |
} | |
} | |
// 聊天页面 | |
class ChatPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return CupertinoPageScaffold( | |
navigationBar: CupertinoNavigationBar( | |
// 导航栏,包含左中右三部分 | |
// 中间标题 | |
middle: Text("聊天面板"), | |
// 右侧按钮 | |
trailing: Icon(CupertinoIcons.add), | |
// 左侧按钮 | |
leading: Icon(CupertinoIcons.back), | |
), | |
child: Center( | |
child: Text( | |
'聊天面板', | |
style: Theme.of(context).textTheme.button, | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment