所需组件
1、PageView
官方介绍:https://api.flutter-io.cn/flutter/widgets/PageView-class.html
2、AnimationController
官方介绍:https://api.flutter-io.cn/flutter/animation/AnimationController-class.html
个人需求与解决方法
将个人需求导入谷歌的GPT:使用flutter做一个页面,做一个类似列表的东西,其中元素左右滑动时,滑动到中间的元素呈现放大效果与以0度开始的一直旋转的动画
得到:
代码:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'List Animation',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
AnimationController _animationController;
PageController _pageController;
int _currentPage = 0;
@override
void initState() {
super.initState();
_animationController = AnimationController(
duration: Duration(seconds: 2),
vsync: this,
)..repeat(); // 设置旋转动画时长和循环
_pageController = PageController(initialPage: _currentPage);
}
@override
void dispose() {
_animationController.dispose(); // 释放动画资源
_pageController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('List Animation'),
),
body: PageView.builder(
controller: _pageController,
itemCount: 5,
onPageChanged: (index) {
setState(() {
_currentPage = index;
});
},
itemBuilder: (context