![](https://img.haomeiwen.com/i2988670/47229f4c65d7eae4.png)
入口类项目代码
import 'package:flutter/material.dart';
import './pages/index_page.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return Container(
child: MaterialApp(
title: '百姓生活',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primaryColor: Colors.pink
),
home: IndexPage(),
),
);
}
}
index_page代码
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'home_page.dart';
import 'cart_page.dart';
import 'category_page.dart';
import 'member_page.dart';
class IndexPage extends StatefulWidget{
_IndexPageState createState() => _IndexPageState();
}
class _IndexPageState extends State<IndexPage>{
//声明一个List变量 定义底部导航的文字和图标
final List<BottomNavigationBarItem> bottomTabs =[
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.home),
title: Text('首页')
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.search),
title: Text('分类')
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.shopping_cart),
title: Text('购物车')
),
BottomNavigationBarItem(
icon: Icon(CupertinoIcons.profile_circled),
title: Text('会员中心')
),
];
final List tabBodies = [
HomePage(),
CategoryPage(),
CartPage(),
MemberPage()
];
//currentIndex tabBodies的List索引,改变索引就相当于改变了页面
//利用currentIndex得到当前选择的页面,并进行呈现出来
int currentIndex = 0;
var currentPage;
@override
void initState(){
currentPage = tabBodies[currentIndex];
super.initState();
}
@override
Widget build(BuildContext context){
return Scaffold(
backgroundColor: Color.fromRGBO(244, 245, 245, 1.0),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: currentIndex,
items: bottomTabs,
onTap: (index){
setState(() {
currentIndex = index;
currentPage = tabBodies[currentIndex];
});
},
),
body: currentPage,
);
}
}
item页以home为类
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body:Center(
child: Text('商城首页'),
)
);
}
}
最终效果
![](https://img.haomeiwen.com/i2988670/cac066e0d5c13b3e.png)
网友评论