Flutter手势滑动堆叠插件swipe_stack_null_safe的使用
Flutter手势滑动堆叠插件swipe_stack_null_safe的使用
Screenshot
Installation
在你的 pubspec.yaml
文件中添加以下依赖:
swipe_stack_null_safe: ^latest_version
然后运行 flutter pub get
来获取最新的依赖。
Usage
以下是 SwipeStack
的参数及其默认值和描述:
参数 | 默认值 | 描述 |
---|---|---|
maxAngle | 35 | 最大旋转值。必须为介于 0 和 360 之间的整数 |
threshold | 30 | 当卡片拖动距离小于阈值时,手动滑动将被取消。必须为介于 1 和 100 之间的整数 |
stackFrom | StackFrom.None | 堆叠类型。必须是 StackFrom.Left, StackFrom.Right, StackFrom.Top, StackFrom.Bottom, StackFrom.None 中的一个 |
visibleCount | 2 | 显示的对象数量。必须为至少为 2 的整数 |
translationInterval | 0 | 对象之间的距离。必须为正整数 |
scaleInterval | 0 | 缩放值递减。必须为正浮点数 |
animationDuration | Duration(milliseconds: 200) | 动画持续时间 |
historyCount | 1 | 保持在历史记录中的对象数量。必须为正整数 |
padding | EdgeInsets.symmetric(vertical: 20, horizontal: 25) | 主容器内边距 |
示例代码:
SwipeStack(
children: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map((int index) {
return SwiperItem(
builder: (SwiperPosition position, double progress) {
return Material(
elevation: 4,
borderRadius: BorderRadius.all(Radius.circular(6)),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(6)),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Item $index", style: TextStyle(color: Colors.black, fontSize: 20)),
Text("Progress $progress", style: TextStyle(color: Colors.blue, fontSize: 12)),
],
)
)
);
}
);
}).toList(),
visibleCount: 3,
stackFrom: StackFrom.Top,
translationInterval: 6,
scaleInterval: 0.03,
onEnd: () => debugPrint("onEnd"),
onSwipe: (int index, SwiperPosition position) => debugPrint("onSwipe $index $position"),
onRewind: (int index, SwiperPosition position) => debugPrint("onRewind $index $position"),
)
手动使用
首先定义一个全局的 GlobalKey<SwipeStackState>
:
final GlobalKey<SwipeStackState> _swipeKey = GlobalKey<SwipeStackState>();
然后在 SwipeStack
组件中指定该键:
SwipeStack(
key: _swipeKey,
...
)
可以通过以下方法手动操作:
_swipeKey.currentState?.swipeLeft();
_swipeKey.currentState?.swipeRight();
_swipeKey.currentState?.rewind();
更多关于Flutter手势滑动堆叠插件swipe_stack_null_safe的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter手势滑动堆叠插件swipe_stack_null_safe的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
swipe_stack_null_safe
是一个用于 Flutter 的插件,它允许你创建一个可以滑动堆叠的卡片组件。每个卡片可以通过滑动手势(如向左、向右滑动)来移除,并显示下一个卡片。这个插件是 null safety 兼容的,因此可以在支持 null safety 的 Flutter 项目中使用。
安装
首先,你需要在 pubspec.yaml
文件中添加 swipe_stack_null_safe
插件的依赖:
dependencies:
flutter:
sdk: flutter
swipe_stack_null_safe: ^1.0.0
然后运行 flutter pub get
来安装依赖。
使用
以下是一个简单的示例,展示了如何使用 swipe_stack_null_safe
插件来创建一个可以滑动的卡片堆叠:
import 'package:flutter/material.dart';
import 'package:swipe_stack_null_safe/swipe_stack_null_safe.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SwipeStackExample(),
);
}
}
class SwipeStackExample extends StatefulWidget {
@override
_SwipeStackExampleState createState() => _SwipeStackExampleState();
}
class _SwipeStackExampleState extends State<SwipeStackExample> {
final List<String> cards = ['Card 1', 'Card 2', 'Card 3', 'Card 4'];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Swipe Stack Example'),
),
body: SwipeStack(
children: cards.map((card) {
return SwiperItem(
builder: (BuildContext context, int index) {
return Card(
child: Center(
child: Text(card),
),
);
},
);
}).toList(),
onSwipe: (int index, SwiperPosition position) {
print('Swiped card index: $index, position: $position');
},
onRewind: (int index, SwiperPosition position) {
print('Rewinded card index: $index, position: $position');
},
),
);
}
}
解释
-
SwipeStack
: 这是一个包裹所有卡片的组件。它负责处理滑动手势并管理卡片堆叠。 -
SwiperItem
: 每个卡片都是一个SwiperItem
。你可以在builder
中定义每个卡片的内容。 -
onSwipe
: 当用户滑动一个卡片时,这个回调会被触发。你可以在这个回调中处理卡片的移除逻辑。 -
onRewind
: 当用户回退一个卡片时,这个回调会被触发。
自定义
你可以通过以下方式自定义 SwipeStack
:
swipeSensitivity
: 控制滑动的灵敏度。swipeThreshold
: 控制滑动的最小距离。maxAngle
: 控制卡片旋转的最大角度。stackFrom
: 控制卡片的堆叠方向(从上到下或从下到上)。
例如:
SwipeStack(
swipeSensitivity: 0.6,
swipeThreshold: 0.3,
maxAngle: 30,
stackFrom: StackFrom.bottom,
children: cards.map((card) {
return SwiperItem(
builder: (BuildContext context, int index) {
return Card(
child: Center(
child: Text(card),
),
);
},
);
}).toList(),
onSwipe: (int index, SwiperPosition position) {
print('Swiped card index: $index, position: $position');
},
onRewind: (int index, SwiperPosition position) {
print('Rewinded card index: $index, position: $position');
},
)