今是昨非

今是昨非

日出江花红胜火,春来江水绿如蓝

Flutter Component - Tabbar

[Flutter Component - Tabbar]#

Usage#

To use Tabbar, set the style of indicator, its length, and the styles for selected and unselected tabs. Create the Tabbar based on an array.

The code is as follows:


import 'package:flutter/material.dart';

class TabbarDemo extends StatefulWidget {
  TabbarDemo({Key? key}) : super(key: key);

  _TabbarDemoState createState() => _TabbarDemoState();
}

class _TabbarDemoState extends State<TabbarDemo>
    with SingleTickerProviderStateMixin {
  var tabTextList = <String>[];
  var categoryTabs = <Tab>[
    Tab(
      text: "Home",
    ),
  ];

  var tempCategoryTabs = <Tab>[];

  @override
  void initState() {
    super.initState();
    tabTextList = ["Home", "New", "Hottest"];
    tabTextList.forEach((text) {
      tempCategoryTabs.add(
        Tab(
          text: text,
        ),
      );
    });
    setState(() {
      categoryTabs = tempCategoryTabs;
    });
  }

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

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      initialIndex: 0,
      length: categoryTabs.length,
      child: Scaffold(
        appBar: AppBar(title: Text("Tabbar")),
        body: Column(
          children: [
            TabBar(
              tabs: categoryTabs,
              isScrollable: true,
              labelStyle: TextStyle(
                fontSize: 24.0,
              ),
              labelColor: Colors.blueAccent,
              unselectedLabelStyle: TextStyle(fontSize: 16.0),
              unselectedLabelColor: Colors.grey,
              indicator: UnderlineTabIndicator(
                borderSide: BorderSide(color: Colors.orangeAccent, width: 3.0),
                insets: EdgeInsets.fromLTRB(20, 0, 20, 0),
              ),
            ),
            Expanded(
              child: TabBarView(
                children:
                    tabTextList.map((e) => Center(child: Text(e))).toList(),
              ),
            )
          ],
        ),
      ),
    );
  }
}

Reference#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.