android+flutter实现tabView主框架展示

用flutter实现首页面tab和对应的tab页面的展示:

home

 

一、主页面的main.dart代码:

import 'package:flutter/material.dart';
import './DrawerWidget.dart';
import './modal/main_bean.dart';

class MainPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,  // 页面不展示debug标签,设置这一属性即可
      title: 'MainPageHome',
      theme: ThemeData(
        brightness: Brightness.light,
        primaryColor: const Color(0xFF0a3470),
        accentColor: const Color(0xFF0a3470),
      ),
      home: MainWidget(),
    );
  }
}

//页面加载器,作为主页面
class MainWidget extends StatefulWidget {
  //生命周期函数
  @override
  MainPageState  createState() => MainPageState();
}

//主页面(就是类似于activity)   https://blog.csdn.net/yuezheyue123/article/details/83588040
class MainPageState extends State<MainWidget> with SingleTickerProviderStateMixin{
  TabController _tabController; //需要定义一个Controller

  @override
  void initState() {
    super.initState();
    // 创建Controller
    _tabController = new TabController(vsync: this, length: choices.length,initialIndex:0);
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        //导航栏
        appBar: new MainAppBar(),
        //抽屉
        drawer: new DrawerWidget(),

        bottomNavigationBar: BottomAppBar(
            color: Colors.white,
            shape: CircularNotchedRectangle(), // 底部导航栏打一个圆形的洞
            child: Container(
              height: 60,
              child:new TabBar(
                isScrollable: false,
                  labelColor: const Color(0xFF0a3470),//选中的tab颜色
                  indicatorWeight: 0.1,//indicator的权重,也就是所占高度
                  indicatorColor: Colors.white,//indicator的颜色默认设置成背景色
                  unselectedLabelColor: const Color(0xFF999999),//未选中的tab颜色
                  tabs: <Widget>[
                    new Tab(icon: new Icon(choices[0].icon),text: choices[0].title,),
                    new Tab(icon: new Icon(choices[1].icon),text: choices[1].title,),
                    //new Tab(icon: new Icon(choices[2].icon,size: 45.0,),),
                    new Tab(icon:new Icon(choices[2].icon,size: 20.0,) ,text:choices[2].title,),
                    new Tab(icon: new Icon(choices[3].icon),text: choices[3].title,),
                    new Tab(icon: new Icon(choices[4].icon),text: choices[4].title,),
                  ],
                controller: _tabController,
              ) ,
            )
        ),

        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        floatingActionButton: FloatingActionButton( //悬浮按钮
            backgroundColor: const Color(0x00000000),
            foregroundColor: const Color(0xffffffff),
            elevation: 0,
            highlightElevation: 0,
            child: Image(
              image: AssetImage("images/center_tab_icon.png"),
              width: 60,
              height: 60,
              alignment: Alignment.topCenter,
            ),
            onPressed:(){
                _tabController.animateTo(2);
            }
        ),

        //主要内容
        body: TabBarView(
            physics: new NeverScrollableScrollPhysics(),
            controller: _tabController,
            children: choices.map<Widget>((Choice choice) {
                return Container(
                    child: choice.widgetPage,
                );
            }).toList(),
        )
    );
  }
}




//导航栏
class MainAppBar extends AppBar{

  //标题
  @override
  Widget get title => Text('主页面');
  @override
  bool get centerTitle => true;

  //右侧图标
  @override
  List<Widget> get actions => <Widget>[
    IconButton(icon: Icon(Icons.share), onPressed: () {}),
  ];

  //左侧图标
  @override
  Widget get leading => Builder(builder: (context) {
    return IconButton(
      icon: Icon(Icons.dashboard, color: Colors.white), //自定义图标
      onPressed: () {
        // 打开抽屉菜单
        Scaffold.of(context).openDrawer();
      },
    );
  });
}

二、使用到的bean类:

import 'package:flutter/material.dart';
import '../TabWidget/MessagePage.dart';
import '../TabWidget/StaticsPage.dart';
import '../TabWidget/QuickMenuPage.dart';
import '../TabWidget/WorkPage.dart';
import '../TabWidget/MinePage.dart';
class Choice {
  const Choice({ this.title, this.icon ,this.widgetPage});
  final String title;
  final IconData icon;
  final StatelessWidget widgetPage;
}

const List<Choice> choices = const <Choice>[
  const Choice(title: '消息', icon: Icons.directions_car,widgetPage: MessagePage(tabInfo: '消息',)),
  const Choice(title: '统计', icon: Icons.directions_bike,widgetPage: StaticsPage(tabInfo: '统计',)),
  const Choice(title: '快捷', icon: Icons.work,widgetPage: QuickMenuPage(tabInfo: '快捷',)),
  const Choice(title: '工作', icon: Icons.directions_boat,widgetPage: WorkPage(tabInfo: '工作',)),
  const Choice(title: '我的', icon: Icons.directions_bus,widgetPage: MinePage(tabInfo: '我的',)),
];

 

demo源码https://download.csdn.net/download/u010326875/11102116

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漠天515

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值