Flutter动作滑块插件action_slider的使用
Flutter动作滑块插件action_slider的使用
插件介绍
action_slider
是一个用于确认操作并提供操作成功后反馈的滑块组件。它支持从左到右(LTR)和从右到左(RTL)两种方向,并且提供了多种自定义选项,以满足不同的设计需求。对于类似外观的开关组件,可以查看 animated_toggle_switch。
如果你喜欢这个包,请在 pub.dev 上点赞并在 GitHub 上给个星标。
示例图片
-
标准滑块(Stretch行为)
-
标准滑块(RTL方向)
-
双端滑块
-
滚动滑块
-
滚动滑块(Stretch行为)
-
自定义滑块
示例代码
以下是一个完整的示例代码,展示了如何使用 action_slider
插件:
import 'dart:math';
import 'package:action_slider/action_slider.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Confirmation Slider Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Confirmation Slider Example'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _controller = ActionSliderController();
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
DefaultTextStyle.merge(
style: const TextStyle(color: Colors.white),
child: ActionSlider.dual(
backgroundBorderRadius: BorderRadius.circular(10.0),
foregroundBorderRadius: BorderRadius.circular(10.0),
width: 300.0,
backgroundColor: Colors.black,
startChild: const Text('Start'),
endChild: const Text('End'),
icon: Padding(
padding: const EdgeInsets.only(right: 2.0),
child: Transform.rotate(
angle: 0.5 * pi,
child: const Icon(Icons.unfold_more_rounded, size: 28.0)),
),
startAction: (controller) async {
controller.loading(); // starts loading animation
await Future.delayed(const Duration(seconds: 3));
controller.success(); // starts success animation
await Future.delayed(const Duration(seconds: 1));
controller.reset(); // resets the slider
},
endAction: (controller) async {
controller.loading(); // starts loading animation
await Future.delayed(const Duration(seconds: 3));
controller.success(); // starts success animation
await Future.delayed(const Duration(seconds: 1));
controller.reset(); // resets the slider
},
),
),
const SizedBox(height: 24.0),
ActionSlider.standard(
sliderBehavior: SliderBehavior.stretch,
width: 300.0,
backgroundColor: Colors.white,
toggleColor: Colors.lightGreenAccent,
action: (controller) async {
controller.loading(); // starts loading animation
await Future.delayed(const Duration(seconds: 3));
controller.success(); // starts success animation
await Future.delayed(const Duration(seconds: 1));
controller.reset(); // resets the slider
},
child: const Text('Slide to confirm'),
),
const SizedBox(height: 24.0),
ActionSlider.standard(
width: 300.0,
action: (controller) async {
controller.loading(); // starts loading animation
await Future.delayed(const Duration(seconds: 3));
controller.success(); // starts success animation
await Future.delayed(const Duration(seconds: 1));
controller.reset(); // resets the slider
},
direction: TextDirection.rtl,
child: const Text('Slide to confirm'),
),
const SizedBox(height: 24.0),
ActionSlider.standard(
rolling: true,
width: 300.0,
backgroundColor: Colors.black,
reverseSlideAnimationCurve: Curves.easeInOut,
reverseSlideAnimationDuration: const Duration(milliseconds: 500),
toggleColor: Colors.purpleAccent,
icon: const Icon(Icons.add),
action: (controller) async {
controller.loading(); // starts loading animation
await Future.delayed(const Duration(seconds: 3));
controller.success(); // starts success animation
await Future.delayed(const Duration(seconds: 1));
controller.reset(); // resets the slider
},
child: const Text('Rolling slider', style: TextStyle(color: Colors.white)),
),
const SizedBox(height: 24.0),
ActionSlider.standard(
sliderBehavior: SliderBehavior.stretch,
rolling: true,
width: 300.0,
backgroundColor: Colors.white,
toggleColor: Colors.amber,
iconAlignment: Alignment.centerRight,
loadingIcon: SizedBox(
width: 55,
child: Center(
child: SizedBox(
width: 24.0,
height: 24.0,
child: CircularProgressIndicator(strokeWidth: 2.0, color: theme.iconTheme.color),
))),
successIcon: const SizedBox(width: 55, child: Center(child: Icon(Icons.check_rounded))),
icon: const SizedBox(width: 55, child: Center(child: Icon(Icons.refresh_rounded))),
action: (controller) async {
controller.loading(); // starts loading animation
await Future.delayed(const Duration(seconds: 3));
controller.success(); // starts success animation
await Future.delayed(const Duration(seconds: 1));
controller.reset(); // resets the slider
},
child: const Text('Swipe right'),
),
const SizedBox(height: 24.0),
ActionSlider.custom(
sliderBehavior: SliderBehavior.stretch,
width: 300.0,
controller: _controller,
height: 60.0,
toggleWidth: 60.0,
toggleMargin: EdgeInsets.zero,
backgroundColor: Colors.green,
foregroundChild: DecoratedBox(
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(5)),
child: const Icon(Icons.check_rounded, color: Colors.white)),
foregroundBuilder: (context, state, child) => child!,
outerBackgroundBuilder: (context, state, child) => Card(
margin: EdgeInsets.zero,
color: Color.lerp(Colors.red, Colors.green, state.position),
child: Center(
child: Text(state.position.toStringAsFixed(2),
style: theme.textTheme.titleMedium)),
),
backgroundBorderRadius: BorderRadius.circular(5.0),
action: (controller) async {
controller.loading(); // starts loading animation
await Future.delayed(const Duration(seconds: 3));
controller.success(); // starts success animation
await Future.delayed(const Duration(seconds: 1));
controller.reset(); // resets the slider
},
),
const SizedBox(height: 24.0),
ActionSlider.custom(
toggleMargin: EdgeInsets.zero,
width: 300.0,
controller: _controller,
toggleWidth: 60.0,
height: 60.0,
backgroundColor: Colors.green,
foregroundChild: Container(
decoration: const BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.all(Radius.circular(30.0)),
),
child: const Icon(Icons.check_rounded, color: Colors.white)),
foregroundBuilder: (context, state, child) => child!,
backgroundChild: Center(
child: Text('Highly Customizable :)', style: theme.textTheme.titleMedium),
),
backgroundBuilder: (context, state, child) => ClipRect(
child: OverflowBox(
maxWidth: state.standardSize.width,
maxHeight: state.toggleSize.height,
minWidth: state.standardSize.width,
minHeight: state.toggleSize.height,
child: child!)),
backgroundBorderRadius: BorderRadius.circular(5.0),
action: (controller) async {
controller.loading(); // starts loading animation
await Future.delayed(const Duration(seconds: 3));
controller.success(); // starts success animation
await Future.delayed(const Duration(seconds: 1));
controller.reset(); // resets the slider
},
),
],
),
),
);
}
}
使用说明
标准滑块
使用 ActionSlider.standard()
创建一个标准的动作滑块。你可以通过设置 sliderBehavior
来控制滑块的行为(例如拉伸或固定),并通过 action
参数定义滑动完成后的操作。
ActionSlider.standard(
sliderBehavior: SliderBehavior.stretch,
width: 300.0,
backgroundColor: Colors.white,
toggleColor: Colors.lightGreenAccent,
action: (controller) async {
controller.loading(); // starts loading animation
await Future.delayed(const Duration(seconds: 3));
controller.success(); // starts success animation
await Future.delayed(const Duration(seconds: 1));
controller.reset(); // resets the slider
},
child: const Text('Slide to confirm'),
)
双端滑块
使用 ActionSlider.dual()
创建一个两端都有触发动作的滑块。你可以分别为两端设置不同的触发动作。
ActionSlider.dual(
backgroundBorderRadius: BorderRadius.circular(10.0),
foregroundBorderRadius: BorderRadius.circular(10.0),
width: 300.0,
backgroundColor: Colors.black,
startChild: const Text('Start'),
endChild: const Text('End'),
icon: Padding(
padding: const EdgeInsets.only(right: 2.0),
child: Transform.rotate(
angle: 0.5 * pi,
child: const Icon(Icons.unfold_more_rounded, size: 28.0)),
),
startAction: (controller) async {
controller.loading(); // starts loading animation
await Future.delayed(const Duration(seconds: 3));
controller.success(); // starts success animation
await Future.delayed(const Duration(seconds: 1));
controller.reset(); // resets the slider
},
endAction: (controller) async {
controller.loading(); // starts loading animation
await Future.delayed(const Duration(seconds: 3));
controller.success(); // starts success animation
await Future.delayed(const Duration(seconds: 1));
controller.reset(); // resets the slider
},
)
自定义滑块
使用 ActionSlider.custom()
创建高度自定义的滑块。你可以完全控制滑块的前景、背景和外层背景的构建逻辑。
ActionSlider.custom(
sliderBehavior: SliderBehavior.stretch,
width: 300.0,
controller: _controller,
height: 60.0,
toggleWidth: 60.0,
toggleMargin: EdgeInsets.zero,
backgroundColor: Colors.green,
foregroundChild: DecoratedBox(
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(5)),
child: const Icon(Icons.check_rounded, color: Colors.white)),
foregroundBuilder: (context, state, child) => child!,
outerBackgroundBuilder: (context, state, child) => Card(
margin: EdgeInsets.zero,
color: Color.lerp(Colors.red, Colors.green, state.position),
child: Center(
child: Text(state.position.toStringAsFixed(2),
style: theme.textTheme.titleMedium)),
),
backgroundBorderRadius: BorderRadius.circular(5.0),
action: (controller) async {
controller.loading(); // starts loading animation
await Future.delayed(const Duration(seconds: 3));
controller.success(); // starts success animation
await Future.delayed(const Duration(seconds: 1));
controller.reset(); // resets the slider
},
)
希望这些信息能帮助你更好地理解和使用 action_slider
插件!如果有任何问题或需要进一步的帮助,请随时提问。
更多关于Flutter动作滑块插件action_slider的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter动作滑块插件action_slider的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何在Flutter中使用action_slider
插件的示例代码。这个插件允许你创建一个带有动作按钮的滑块。首先,你需要确保你的Flutter项目中已经添加了这个插件。如果还没有添加,可以在pubspec.yaml
文件中添加以下依赖:
dependencies:
flutter:
sdk: flutter
action_slider: ^最新版本号 # 替换为实际的最新版本号
然后,运行flutter pub get
来安装依赖。
下面是一个完整的示例代码,展示了如何使用action_slider
插件:
import 'package:flutter/material.dart';
import 'package:action_slider/action_slider.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Action Slider Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
double _sliderValue = 0.0;
void _onSliderChange(double value) {
setState(() {
_sliderValue = value;
});
}
void _onActionPressed() {
// 这里是点击动作按钮后的处理逻辑
print("Action button pressed with slider value: $_sliderValue");
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Action Slider Demo'),
),
body: Center(
child: ActionSlider(
value: _sliderValue,
minValue: 0.0,
maxValue: 100.0,
onValueChanged: _onSliderChange,
actionButton: IconButton(
icon: Icon(Icons.check),
color: Colors.blue,
onPressed: _onActionPressed,
),
actionButtonText: 'Action', // 可选,如果不需要文本可以省略
actionButtonTextStyle: TextStyle(color: Colors.white), // 可选,设置文本样式
),
),
);
}
}
解释
-
依赖添加:
- 在
pubspec.yaml
文件中添加action_slider
依赖。
- 在
-
导入包:
- 在Dart文件中导入
action_slider
包。
- 在Dart文件中导入
-
UI结构:
- 使用
MaterialApp
来构建应用的基本结构。 MyApp
是根widget,包含一个主题和一个主页MyHomePage
。MyHomePage
是一个StatefulWidget
,它管理滑块的值。
- 使用
-
滑块和动作按钮:
ActionSlider
widget用于创建带有动作按钮的滑块。value
:当前滑块的值。minValue
和maxValue
:滑块的最小值和最大值。onValueChanged
:滑块值改变时的回调。actionButton
:一个IconButton
,当用户点击时触发_onActionPressed
函数。actionButtonText
和actionButtonTextStyle
:可选参数,用于显示动作按钮上的文本及其样式。
-
处理逻辑:
_onSliderChange
函数更新滑块的值。_onActionPressed
函数处理动作按钮的点击事件,并打印当前滑块的值。
这个示例展示了如何集成和使用action_slider
插件,创建一个带有动作按钮的滑块,并在用户交互时处理相应的逻辑。