TCard
一个类似于Tinder的卡片Flutter插件,可以左右滑动它的子项。您可以使用它来显示一些图片、视频等。
安装
# pubspec.yaml
dependencies:
tcard: ^1.3.3
用法
普通小部件
List<Widget> cards = List.generate(
5,
(index) => Container(
color: Colors.blue,
child: Center(
child: Text(
'$index',
style: TextStyle(fontSize: 60, color: Colors.white),
),
),
),
);
TCard(
cards: cards,
)

网络图片
List<String> images = [
'https://gank.io/images/5ba77f3415b44f6c843af5e149443f94',
'https://gank.io/images/02eb8ca3297f4931ab64b7ebd7b5b89c',
'https://gank.io/images/31f92f7845f34f05bc10779a468c3c13',
'https://gank.io/images/b0f73f9527694f44b523ff059d8a8841',
'https://gank.io/images/1af9d69bc60242d7aa2e53125a4586ad',
];
List<Widget> cards = List.generate(
images.length,
(int index) {
return Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16.0),
boxShadow: [
BoxShadow(
offset: Offset(0, 17),
blurRadius: 23.0,
spreadRadius: -13.0,
color: Colors.black54,
)
],
),
child: ClipRRect(
borderRadius: BorderRadius.circular(16.0),
child: Image.network(
images[index],
fit: BoxFit.cover,
),
),
);
},
);
TCard(
size: Size(400, 600),
cards: cards,
);

使用控制器来控制
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
TCardController _controller = TCardController();
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TCard(
cards: cards,
size: Size(360, 480),
controller: _controller,
onForward: (index, info) {
print(index);
},
onBack: (index, info) {
print(index);
},
onEnd: () {
print('end');
},
),
SizedBox(
height: 40,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
OutlineButton(
onPressed: () {
print(_controller);
_controller.back();
},
child: Text('Back'),
),
OutlineButton(
onPressed: () {
_controller.reset();
},
child: Text('Reset'),
),
OutlineButton(
onPressed: () {
_controller.forward();
},
child: Text('Forward'),
),
],
),
],
),
),
);
}
}

确定滑动方向
TCard(
cards: cards,
size: Size(360, 480),
controller: _controller,
onForward: (index, info) {
print(index);
print(info.direction);
if (info.direction == SwipDirection.Right) {
print('like');
} else {
print('dislike');
}
},
onBack: (index, info) {
print(index);
},
onEnd: () {
print('end');
},
)

重置为新的卡片宽度
List<Widget> newCards = [];
TCardController _controller = TCardController();
_controller.reset(cards: newCards);
属性
| 属性 | 类型 | 默认值 | 描述 | 必需 |
|---|---|---|---|---|
| 卡片 | List<Widget> |
空 |
渲染卡片 | 真 |
| size | Size |
空 |
卡片大小 | 假 |
| 控制器 | TCardController |
空 |
卡片控制器 | 假 |
| onForward | ForwardCallback |
空 |
前进动画回调 | 假 |
| onBack | BackCallback |
空 |
后退动画回调 | 假 |
| onEnd | EndCallback |
空 |
前进结束回调 | 假 |
| lockYAxis | 布尔值 |
假 |
锁定Y轴手势 | 假 |
| slideSpeed | 双精度 |
20 |
它应该滑动得多快?值越小越慢。10有点慢。20足够快。 | 假 |
| delaySlideFor | 整数 |
500 |
下次滑动需要等待多久?值越小越快。100足够快。500有点慢。 | 假 |