gpt4 book ai didi

flutter - 如何使用流畅的动画从页面 View 中删除页面?

转载 作者:行者123 更新时间:2023-12-04 13:59:13 25 4
gpt4 key购买 nike

我对 flutter 动画还是个新手,我试图让一个 PageView 在你用动画按下它时关闭。

我有这个代码:

class Carroussel extends StatefulWidget {
@override
_CarrousselState createState() => new _CarrousselState();
}

class _CarrousselState extends State<Carroussel> {
PageController controller;
int currentpage = 0;

@override
initState() {
super.initState();
controller = new PageController(
initialPage: currentpage,
keepPage: false,
viewportFraction: 0.5,
);
}

@override
dispose() {
controller.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Container(
child: new PageView.builder(
onPageChanged: (value) {
setState(() {
currentpage = value;
});
},
controller: controller,
itemBuilder: (context, index) => builder(index)),
),
),
);
}

builder(int index) {
return new AnimatedBuilder(
animation: controller,
builder: (context, child) {
double value = 1.0;
if (pageController.position.haveDimensions) {
value = controller.page - index;
value = (1 - (value.abs() * .5)).clamp(0.0, 1.0);
}

return new Center(
child: new SizedBox(
height: Curves.easeOut.transform(value) * 300,
width: Curves.easeOut.transform(value) * 250,
child: child,
),
);
},
child: new Container(
margin: const EdgeInsets.all(8.0),
color: index % 2 == 0 ? Colors.blue : Colors.red,
),
);
}
}

以上代码作者: https://stackoverflow.com/a/47357960/1194779

产生以下内容:
enter image description here

现在我需要在我按下一张卡片后制作它,然后用动画将其解散。

我曾尝试实现将卡片向下滑动的 SlideTransition。但是我没有让 PageView 为刚刚移除的卡片的填充空白空间设置动画。

最佳答案

您可以使用 Dismissible在您的小部件上启用滑动手势。尽管与 PageViews 一起使用时,Dismissible 会产生可能不受欢迎的卡顿。

class Carousel extends StatefulWidget {
@override
_CarouselState createState() => _CarouselState();
}

class _CarouselState extends State<Carousel>{
late PageController controller;
int currentPage = 0;
List<String> listItem = ['Page 1', 'Page 2', 'Page 3', 'Page 4'];

@override
initState() {
super.initState();
controller = PageController(
initialPage: currentPage,
keepPage: false,
viewportFraction: 0.5,
);
}

@override
dispose() {
controller.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
child: PageView.builder(
itemCount: listItem.length,
onPageChanged: (value) {
setState(() {
currentPage = value;
});
},
controller: controller,
itemBuilder: (BuildContext context, int index) =>
pageBuilder(index),
),
),
),
);
}

pageBuilder(int index) {
var dismissibleKey = GlobalKey<State>();
return AnimatedBuilder(
animation: controller,
builder: (context, child) {
double value = 1.0;
if (controller.position.haveDimensions) {
value = controller.page! - index;
value = (1 - (value.abs() * .5)).clamp(0.0, 1.0);
}

return Center(
child: SizedBox(
height: Curves.easeOut.transform(value) * 300,
width: Curves.easeOut.transform(value) * 250,
child: child,
),
);
},
child: Dismissible(
key: dismissibleKey,
direction: DismissDirection.down,
onDismissed: (DismissDirection direction) {
/// Remove item from List
setState(() {
listItem.removeAt(index);
});
},
child: InkWell(
onLongPress: () {
debugPrint('Delete! $index');
setState(() {
listItem.removeAt(index);
});
},
onTap: () {
controller.animateToPage(index,
duration: Duration(milliseconds: 500), curve: Curves.ease);
},
child: Container(
margin: const EdgeInsets.all(8.0),
// color: index % 2 == 0 ? Colors.blue : Colors.red,
color: Colors.lightBlueAccent,
child: Center(
child: Text('${listItem[index]}'),
),
),
),
),
);
}
}
Demo

关于flutter - 如何使用流畅的动画从页面 View 中删除页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54910418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com