我们在主文件为 main.dart 文件,里面有一个人入口文件 runApp

    一般我们写页面的时候需要分模块来写。

    页面入口,我首先拿底部一个【导航条】导航条切换。

    main.dart 文件

    1. import 'package:flutter/material.dart';
    2. import 'package:flutter_app/app/app_scence.dart';
    3. void main() {
    4. runApp(AppScene());
    5. }

    app_scence.dart 文件

    1. import 'package:flutter/material.dart';
    2. import 'package:flutter_app/app/root_scence.dart';
    3. import 'package:provider/provider.dart';
    4. import 'app_color.dart';
    5. class AppScene extends StatelessWidget {
    6. Widget build(BuildContext context){
    7. print("根本构建:$context");
    8. // 使用状态管理
    9. return MultiProvider(
    10. providers : [
    11. // 这里应该是通知吧。
    12. ChangeNotifierProvider(create: (value){
    13. print("根部执行状态管理!");
    14. } )
    15. ],
    16. child: MaterialApp(
    17. title : "AYuWei",
    18. debugShowCheckedModeBanner:false, // 测试的时候不显示debug图标。
    19. theme: ThemeData(
    20. primaryColor : Color(0xff437DFA),
    21. dividerColor : Color(0xffEEEEEE),
    22. scaffoldBackgroundColor : AppColor.paper,
    23. textTheme: TextTheme(
    24. bodyText1 : TextStyle( color : AppColor.darkGrey )
    25. )
    26. ),
    27. home: RootScene(),
    28. )
    29. );
    30. }
    31. }
    32. /**
    33. * 状态管理都需要使用到 provider包。
    34. */

    app根部构建RootScene, 文件路径root_scence.dart

    1. import 'package:flutter/cupertino.dart';
    2. import 'package:flutter/material.dart';
    3. import 'package:flutter_app/pages/exchange/exchange_scene.dart';
    4. import 'package:flutter_app/pages/home/home.dart';
    5. import 'package:flutter_app/pages/my/my_scene.dart';
    6. import 'package:flutter_app/pages/profit/profit_scene.dart';
    7. import 'package:flutter_app/pages/promotion/promotion_scene.dart';
    8. class RootScene extends StatefulWidget {
    9. @override
    10. _RootScene createState() => _RootScene();
    11. }
    12. class _RootScene extends State<RootScene>{
    13. int _tabIndex = 0;
    14. int _currentIndex = 0; // 当前底部导航条的索引下表。
    15. // 存储切换页面
    16. final List<Widget> _children = [HomeScene(), ExchangeScene(), ProfitScene(), PromotionScene(), MyScence() ];
    17. Widget build(BuildContext context){
    18. // 存储页面底部导航条信息
    19. final List<BottomNavigationBarItem> _list = <BottomNavigationBarItem>[
    20. BottomNavigationBarItem(
    21. icon: Icon(Icons.home),
    22. title: Text("首页")
    23. ),
    24. BottomNavigationBarItem(
    25. icon: Icon(Icons.attach_file),
    26. title: Text("委托购买")
    27. ),
    28. BottomNavigationBarItem(
    29. icon: Icon(Icons.attach_money),
    30. title: Text("收益")
    31. ),
    32. BottomNavigationBarItem(
    33. icon: Icon(Icons.people),
    34. title: Text("推广")
    35. ),
    36. BottomNavigationBarItem(
    37. icon: Icon(Icons.tag_faces),
    38. title: Text("我的")
    39. ),
    40. ];
    41. return Scaffold(
    42. body: Container(
    43. child : _children[_tabIndex],
    44. ),
    45. // 底部导航条。
    46. bottomNavigationBar: BottomNavigationBar(
    47. items: _list,
    48. type : BottomNavigationBarType.fixed, // fixed 将导航条固定在底部。
    49. onTap: onTapTapped, // 当点击的时候获取下标
    50. currentIndex: _currentIndex,
    51. // backgroundColor: Colors.grey[400] // 设置背景色
    52. // 还有详情请查看源码。
    53. ),
    54. );
    55. }
    56. void onTapTapped(index){
    57. print(index);
    58. setState(() {
    59. _currentIndex = index;
    60. _tabIndex = index;
    61. });
    62. }
    63. }

    my_scene.dart 文件

    1. import 'package:flutter/cupertino.dart';
    2. import 'package:flutter/material.dart';
    3. class MyScence extends StatefulWidget {
    4. @override
    5. _MyScence createState() => _MyScence();
    6. }
    7. class _MyScence extends State<MyScence>{
    8. Widget build(BuildContext context){
    9. return Scaffold(
    10. appBar: AppBar(
    11. title: Text("我的"),
    12. centerTitle: true, // 标题是否居中
    13. backgroundColor: Colors.green[400], // 头部背景色。
    14. ),
    15. body: ListView(
    16. children : <Widget>[
    17. Text("我的页面", textAlign: TextAlign.center ),
    18. ]
    19. ),
    20. );
    21. }
    22. }

    一共有五个页面,这个是我的页面,其余的结构类似。

    写了那么多还是拿图说话吧。
    WechatIMG32.png