【发布时间】:2019-06-26 21:18:50
【问题描述】:
我想在flutter中构建类似于这个链接的ui。
https://github.com/loopeer/CardStackView/blob/master/screenshot/screenshot1.gif
主要的理想特性如下。
- 行为类似于列表视图,但卡片应堆叠在屏幕顶部。
- 列表可以有无限项。所以应该回收旧卡以节省内存。
- 我还想为每张卡片设置不同的尺寸。
首先,我发现了一些像 ui 一样的“火种”并尝试了它们。 https://blog.geekyants.com/tinder-swipe-in-flutter-7e4fc56021bc
但是,用户需要刷每张卡片,需要用户多次刷卡才能浏览列表项。
然后我可以以某种方式制作一个列表视图,其项目与下一个重叠。
import 'package:flutter/material.dart';
class StackedList extends StatelessWidget {
List<ItemCard> cards = [];
StackedList() {
for (int i = 0; i < 20; i++) {
cards.add(ItemCard(i));
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('title')),
body: Container(
child: ListView.builder(
itemBuilder: (context, index) {
return Align(
alignment: Alignment.topCenter,
heightFactor: 0.8,
child: cards[index],
);
},
itemCount: cards.length,
),
),
);
}
}
class ItemCard extends StatelessWidget {
int index;
ItemCard(this.index);
@override
Widget build(BuildContext context) {
return Container(
decoration: const BoxDecoration(
boxShadow: [
BoxShadow(color: Colors.black, blurRadius: 20.0),
],
),
child: SizedBox.fromSize(
size: const Size(300, 400),
child: Card(
elevation: 5.0,
color: index % 2 == 0 ? Colors.blue : Colors.red,
child: Center(
child: Text(index.toString()),
),
),
),
);
}
}
但是项目不会停留在屏幕顶部,这并不是我想要的。 我想我可以通过自定义 ScrollController 或 ScrollPhysics 来实现这种效果,但我不确定我应该在哪里更改。
【问题讨论】:
-
显示代码,显示你做了什么,否则问题将被关闭。
-
好的,我更新了我的问题!
标签: dart flutter flutter-layout