1. main.dart入口
// 自带样式类库
import 'package:flutter/material.dart';
// 入口函数
void main() => runApp(MyApp());
// 入口widget
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
/*
* Material应用程序以MaterialApp widget开始
* 该widget在应用程序的根部创建了一些有用的widget,
* 比如一个Theme配置了应用的主题,home配置初始页面
* */
return new MaterialApp(
title:"首页",
home:Home()
);
}
}
// 初始页面类
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: Text("首页"),
);
}
}
欢迎加群讨论更多flutter相关问题(7天有效)如果失效,可加个人微信拉群
2. 底部导航栏切换及标题切换
BottomNavigationBar:
items:底部导航栏的显示项,
onTap:点击导航栏子项时的回调,
currentIndex:当前显示项的下标(默认从0开始),
type:底部导航栏的类型,有fixed和shifting两个类型,显示效果不一样,
fixedColor:底部导航栏type为fixed时导航栏的颜色,如果为空的话默认使用ThemeData.primaryColor,
iconSize:BottomNavigationBarItem icon的大小
// 自带样式类库
import 'package:flutter/material.dart';
// 入口函数
void main() => runApp(MyApp());
// 入口widget
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
/*
* Material应用程序以MaterialApp widget开始
* 该widget在应用程序的根部创建了一些有用的widget,
* 比如一个Theme配置了应用的主题,home配置初始页面
* */
return new MaterialApp(
title:"首页",
home:Application()
);
}
}
// 应用程序入口
class Application extends StatefulWidget {
@override
_ApplicationState createState() => _ApplicationState();
}
class _ApplicationState extends State<Application> {
// 当前底部导航显示的索引页面
int _currentIndex = 0;
// 页面内容数组
List <Widget> _bodyList = [
HomePage(),
SettingPage()
];
// 底部显示的项
List <BottomNavigationBarItem> _bottomTabBarList = [
BottomNavigationBarItem(
icon:Icon(Icons.home),
title: Text("首页")
),
BottomNavigationBarItem(
icon:Icon(Icons.settings),
title: Text("设置")
),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// 当前页面标题
title:this._bottomTabBarList[this._currentIndex].title
),
body:this._bodyList[this._currentIndex],
bottomNavigationBar: BottomNavigationBar(
items: this._bottomTabBarList,
currentIndex: this._currentIndex,
// 点击事件
onTap: (int index)=>{
setState(()=>{
this._currentIndex = index
})
},
),
);
}
}
// 首页
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Container(
child:Center(
child:Text("首页")
)
);
}
}
// 设置页面
class SettingPage extends StatefulWidget {
@override
_SettingPageState createState() => _SettingPageState();
}
class _SettingPageState extends State<SettingPage> {
@override
Widget build(BuildContext context) {
return Container(
child:Center(
child:Text("设置页面")
)
);
}
}
2.1 底部导航栏使用图片Icon模式
先在pubspec.yaml配置静态资源文件:
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
assets:
- assets/images/ic_tab_home_normal.png
- assets/images/ic_tab_subject_normal.png
- assets/images/ic_tab_profile_normal.png
import 'package:flutter/material.dart';
// 入口函数
void main() => runApp(MyApp());
// 入口widget
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
/*
* Material应用程序以MaterialApp widget开始
* 该widget在应用程序的根部创建了一些有用的widget,
* 比如一个Theme配置了应用的主题,home配置初始页面
* */
return new MaterialApp(
title:"应用程序标题",
home:Application()
);
}
}
class Application extends StatefulWidget {
@override
_ApplicationState createState() => _ApplicationState();
}
class _ApplicationState extends State<Application> {
int _currentIndex = 0;
List<Widget> _bodyList = [
HomePage(),
MediaPage(),
MinePage()
];
List<BottomNavigationBarItem> _bottomNavBarItemList = [
BottomNavigationBarItem(
icon:Image.asset(
'assets/images/ic_tab_home_normal.png',
width:30.0,
height:30.0
),
title:Text(
"首页",
style:TextStyle(
fontSize: 10.0
)
),
activeIcon: Image.asset(
'assets/images/ic_tab_home_active.png',
width:30.0,
height:30.0
),
),
BottomNavigationBarItem(
icon:Image.asset(
'assets/images/ic_tab_subject_normal.png',
width:30.0,
height:30.0
),
title:Text(
"媒体",
style:TextStyle(
fontSize: 10.0
)
),
activeIcon: Image.asset(
'assets/images/ic_tab_subject_active.png',
width:30.0,
height:30.0
),
),
BottomNavigationBarItem(
icon:Image.asset(
'assets/images/ic_tab_profile_normal.png',
width:30.0,
height:30.0
),
title:Text(
"我的",
style:TextStyle(
fontSize: 10.0
)
),
activeIcon: Image.asset(
'assets/images/ic_tab_profile_active.png',
width:30.0,
height:30.0
),
),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title:this._bottomNavBarItemList[this._currentIndex].title
),
body:this._bodyList[this._currentIndex],
bottomNavigationBar: BottomNavigationBar(
items: this._bottomNavBarItemList,
currentIndex: this._currentIndex,
onTap:(int index){
setState((){
this._currentIndex = index;
});
},
type:BottomNavigationBarType.fixed,
fixedColor:Colors.green
),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Container(
child:Center(child: Text("首页"),)
);
}
}
// 媒体页面
class MediaPage extends StatefulWidget {
@override
_MediaPageState createState() => _MediaPageState();
}
class _MediaPageState extends State<MediaPage> {
@override
Widget build(BuildContext context) {
return Container(
child:Center(child: Text("媒体页面"),)
);
}
}
// 我的页面
class MinePage extends StatefulWidget {
@override
_MinePageState createState() => _MinePageState();
}
class _MinePageState extends State<MinePage> {
@override
Widget build(BuildContext context) {
return Container(
child:Center(child: Text("我的页面"),)
);
}
}
3. 普通路由跳转及带参数跳转
// 自带样式类库
import 'package:flutter/material.dart';
// 入口函数
void main() => runApp(MyApp());
// 入口widget
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
/*
* Material应用程序以MaterialApp widget开始
* 该widget在应用程序的根部创建了一些有用的widget,
* 比如一个Theme配置了应用的主题,home配置初始页面
* */
return new MaterialApp(
title:"首页",
home:HomePage()
);
}
}
// 首页
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title:Text("首页")
),
body:Center(
child:RaisedButton(
child:Text("跳转到详情页1"),
onPressed: ()=>{
Navigator.of(context).push(
MaterialPageRoute(
// 当做参数传递
builder: (context)=> DetailPage(id:1)
)
)
},
)
)
);
}
}
class DetailPage extends StatefulWidget {
int id;
DetailPage({this.id=0});
@override
_DetailPageState createState() => _DetailPageState(id);
}
class _DetailPageState extends State<DetailPage> {
int id;
_DetailPageState(this.id);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title:Text("详情页")
),
body: Center(
child: Text("当前页面id=${id}"),
),
);
}
}
4. 命名路由跳转及传值
// 自带样式类库
import 'package:flutter/material.dart';
// 入口函数
void main() => runApp(MyApp());
// 入口widget
class MyApp extends StatelessWidget {
final routes = {
"home":(context)=>HomePage(),
"detail":(context,{arguments})=>DetailPage(arguments:arguments),
};
@override
Widget build(BuildContext context) {
/*
* Material应用程序以MaterialApp widget开始
* 该widget在应用程序的根部创建了一些有用的widget,
* 比如一个Theme配置了应用的主题,home配置初始页面
* */
return new MaterialApp(
initialRoute: 'home',
onGenerateRoute: (RouteSettings settings){
// 统一处理
final String routeName = settings.name;
final Function pageContainerBuilder = this.routes[routeName];
print(routeName);
if(pageContainerBuilder != null){
if(settings.arguments != null){
final route = MaterialPageRoute(
builder:(context)=>pageContainerBuilder(context,arguments:settings.arguments)
);
return route;
}else{
final route = MaterialPageRoute(
builder:(context)=>pageContainerBuilder(context)
);
return route;
}
}
},
);
}
}
// 首页
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title:Text("首页")
),
body:Center(
child:RaisedButton(
child:Text("跳转到详情页1"),
onPressed: (){
Navigator.pushNamed(context,"detail", arguments: {"id":1});
},
)
)
);
}
}
// 详情页面
class DetailPage extends StatefulWidget {
final arguments;
DetailPage({this.arguments});
@override
_DetailPageState createState() => _DetailPageState(arguments:arguments);
}
class _DetailPageState extends State<DetailPage> {
final arguments;
_DetailPageState({this.arguments});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title:Text("详情页")
),
body: Center(
child: Text("当前页面id=${arguments != null ? arguments['id'] : 0}"),
),
);
}
}