Flutter底部Tabbar组件使用

APP都少不了底部tabbar导航,Flutter也提供了对应组件,上代码:
TabsPage.dart:

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';

import 'package:seller/res/colors/colors.dart';
import 'package:seller/pages/tabs/user.dart';
import 'package:seller/pages/tabs/home.dart';
import 'package:seller/pages/tabs/message.dart';
import 'package:seller/pages/tabs/marketting.dart';
import 'package:seller/untils/tools.dart';

class TabsPage extends StatefulWidget {
  @override
  _TabsPageState createState() => _TabsPageState();
}

class _TabsPageState extends State<TabsPage> {
  int _currentPageIndex = 0;
  DateTime _lastPressedAt; //上次点击时间
  List<Widget> _pageList = [
    HomePage(),
    MessagePage(),
    MarkettingPage(),
    UserPage(),
  ];

  Widget _bottomNavigationBar() {
    return BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      currentIndex: this._currentPageIndex,
      unselectedItemColor: ThemeColors.medium,
      selectedItemColor: ThemeColors.primary,
      items: _bottomNavigationBarItemList(),
      onTap: (int index) {
        setState(() {
          this._currentPageIndex = index;
        });
      },
    );
  }

  List<BottomNavigationBarItem> _bottomNavigationBarItemList() {
    return <BottomNavigationBarItem>[
      _bottomNavigationBarItem(
          icon: _currentPageIndex == 0
              ? "lib/res/assets/images/tabs/home-selected.svg"
              : "lib/res/assets/images/tabs/home.svg",
          title: '工作台'),
      _bottomNavigationBarItem(
          icon: _currentPageIndex == 1
              ? "lib/res/assets/images/tabs/info-selected.svg"
              : "lib/res/assets/images/tabs/info.svg",
          title: '消息'),
      _bottomNavigationBarItem(
          icon: _currentPageIndex == 2
              ? "lib/res/assets/images/tabs/marketing-selected.svg"
              : "lib/res/assets/images/tabs/marketing.svg",
          title: '营销'),
      _bottomNavigationBarItem(
          icon: _currentPageIndex == 3
              ? "lib/res/assets/images/tabs/user-selected.svg"
              : "lib/res/assets/images/tabs/user.svg",
          title: '我的'),
    ];
  }

  BottomNavigationBarItem _bottomNavigationBarItem(
      {String icon, String title}) {
    return BottomNavigationBarItem(
      icon: SvgPicture.asset(icon, width: 25, height: 25),
      title: Text(
        title,
        style: TextStyle(fontSize: ScreenUtil().setSp(12)),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: Scaffold(
        body: IndexedStack(
            children: this._pageList, 
            index: this._currentPageIndex,
        ),
        bottomNavigationBar: _bottomNavigationBar(),
      ),
      onWillPop: () async {
        if (_lastPressedAt == null ||
            DateTime.now().difference(_lastPressedAt) > Duration(seconds: 1)) {
          //两次点击间隔超过1秒则重新计时
          _lastPressedAt = DateTime.now();
          Tools().toast('再按一次退出');
          return false;
        }
        return true;
      },
    );
  }
}

在main.dart中引入使用:

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/cupertino.dart';
import 'package:seller/routes/routes.dart';
import 'package:seller/pages/tabs/tabs.dart';

import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

void main() {
  // 沉浸式
  if (Platform.isAndroid) {
    SystemUiOverlayStyle systemUiOverlayStyle =
        SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return FlutterEasyLoading(
      child: MaterialApp(
        title: 'APP',
        theme: ThemeData(
          primarySwatch: Colors.blue,
          platform:
              Platform.isAndroid ? TargetPlatform.android : TargetPlatform.iOS,
          textTheme: TextTheme(
            subhead: TextStyle(textBaseline: TextBaseline.alphabetic),
          ),
        ),
        home: MyHomePage(),
        onGenerateRoute: (RouteSettings settings) {
          // 路由表对应单页
          Widget _page = Router.routerStore[settings.name];
          print(settings.arguments);
          // 埋点等操作

          // 自定义路由动画
          return CupertinoPageRoute(
              settings: settings, builder: (context) => _page);
        },
        localizationsDelegates: [
          //此处 系统是什么语言就显示什么语言
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
        ],
        supportedLocales: [
          //此处 系统是什么语言就显示什么语言
          const Locale('zh', 'CH'),
          const Locale('en', 'US'),
        ],
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 375, height: 667, allowFontScaling: false);
    return TabsPage();
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

废柴前端

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

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

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

打赏作者

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

抵扣说明:

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

余额充值