flutter之TabBar

作者: 寒云暮雨 | 来源:发表于2019-10-18 18:21 被阅读0次

    一款完美的App怎么少得了TabBar


    image.png
    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(title: 'flutter的底部导航'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage>
        with SingleTickerProviderStateMixin {
      int _tabIndex = 0;
      TabController _controller;
      List<Tab> _tabs = <Tab>[
        Tab(
          text: '最新动态',
        ),
        Tab(
          text: '成员列表',
        ),
        Tab(
          text: '社团活动',
        ),
      ];
    
      @override
      void initState() {
        super.initState();
    
        //initialIndex初始选中第几个
        _controller = TabController(
          initialIndex: 2,
          length: _tabs.length,
          vsync: this,
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
            centerTitle: true,
            bottom: TabBar(
              isScrollable: true,
              tabs: _tabs,
              controller: _controller,
            ),
          ),
          body: TabBarView(
            controller: _controller,
            children: _tabs
                .map(
                  (Tab tab) => Container(
                    child: Card(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadiusDirectional.circular(20),
                      ),
                      clipBehavior: Clip.antiAlias,
                      child: Image.asset(
                        "images/big.jpg",
                        width: double.maxFinite,
                      ),
                    ),
                  ),
                )
                .toList(),
          ),
          bottomNavigationBar: BottomNavigationBar(
            items: <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.home),
                title: Text('home'),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.school),
                title: Text('school'),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.person),
                title: Text('person'),
              ),
            ],
            type: BottomNavigationBarType.fixed,
            currentIndex: _tabIndex,
            onTap: (index) {
              setState(() {
                _tabIndex = index;
              });
            },
          ),
        );
      }
    }
    
    

    定制TabBar

    /**
        const TabBar({
        Key key,
        @required this.tabs,//显示的标签内容,一般使用Tab对象,也可以是其他的Widget
        this.controller,//TabController对象
        this.isScrollable = false,//是否可滚动
        this.indicatorColor,//指示器颜色
        this.indicatorWeight = 2.0,//指示器高度
        this.indicatorPadding = EdgeInsets.zero,//底部指示器的Padding
        this.indicator,//指示器decoration,例如边框等
        this.indicatorSize,//指示器大小计算方式,TabBarIndicatorSize.label跟文字等宽,TabBarIndicatorSize.tab跟每个tab等宽
        this.labelColor,//选中label颜色
        this.labelStyle,//选中label的Style
        this.labelPadding,//每个label的padding值
        this.unselectedLabelColor,//未选中label颜色
        this.unselectedLabelStyle,//未选中label的Style
        }) : assert(tabs != null),
        assert(isScrollable != null),
        assert(indicator != null || (indicatorWeight != null && indicatorWeight > 0.0)),
        assert(indicator != null || (indicatorPadding != null)),
        super(key: key);
     */
    
    

    相关文章

      网友评论

        本文标题:flutter之TabBar

        本文链接:https://www.haomeiwen.com/subject/prdwmctx.html