我不是新手。我有一个运动场应用程序,我正在尝试从列表中删除该项目。我发现此示例没有关闭功能。通过在特定方向上滑动,它可以正常工作,但是我需要通过点击卡上的数字来消除它,并完全消除滑动功能。预先感谢。
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),
);
}
}