Flutter之tabBar的实现

2021-05-21  本文已影响0人  稀释1
import 'package:flutter/material.dart';
import './FirstScreen.dart';
import './SecondScreen.dart';

class RootPage extends StatefulWidget {
  @override
  _RootPageState createState() => new _RootPageState();
}

class _RootPageState extends State<RootPage> {
  List<Widget> pages = [
    FirstScreen(),
    SecondScreen(),
  ];

  int _currentIndex = 1;
  @override
  Widget build(BuildContext context) {
    return new Container(
      child: Scaffold(
        bottomNavigationBar: new BottomNavigationBar(
            onTap: (int index) {
              setState(() {
                _currentIndex = index;
              });
            },
            selectedFontSize: 12.0,
            type: BottomNavigationBarType.fixed,
            fixedColor: Colors.green,
            currentIndex: _currentIndex,
            items: <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                  icon: new Image(
                      height: 20,
                      width: 20,
                      image: new AssetImage('assets/tabs/home.png')),
                  activeIcon: new Image(
                      height: 20,
                      width: 20,
                      image: new AssetImage('assets/tabs/home_selected.png')),
                  // ignore: deprecated_member_use
                  title: new Text('订单明细')),
              BottomNavigationBarItem(
                  icon: new Image(
                      height: 20,
                      width: 20,
                      image: new AssetImage('assets/tabs/user.png')),
                  activeIcon: new Image(
                      height: 20,
                      width: 20,
                      image: new AssetImage('assets/tabs/user_selected.png')),
                  // ignore: deprecated_member_use
                  title: new Text('我的商铺'))
            ]),
        body: new Center(
          child: pages[_currentIndex],
        ),
      ),
    );
  }
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: RootPage(),
    );
  }
}
// SecondScreen.dart

import 'package:flutter/material.dart';

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Text('Tab 2 Layout'),
      ),
    );
  }
}

这里介绍几个关键的组件

1、Scaffold(脚手架组件)

Scaffold实现了基本的Material Design布局。只要是在Material Design中定义过的单个接口显示的布局组件元素,都可以使用Scaffold来绘制。

Scaffold常见的属性如下表:

属性名 类型 说明
APPBar AppBar 显示在接口顶部的一个AppBar
body Widget 当前接口所显示的主要内容
floatingActionButton Widget 在Material Design中定义的一个功能按钮
persistentFooterButtons List<Widget> 固定在下方显示的按钮
drawer Widget 侧边栏组件
bottomNavgationBar Widget 显示在底部的导航栏按钮栏
backgroundColor Color 背景颜色
resizeToAvoidBottomPadding bool 控制接口内容body是否重新布局来避免底部被覆盖,比如键盘显示时

2、BottomNavgationBar(底部导航条组件)

BottomNavgationBar是底部导航条,类似于iOS中的TabbarController,可以很容易的再tab之间切换,大部分APP都采用这种布局方式。
BottomNavgationBar的常见属性如下表:

属性名 类型 说明
currentIndex int 当前索引,用来切换按钮控制
fixedColor Color 选中按钮的颜色。如果没有指定值,则用系统主题色
iconSize double 按钮图标大小
items List<BottomNavgationBarItem> 底部导航条按钮集,每一项是一个BottomNavgationBarItem,有icon图标及title文本属性
onTap ValueChanged<int> 按下其中某个按钮的回调事件。需要根据返回的索引设置当前的索引
上一篇 下一篇

猜你喜欢

热点阅读