import 'package:flutter/material.dart';
void main() =>runApp(
new MyApp()
);
class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
title: '选项卡测试',
home: MyHomePage()
);
}
}
class MyHomePage extends StatefulWidget {
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
// 定义默认首选项
int _selectIndex = 1;
// 将来这里面是四个页面
final _widgetOptions =[
Text('首页'),
Text('关注'),
Text('搜索'),
Text('我的')
];
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('多写两边选项卡'),
),
// body
body: Center(
child: _widgetOptions.elementAt(_selectIndex),
),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(icon: Icon(Icons.cake),title: Text("首页")),
BottomNavigationBarItem(icon: Icon(Icons.add_circle_outline),title: Text("关注")),
BottomNavigationBarItem(icon: Icon(Icons.add_shopping_cart),title: Text("搜索")),
BottomNavigationBarItem(icon: Icon(Icons.adjust),title: Text("我的"))
],
currentIndex: _selectIndex,
fixedColor:Colors.deepPurple,
onTap: _onItemTapped,
),
);
}
void _onItemTapped(int index){
setState(() {
_selectIndex =index;
});
}
}