android - 如何通过在 AnimatedBuilder 中点击来关闭带有 Dismissible 小部件的项目?
问题描述
我是新来的。我有一个游乐场应用程序,我正在尝试从列表中取消该项目。我发现这个例子没有关闭功能。通过向特定方向滑动可以正常工作,但我需要的是通过点击卡上的数字来消除它并完全删除滑动功能。提前致谢。
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class ColorCards {
int id;
Color color;
ColorCards({this.id, this.color});
}
class _MyHomePageState extends State<MyHomePage> {
PageController controller;
int currentPage = 0;
List<ColorCards> cards = [
ColorCards(id: 1, color: Colors.green),
ColorCards(id: 2, color: Colors.yellow),
ColorCards(id: 3, color: Colors.green),
ColorCards(id: 4, color: Colors.yellow),
ColorCards(id: 5, color: Colors.green),
];
@override
initState() {
super.initState();
controller = PageController(
initialPage: currentPage,
keepPage: false,
viewportFraction: 0.85,
);
}
@override
dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
//Scaffold
return Scaffold(
appBar: AppBar(
title: Text('Playground App'),
backgroundColor: Colors.grey,
),
body: Center(
child: Container(
child: PageView.builder(
onPageChanged: (value) {
setState(() {
currentPage = value;
});
},
itemCount: cards.length,
controller: controller,
itemBuilder: (context, index) => builder(index)),
),
),
);
}
builder(int index) {
return AnimatedBuilder(
animation: controller,
builder: (context, child) {
double value = 1.0;
if (controller.position.haveDimensions) {
value = controller.page - index;
value = (1 - (value.abs() * .1)).clamp(0.0, 1.0);
}
return Center(
child: Dismissible(
key: Key(cards[index].id.toString()),
direction: DismissDirection.up,
onDismissed: (direction) {
cards.removeAt(index);
setState(() {
controller.animateToPage(index,
duration: Duration(milliseconds: 1), curve: Curves.bounceIn);
});
},
child: SizedBox(
height: Curves.easeOut.transform(value) * 300,
width: Curves.easeOut.transform(value) * 350,
child: child,
),
),
);
},
child: Container(
child: Center(
child: GestureDetector(
onTap: () {
print('Dismiss this item');
},
child: Text(
cards[index].id.toString(),
style: TextStyle(
fontSize: 60.0, color: Colors.white, fontWeight: FontWeight.bold),
),
)),
margin: const EdgeInsets.all(8.0),
color: cards[index].color),
);
}
}
解决方案
推荐阅读
- elasticsearch - 如何在 elaticsearch 中索引问题和答案
- java - Gson 使用通用代码将枚举序列化和反序列化为整数
- c# - Xamarin Auth 包在 Xamarin tvos 中工作?
- windows - 将所有进程从 Get-Process 传递/管道/循环到 Powershell PoshInternals 脚本
- c# - C# 反序列化 json api 响应
- ios - 覆盖模拟器设备上的屏幕尺寸
- python - Ansible:委托时更改python解释器
- python - Python multiprocessing Lock.acquire() 即使 Lock 被释放甚至返回 True 也会一直阻塞
- javascript - Flow Coverage:导出默认 ReactComponent 警告
- date - 数据在表中存储为 DD/MM/YYYY,但在 DD/MM/YYYY 中查询不适用于所有日期 MS Access 2016