• flutter 导航栏 BottomNavigationBar底部导航栏相当于iOS tabbar


    //main.dart文件
    import 'package:flutter/material.dart';
    import 'package:myapp/Tabbar.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      const MyApp({Key key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Tabbar(),
        );
      }
    }
    //Tabbar.dart文件
    import 'package:flutter/material.dart';
    import './Home.dart';
    import './Category.dart';
    import './Setting.dart';
    
    class Tabbar extends StatefulWidget {
      Tabbar({Key key}) : super(key: key);
    
      _TabbarState createState() => _TabbarState();
    }
    
    class _TabbarState extends State<Tabbar> {
      int _tabbarIndex = 0;//用于记录当前选中第几个item
      List _tabPages = [//装载底部tabbar的item所对应的页面
        HomePage(),
        CategoryPage(),
        SettingPage()
      ];
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("appbarTitle"),
          ),
          body: this._tabPages[this._tabbarIndex],//显示当前选中的tabbatItem所对应的页面
          bottomNavigationBar: BottomNavigationBar(
            iconSize: 36,
            fixedColor: Colors.pink,
            type: BottomNavigationBarType.fixed,
            currentIndex: this._tabbarIndex,//当前选中第几个item
            onTap: (index){//点击回调
              setState(() {//当需要改变页面中的参数值的时候,需要在该方法中更新数据,否则不会刷新页面
                this._tabbarIndex = index;
              });
            },
            items: [//配置底部导航栏的按钮列表
              BottomNavigationBarItem(icon: Icon(Icons.home), title: Text("首页")),
              BottomNavigationBarItem(
                  icon: Icon(Icons.category), title: Text("分类")),
              BottomNavigationBarItem(
                  icon: Icon(Icons.settings), title: Text("设置")),
            ],
          ),
        );
      }
    }
    //首页  Home.dart
    import 'package:flutter/material.dart';
    
    class HomePage extends StatelessWidget {
      const HomePage({Key key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Container(
          child: Text('首页'),
        );
      }
    }
    //分类 Category.dart
    import 'package:flutter/material.dart';
    
    class CategoryPage extends StatelessWidget {
      const CategoryPage({Key key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Container(
            child: Text('分类')
        );
      }
    }
    //设置 Setting.dart
    import 'package:flutter/material.dart';
    
    class CategoryPage extends StatelessWidget {
      const CategoryPage({Key key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Container(
            child: Text('分类')
        );
      }
    }

    还有参考同行大神的

    https://blog.csdn.net/qq_16247851/article/details/81172628

  • 相关阅读:
    C# 托管内存与非托管内存之间的转换
    A*算法详解链接
    【转】使用minizip解压缩多个文件(基于zlib)
    lua中table的遍历,以及删除
    clientHeight scrollHeight offsetHeight
    消息中间件(转)
    js 原型链和继承(转)
    session 和 cookie (转)
    java servlet
    redis 命令
  • 原文地址:https://www.cnblogs.com/gaozhang12345/p/11957701.html
Copyright © 2020-2023  润新知