flutter tab效果实现
2021-07-04 本文已影响0人
跨界师
开发app时对手机页面的使用,就会要使用到tab分页功能
主要页面代码如下:
class Tabs extends StatefulWidget {
Tabs({Key? key}) : super(key: key);
@override
_TabsState createState() => _TabsState();
}
class _TabsState extends State<Tabs> {
int _currentIndex = 0;
List<Widget> _currentPage = [HomePage(), Settings(), Search()];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: this._currentPage[this._currentIndex],
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: "首页",
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
label: "设置",
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
label: "搜索",
),
],
onTap: (int index) {
setState(() {
this._currentIndex = index;
});
},
fixedColor: Colors.red,
type: BottomNavigationBarType.fixed,
),
);
}
}
上述代码是对底部导航栏进行命名
另外还需要新建几个分页面:
- HomePage
class HomePage extends StatefulWidget {
HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
child: Text("跳转搜索页面"),
onPressed: () {
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => Search()));
},
),
SizedBox(
height: 10.0,
),
ElevatedButton(
child: Text("跳转到表单页面并传值"),
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => FormPage(title: "hahah")));
},
),
],
);
}
}
- Settings
class Settings extends StatefulWidget {
Settings({Key? key}) : super(key: key);
@override
_SettingsState createState() => _SettingsState();
}
class _SettingsState extends State<Settings> {
@override
Widget build(BuildContext context) {
return ListView(
children: [
ListTile(
title: Text("我是一个文本"),
),
ListTile(
title: Text("我是一个文本"),
),
ListTile(
title: Text("我是一个文本"),
),
],
);
}
}
- Search
class Search extends StatefulWidget {
Search({Key? key}) : super(key: key);
@override
_SearchState createState() => _SearchState();
}
class _SearchState extends State<Search> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("我是搜索页面"),
),
body: Text("内容区域"),
);
}
}
这样就可以实现tab效果,
image.png