【译】 Flutter 延迟加载组件 【包体积优化 _ 动态化】
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<Widget> _widgetOptions = <Widget>[
Text('选项 1'),
Text('选项 2'),
Text('选项 3'),
];
int _selectedIndex = 0;
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('主页')),
BottomNavigationBarItem(icon: Icon(Icons.business), title: Text('业务')),
BottomNavigationBarItem(icon: Icon(Icons.school), title: Text('学校')),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
}
这段代码示例展示了如何在Flutter中实现底部导航栏,并根据用户的选择动态更新页面内容。这是一个简单的应用程序,但它演示了Flutter应用程序的基本结构,并且对于初学者来说是一个很好的起点。
评论已关闭