Flutter标签页指示器样式定制插件flutter_tab_indicator_styler的使用

发布于 1周前 作者 gougou168 来自 Flutter

Flutter标签页指示器样式定制插件 flutter_tab_indicator_styler 的使用

flutter_tab_indicator_styler 是一个用于自定义Flutter中TabBar指示器样式的插件。它支持多种风格的指示器,并且高度可定制,适用于Android、iOS和Web平台。

特性 💚

  • 支持Android、iOS、Web
  • 可直接添加到默认的TabBar中
  • 提供三种不同的Tab指示器样式:DotIndicator、MaterialIndicator和RectangularIndicator
  • 高度可定制

演示

Demo

样式

DotIndicator

DotIndicator(
  radius: 3,
  color: Colors.black,
  distanceFromCenter: 16,
  paintingStyle: PaintingStyle.fill,
)

MaterialIndicator

MaterialIndicator(
  height: 5,
  tabPosition: TabPosition.bottom,
  topRightRadius: 8,
  topLeftRadius: 8,
  horizontalPadding: 50,
  color: Colors.green,
)

RectangularIndicator

RectangularIndicator(
  bottomLeftRadius: 100,
  bottomRightRadius: 100,
  topLeftRadius: 100,
  topRightRadius: 100,
  color: Colors.black,
  paintingStyle: PaintingStyle.stroke,
)

如何使用

下面是一个完整的示例代码,展示了如何在项目中使用flutter_tab_indicator_styler

import 'package:flutter/material.dart';
import 'package:flutter_tab_indicator_styler/flutter_tab_indicator_styler.dart';

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: DefaultTabController(
        length: 3,
        initialIndex: 0,
        child: Center(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 16),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text("Material Indicator: position bottom"),
                ),
                Material(
                  child: TabBar(
                    indicatorColor: Colors.green,
                    tabs: [
                      Tab(text: "Home"),
                      Tab(text: "Work"),
                      Tab(text: "Play"),
                    ],
                    labelColor: Colors.black,
                    indicator: MaterialIndicator(
                      height: 5,
                      topLeftRadius: 8,
                      topRightRadius: 8,
                      horizontalPadding: 50,
                      tabPosition: TabPosition.bottom,
                    ),
                  ),
                ),
                SizedBox(height: 16),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text("Dot Indicator"),
                ),
                Material(
                  child: TabBar(
                    indicatorColor: Colors.green,
                    tabs: [
                      Tab(text: "Home"),
                      Tab(text: "Work"),
                      Tab(text: "Play"),
                    ],
                    labelColor: Colors.black,
                    indicator: DotIndicator(
                      color: Colors.black,
                      distanceFromCenter: 16,
                      radius: 3,
                      paintingStyle: PaintingStyle.fill,
                    ),
                  ),
                ),
                SizedBox(height: 16),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text("Material Indicator: position top"),
                ),
                Material(
                  child: TabBar(
                    indicatorColor: Colors.green,
                    tabs: [
                      Tab(text: "Home"),
                      Tab(text: "Work"),
                      Tab(text: "Play"),
                    ],
                    labelColor: Colors.black,
                    indicatorSize: TabBarIndicatorSize.label,
                    indicator: MaterialIndicator(
                      height: 5,
                      topLeftRadius: 0,
                      topRightRadius: 0,
                      bottomLeftRadius: 5,
                      bottomRightRadius: 5,
                      tabPosition: TabPosition.top,
                    ),
                  ),
                ),
                SizedBox(height: 16),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text("RectangularIndicator Indicator: fill"),
                ),
                Material(
                  child: TabBar(
                    indicatorColor: Colors.green,
                    tabs: [
                      Tab(text: "Home"),
                      Tab(text: "Work"),
                      Tab(text: "Play"),
                    ],
                    labelColor: Colors.white,
                    unselectedLabelColor: Colors.black,
                    indicator: RectangularIndicator(
                      bottomLeftRadius: 100,
                      bottomRightRadius: 100,
                      topLeftRadius: 100,
                      topRightRadius: 100,
                    ),
                  ),
                ),
                SizedBox(height: 16),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text("RectangularIndicator Indicator: stroke"),
                ),
                TabBar(
                  indicatorColor: Colors.green,
                  tabs: [
                    Tab(text: "Home"),
                    Tab(text: "Work"),
                    Tab(text: "Play"),
                  ],
                  labelColor: Colors.black,
                  indicator: RectangularIndicator(
                    bottomLeftRadius: 100,
                    bottomRightRadius: 100,
                    topLeftRadius: 100,
                    topRightRadius: 100,
                    paintingStyle: PaintingStyle.stroke,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

更多关于Flutter标签页指示器样式定制插件flutter_tab_indicator_styler的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter标签页指示器样式定制插件flutter_tab_indicator_styler的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用flutter_tab_indicator_styler插件来自定义标签页指示器样式的示例代码。这个插件允许你高度自定义TabBar指示器的外观,比如颜色、形状、动画等。

首先,确保你的pubspec.yaml文件中已经添加了flutter_tab_indicator_styler依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_tab_indicator_styler: ^x.y.z  # 请替换为最新版本号

然后,运行flutter pub get来安装依赖。

接下来,是一个完整的示例代码,展示如何使用flutter_tab_indicator_styler来自定义TabBar的指示器样式:

import 'package:flutter/material.dart';
import 'package:flutter_tab_indicator_styler/flutter_tab_indicator_styler.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Tab Indicator Styler Example'),
        ),
        body: DefaultTabController(
          length: 3,
          child: Column(
            children: [
              TabBar(
                tabs: [
                  Tab(icon: Icon(Icons.directions_car)),
                  Tab(icon: Icon(Icons.directions_transit)),
                  Tab(icon: Icon(Icons.directions_bike)),
                ],
                labelColor: Colors.black,
                unselectedLabelColor: Colors.grey,
                indicator: TabIndicatorStyler(
                  indicatorColor: Colors.blue,
                  indicatorSize: TabIndicatorSize.tab,
                  indicatorShape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(25.0),
                  ),
                  indicatorPadding: EdgeInsets.all(8.0),
                  indicatorAnimationDuration: Duration(milliseconds: 300),
                ),
              ),
              Expanded(
                child: TabBarView(
                  children: [
                    Icon(Icons.directions_car),
                    Icon(Icons.directions_transit),
                    Icon(Icons.directions_bike),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

代码解释

  1. 依赖导入

    import 'package:flutter/material.dart';
    import 'package:flutter_tab_indicator_styler/flutter_tab_indicator_styler.dart';
    
  2. TabBar的自定义指示器

    TabBar(
      tabs: [
        Tab(icon: Icon(Icons.directions_car)),
        Tab(icon: Icon(Icons.directions_transit)),
        Tab(icon: Icon(Icons.directions_bike)),
      ],
      labelColor: Colors.black,
      unselectedLabelColor: Colors.grey,
      indicator: TabIndicatorStyler(
        indicatorColor: Colors.blue,
        indicatorSize: TabIndicatorSize.tab,
        indicatorShape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(25.0),
        ),
        indicatorPadding: EdgeInsets.all(8.0),
        indicatorAnimationDuration: Duration(milliseconds: 300),
      ),
    ),
    
    • indicatorColor:指示器的颜色。
    • indicatorSize:指示器的大小,可以是TabIndicatorSize.tab(与Tab宽度一致)或TabIndicatorSize.label(与Tab标签文本宽度一致)。
    • indicatorShape:指示器的形状,这里使用了圆角矩形。
    • indicatorPadding:指示器内部的填充。
    • indicatorAnimationDuration:指示器动画的持续时间。

通过这种方式,你可以灵活地定制TabBar指示器的样式,以满足不同的UI设计需求。

回到顶部