Flutter初学者之普通底部导航栏及自定义不规则底部导航栏的实现
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: BottomNavigationBarPage(),
);
}
}
class BottomNavigationBarPage extends StatefulWidget {
@override
_BottomNavigationBarPageState createState() => _BottomNavigationBarPageState();
}
class _BottomNavigationBarPageState extends State<BottomNavigationBarPage> {
int _currentIndex = 0;
static const TextStyle optionStyle =
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static const List<Widget> _widgetOptions = <Widget>[
Text(
'主页',
style: optionStyle,
),
Text(
'消息',
style: optionStyle,
),
Text(
'我的',
style: optionStyle,
),
];
void _onItemTapped(int index) {
setState(() {
_currentIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: _widgetOptions.elementAt(_currentIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('主页'),
),
BottomNavigationBarItem(
icon: Icon(Icons.message),
title: Text('消息'),
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
title: Text('我的'),
),
],
currentIndex: _currentIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
}
这段代码实现了一个带有普通底部导航栏的Flutter应用,其中包含三个页面,分别是主页、消息和我的。用户可以点击底部导航栏的不同项来切换不同的页面。这是学习Flutter的一个很好的起点。
评论已关闭