Lam*_*oft 5 flutter flutter-layout flutter-animation
我对颤振动画还是新手,我正在尝试制作一个 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,
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
上述代码的作者:https ://stackoverflow.com/a/47357960/1194779
现在,我需要在按下卡片后以动画方式将其关闭。
我尝试实现将卡片向下滑动的 SlideTransition。但我无法使 PageView 为刚刚删除的卡片的填充空白空间设置动画。
您可以使用Dismissible在小部件上启用滑动手势。不过,当与 PageView 一起使用时,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]}'),
),
),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4523 次 |
| 最近记录: |