Flutter Tab指示器样式定制插件tab_indicator_styler的使用

Flutter Tab指示器样式定制插件tab_indicator_styler的使用

插件介绍

tab_indicator_styler 是一个用于自定义Flutter应用程序中TabBar指示器样式的插件。它提供了三种不同风格的TabIndicator,并且可以在默认的TabBar上直接添加,支持Android、iOS和Web平台。

功能特性

  • 支持Android, iOS, Web
  • 可直接添加到默认的TabBar中
  • 提供了3种不同样式的TabIndicator
  • 高度可定制化

样式展示

DotIndicator

DotIndicator是一种简单的圆点指示器,可以通过设置其半径、颜色、距离中心的距离等属性来自定义样式。

/// 圆点的半径
final double radius;

/// 圆点的颜色
final Color color;

/// 距离中心的距离,正值表示位于Tab中心下方,负值则在上方
final double distanceFromCenter;

/// 绘制风格,填充或描边
final PaintingStyle paintingStyle;

/// 当paintingStyle为描边时使用的宽度
final double strokeWidth;

MaterialIndicator

MaterialIndicator模仿了Material Design风格的指示器,可以调整高度、位置(顶部或底部)、各个角的圆角半径等属性。

/// 指示器的高度,默认为4
final double height;

/// 指示器的位置,默认在底部
final TabPosition tabPosition;

/// 各个角的圆角半径
final double topRightRadius;
final double topLeftRadius;
final double bottomRightRadius;
final double bottomLeftRadius;

/// 指示器的颜色,默认为黑色
final Color color;

/// 水平内边距,默认为0
final double horizontalPadding;

/// 绘制风格,默认为填充
final PaintingStyle paintingStyle;

/// 描边宽度,默认为2
final double strokeWidth;

RectangularIndicator

RectangularIndicator提供了一种矩形样式的指示器,同样可以调整四个角的圆角半径以及是否采用描边方式。

/// 各个角的圆角半径
final double topRightRadius;
final double topLeftRadius;
final double bottomRightRadius;
final double bottomLeftRadius;

/// 指示器的颜色,默认为黑色
final Color color;

/// 水平和垂直内边距,默认均为0
final double horizontalPadding;
final double verticalPadding;

/// 绘制风格,默认为填充
final PaintingStyle paintingStyle;

/// 描边宽度,默认为0
final double strokeWidth;

使用方法

要使用这个插件,只需要将其作为参数传递给TabBar组件即可。下面是一个完整的例子:

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

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

class MyApp extends StatelessWidget {
  @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
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @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: [
                // Material Indicator: position bottom
                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),

                // Dot Indicator
                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),

                // Material Indicator: position top
                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),

                // RectangularIndicator Indicator: fill
                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),

                // RectangularIndicator Indicator: stroke
                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,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

通过上述代码,您可以轻松地将tab_indicator_styler集成到您的Flutter项目中,并根据需要自定义TabBar的指示器样式。希望这能帮助您更好地理解和使用该插件!


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

1 回复

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


当然,以下是如何在Flutter项目中使用tab_indicator_styler插件来自定义Tab指示器样式的示例代码。这个插件允许你灵活地调整Tab指示器的外观,包括颜色、形状、边距等。

首先,你需要在你的pubspec.yaml文件中添加tab_indicator_styler依赖:

dependencies:
  flutter:
    sdk: flutter
  tab_indicator_styler: ^最新版本号  # 请替换为当前最新版本号

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

接下来是一个简单的示例代码,展示如何使用tab_indicator_styler来自定义Tab指示器样式:

import 'package:flutter/material.dart';
import 'package:tab_indicator_styler/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('Tab Indicator Styler Example'),
        ),
        body: DefaultTabController(
          length: 3,
          child: Column(
            children: <Widget>[
              TabBar(
                indicatorSize: TabBarIndicatorSize.tab,
                indicator: TabIndicatorStyler(
                  indicatorColor: Colors.blue,
                  indicatorWeight: 5.0,
                  indicatorPadding: EdgeInsets.symmetric(horizontal: 10.0),
                  indicatorShape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(20.0),
                  ),
                ),
                tabs: [
                  Tab(icon: Icon(Icons.directions_car), text: 'Tab 1'),
                  Tab(icon: Icon(Icons.directions_transit), text: 'Tab 2'),
                  Tab(icon: Icon(Icons.directions_bike), text: 'Tab 3'),
                ],
              ),
              Expanded(
                child: TabBarView(
                  children: [
                    Center(child: Text('Content of Tab 1')),
                    Center(child: Text('Content of Tab 2')),
                    Center(child: Text('Content of Tab 3')),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

代码解释:

  1. 依赖导入

    import 'package:tab_indicator_styler/tab_indicator_styler.dart';
    
  2. TabIndicatorStyler 配置

    indicator: TabIndicatorStyler(
      indicatorColor: Colors.blue,           // 指示器颜色
      indicatorWeight: 5.0,                  // 指示器高度
      indicatorPadding: EdgeInsets.symmetric(horizontal: 10.0), // 指示器内边距
      indicatorShape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(20.0), // 指示器圆角
      ),
    ),
    
  3. TabBar 和 TabBarView

    • TabBar 用于显示标签页头部。
    • TabBarView 用于显示与标签页关联的内容。

通过这种方式,你可以完全控制Tab指示器的样式,使其符合你的应用设计需求。如果你需要更多自定义选项,请参考tab_indicator_styler的官方文档。

回到顶部