Flutter自定义指示器插件custom_indicator的使用
特性
🌟 支持自定义圆形指示器
🌟 支持自定义线性指示器
开始使用
安装插件
在 pubspec.yaml
文件中添加依赖:
dependencies:
flutter:
sdk: flutter
custom_indicator: any
或者直接运行以下命令:
flutter pub add custom_indicator
然后导入插件:
import 'package:custom_indicator/custom_indicator.dart';
使用方法
基本用法
线性指示器
无需传值时可以直接使用默认样式:
CustomLinearIndicator()
圆形指示器
同样可以不传值直接使用:
CustomCircleIndicator()
添加值
可以通过 value
属性为指示器添加具体的进度值。
圆形指示器
CustomCircleIndicator(
value: 100, // 进度值
backColor: Colors.red, // 背景色
),
线性指示器
CustomLinearIndicator(
value: 100, // 进度值
backColor: Colors.red, // 背景色
),
修改指示器类型
可以设置不同的指示器类型。
圆形指示器
CustomCircleIndicator(
value: 35,
backColor: Colors.red,
pointerColor: Colors.red, // 指针颜色
indicatorType: IndicatorType.pointer, // 类型:指针
),
线性指示器
CustomLinearIndicator(
value: 35,
backColor: Colors.red,
indicatorType: IndicatorType.pointer, // 类型:指针
),
结束检查
通过 checkOnFinish
属性可以在进度完成时触发某些操作。
圆形指示器
CustomCircleIndicator(
value: 100,
checkOnFinish: true, // 进度完成时触发
backColor: Colors.red,
),
线性指示器
CustomLinearIndicator(
value: 100,
backColor: Colors.red,
checkOnFinish: true, // 进度完成时触发
height: 70, // 高度
),
修改线性边缘
通过 edgeType
属性可以调整线性指示器的边缘样式。
CustomLinearIndicator(
value: 35,
backColor: Colors.red,
checkOnFinish: true,
edgeType: EdgeType.square, // 边缘类型:方形
),
填充线性指示器
通过 filled
属性可以让线性指示器变为填充样式。
CustomLinearIndicator(
value: 35,
backColor: Colors.red,
checkOnFinish: true,
filled: true, // 填充样式
),
隐藏进度值
通过 showValue
属性可以隐藏指示器上的进度值。
CustomLinearIndicator(
value: 35,
backColor: Colors.red,
checkOnFinish: true,
filled: true,
showValue: false, // 隐藏进度值
),
完整示例代码
import 'package:flutter/material.dart';
import 'package:custom_indicator/custom_indicator.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: '自定义指示器示例',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
[@override](/user/override)
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: CustomCircleIndicator(
value: 50, // 进度值
backColor: Colors.red, // 背景色
checkOnFinish: true, // 进度完成时触发
),
),
);
}
}
更多关于Flutter自定义指示器插件custom_indicator的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter自定义指示器插件custom_indicator的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在 Flutter 中,自定义指示器(Custom Indicator)通常用于自定义分页指示器、加载指示器等。虽然 Flutter 本身提供了一些内置的指示器(如 CircularProgressIndicator
和 LinearProgressIndicator
),但有时你可能需要更复杂或更个性化的指示器。这时,你可以使用或创建一个自定义指示器插件。
假设你已经有一个名为 custom_indicator
的插件,以下是如何在 Flutter 项目中使用它的基本步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 custom_indicator
插件的依赖。假设这个插件已经发布在 pub.dev 上,你可以这样添加:
dependencies:
flutter:
sdk: flutter
custom_indicator: ^1.0.0 # 请根据实际情况替换版本号
然后运行 flutter pub get
来获取依赖。
2. 导入插件
在你的 Dart 文件中导入 custom_indicator
插件:
import 'package:custom_indicator/custom_indicator.dart';
3. 使用自定义指示器
假设 custom_indicator
插件提供了一个名为 CustomIndicator
的组件,你可以在你的 UI 中使用它。以下是一个简单的示例:
import 'package:flutter/material.dart';
import 'package:custom_indicator/custom_indicator.dart';
class MyHomePage extends StatefulWidget {
[@override](/user/override)
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _currentPage = 0;
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Custom Indicator Example'),
),
body: Column(
children: [
Expanded(
child: PageView(
onPageChanged: (index) {
setState(() {
_currentPage = index;
});
},
children: [
Center(child: Text('Page 1')),
Center(child: Text('Page 2')),
Center(child: Text('Page 3')),
],
),
),
CustomIndicator(
count: 3,
currentIndex: _currentPage,
activeColor: Colors.blue,
inactiveColor: Colors.grey,
),
],
),
);
}
}
void main() {
runApp(MaterialApp(
home: MyHomePage(),
));
}
4. 自定义指示器的参数
根据 custom_indicator
插件的文档,你可以传递不同的参数来自定义指示器的外观和行为。例如:
count
: 指示器的总数量。currentIndex
: 当前选中的指示器索引。activeColor
: 当前选中指示器的颜色。inactiveColor
: 未选中指示器的颜色。size
: 指示器的大小。spacing
: 指示器之间的间距。
5. 运行项目
保存你的代码并运行项目,你应该能够看到自定义指示器在页面底部显示,并且随着页面切换而更新。
6. 进一步自定义
如果 custom_indicator
插件提供了更多的自定义选项,你可以根据需要进行调整。如果插件没有提供你需要的功能,你可以考虑修改插件的源代码或创建一个新的自定义指示器。
7. 创建自定义指示器
如果你找不到合适的插件,或者你想完全自定义指示器,你可以直接在 Flutter 中创建一个自定义的指示器组件。以下是一个简单的自定义指示器示例:
class CustomIndicator extends StatelessWidget {
final int count;
final int currentIndex;
final Color activeColor;
final Color inactiveColor;
final double size;
final double spacing;
CustomIndicator({
required this.count,
required this.currentIndex,
this.activeColor = Colors.blue,
this.inactiveColor = Colors.grey,
this.size = 8.0,
this.spacing = 4.0,
});
[@override](/user/override)
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: List.generate(count, (index) {
return Container(
width: size,
height: size,
margin: EdgeInsets.symmetric(horizontal: spacing),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: index == currentIndex ? activeColor : inactiveColor,
),
);
}),
);
}
}