【发布时间】:2021-08-18 07:32:54
【问题描述】:
我正在处理以下链接:https://api.flutter.dev/flutter/widgets/AnimatedContainer-class.html
我稍微修改了一下,添加了一个带有动画的菜单,问题是当我按下一个按钮时所有的动画同时开始,如何开始我按下的按钮上的动画。 非常感谢
main.dart
import 'package:flutter/material.dart';
import './button.dart';
void main() => runApp(const MyStatefulWidget());
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
bool selected = false;
Container countryPageView() {
final PageController controller =
PageController(initialPage: 1, keepPage: true, viewportFraction: 0.35);
return Container(
height: 300,
child: PageView.builder(
scrollDirection: Axis.horizontal,
controller: controller,
itemCount: 5,
physics: BouncingScrollPhysics(),
itemBuilder: (BuildContext context, int index) {
return Container(
child: gestureDetectorOntap(index),
//child :
);
},
),
);
}
GestureDetector gestureDetectorOntap(int index) {
return GestureDetector(
onTap: () {
setState(() {
selected = !selected;
});
},
child: Column(
children: [Text('menu $index'), button(selected)], // animation function
),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: countryPageView(),
),
);
}
}
button.dart
import 'package:flutter/material.dart';
button(bool selected) {
return AnimatedContainer(
width: selected ? 200.0 : 100.0,
height: selected ? 100.0 : 200.0,
color: selected ? Colors.red : Colors.blue,
alignment: selected ? Alignment.center : AlignmentDirectional.topCenter,
duration: const Duration(seconds: 2),
curve: Curves.fastOutSlowIn,
child: const FlutterLogo(size: 75),
);
}
【问题讨论】: