Flutter 如何从颤振中的另一个小部件访问有状态的小部件动画控制器?
我希望计数器小部件从主页显示Flutter 如何从颤振中的另一个小部件访问有状态的小部件动画控制器?,flutter,flutter-animation,Flutter,Flutter Animation,我希望计数器小部件从主页显示onTapwidget。此外,在每次轻触时从“开始”开始播放动画 计数器小部件 class Counter extends StatefulWidget { @override _CounterState createState() => _CounterState(); } class _CounterState extends State<Counter> with SingleTickerProviderStateMixin {
onTap
widget
。此外,在每次轻触时从“开始”开始播放动画
计数器小部件
class Counter extends StatefulWidget {
@override
_CounterState createState() => _CounterState();
}
class _CounterState extends State<Counter> with SingleTickerProviderStateMixin {
AnimationController animationController;
Animation<double> animation;
@override
void initState() {
super.initState();
animationController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 1000),
)
..addListener(() => setState(() {}));
animation = CurvedAnimation(
parent: animationController,
curve: Curves.elasticOut,
);
animationController.forward();
}
@override
Widget build(BuildContext context) {
return ScaleTransition(
child: Container(
height: 100,
width: 100,
color: Colors.blue,
),
scale: animation,
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
Center(
child: Counter(),
),
GestureDetector(
onTap: () {},
child: Container(
width: double.infinity,
height: double.infinity,
),
),
],
));
}
类计数器扩展StatefulWidget{
@凌驾
_CounterState createState()=>\u CounterState();
}
类_CounterState使用SingleTickerProviderStateMixin扩展状态{
动画控制器;
动画;
@凌驾
void initState(){
super.initState();
animationController=animationController(
vsync:这个,,
持续时间:持续时间(毫秒:1000),
)
..addListener(()=>setState((){}));
动画=曲线动画(
父对象:animationController,
曲线:Curves.elasticOut,
);
animationController.forward();
}
@凌驾
小部件构建(构建上下文){
返回刻度转换(
子:容器(
身高:100,
宽度:100,
颜色:颜色,蓝色,
),
比例:动画,
);
}
}
主页小部件
class Counter extends StatefulWidget {
@override
_CounterState createState() => _CounterState();
}
class _CounterState extends State<Counter> with SingleTickerProviderStateMixin {
AnimationController animationController;
Animation<double> animation;
@override
void initState() {
super.initState();
animationController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 1000),
)
..addListener(() => setState(() {}));
animation = CurvedAnimation(
parent: animationController,
curve: Curves.elasticOut,
);
animationController.forward();
}
@override
Widget build(BuildContext context) {
return ScaleTransition(
child: Container(
height: 100,
width: 100,
color: Colors.blue,
),
scale: animation,
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
Center(
child: Counter(),
),
GestureDetector(
onTap: () {},
child: Container(
width: double.infinity,
height: double.infinity,
),
),
],
));
}
@覆盖
小部件构建(构建上下文){
返回脚手架(
主体:堆栈(
儿童:[
居中(
子对象:计数器(),
),
手势检测器(
onTap:(){},
子:容器(
宽度:double.infinity,
高度:双无限,
),
),
],
));
}
您需要定义一个GlobalKey
并将其传递给子小部件。这将使您能够访问该子级的方法和变量:
class HomeWidget extends StatefulWidget {
@override
_HomeWidgetState createState() => _HomeWidgetState();
}
class _HomeWidgetState extends State<HomeWidget> {
GlobalKey<CounterState> counterKey = GlobalKey<CounterState>();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
Center(
child: Counter(key: counterKey),
),
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
counterKey.currentState.animationController.reverse();
},
child: Container(
width: double.infinity,
height: double.infinity,
),
),
],
)
);
}
}
class Counter extends StatefulWidget {
Counter({Key key}): super(key: key);
// This state must be public in order to access it.
@override
CounterState createState() => CounterState();
}
class CounterState extends State<Counter> with SingleTickerProviderStateMixin {
AnimationController animationController;
Animation<double> animation;
@override
void initState() {
super.initState();
animationController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 1000),
)
..addListener(() => setState(() {}));
animation = CurvedAnimation(
parent: animationController,
curve: Curves.elasticOut,
);
animationController.forward();
}
@override
Widget build(BuildContext context) {
return ScaleTransition(
child: Container(
height: 100,
width: 100,
color: Colors.blue,
),
scale: animation,
);
}
}
class HomeWidget扩展了StatefulWidget{
@凌驾
_HomeWidgetState createState();
}
类_HomeWidgetState扩展状态{
GlobalKey counterKey=GlobalKey();
@凌驾
小部件构建(构建上下文){
返回脚手架(
主体:堆栈(
儿童:[
居中(
子项:计数器(键:计数器键),
),
手势检测器(
行为:HitTestBehavior.transparent,
onTap:(){
counterKey.currentState.animationController.reverse();
},
子:容器(
宽度:double.infinity,
高度:双无限,
),
),
],
)
);
}
}
类计数器扩展StatefulWidget{
计数器({Key}):超级(Key:Key);
//此状态必须是公共状态才能访问它。
@凌驾
CounterState createState()=>CounterState();
}
类CounterState使用SingleTickerProviderStateMixin扩展状态{
动画控制器;
动画;
@凌驾
void initState(){
super.initState();
animationController=animationController(
vsync:这个,,
持续时间:持续时间(毫秒:1000),
)
..addListener(()=>setState((){}));
动画=曲线动画(
父对象:animationController,
曲线:Curves.elasticOut,
);
animationController.forward();
}
@凌驾
小部件构建(构建上下文){
返回刻度转换(
子:容器(
身高:100,
宽度:100,
颜色:颜色,蓝色,
),
比例:动画,
);
}
}
您需要定义一个GlobalKey
并将其传递给子小部件。这将使您能够访问该子级的方法和变量:
class HomeWidget extends StatefulWidget {
@override
_HomeWidgetState createState() => _HomeWidgetState();
}
class _HomeWidgetState extends State<HomeWidget> {
GlobalKey<CounterState> counterKey = GlobalKey<CounterState>();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
Center(
child: Counter(key: counterKey),
),
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
counterKey.currentState.animationController.reverse();
},
child: Container(
width: double.infinity,
height: double.infinity,
),
),
],
)
);
}
}
class Counter extends StatefulWidget {
Counter({Key key}): super(key: key);
// This state must be public in order to access it.
@override
CounterState createState() => CounterState();
}
class CounterState extends State<Counter> with SingleTickerProviderStateMixin {
AnimationController animationController;
Animation<double> animation;
@override
void initState() {
super.initState();
animationController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 1000),
)
..addListener(() => setState(() {}));
animation = CurvedAnimation(
parent: animationController,
curve: Curves.elasticOut,
);
animationController.forward();
}
@override
Widget build(BuildContext context) {
return ScaleTransition(
child: Container(
height: 100,
width: 100,
color: Colors.blue,
),
scale: animation,
);
}
}
class HomeWidget扩展了StatefulWidget{
@凌驾
_HomeWidgetState createState();
}
类_HomeWidgetState扩展状态{
GlobalKey counterKey=GlobalKey();
@凌驾
小部件构建(构建上下文){
返回脚手架(
主体:堆栈(
儿童:[
居中(
子项:计数器(键:计数器键),
),
手势检测器(
行为:HitTestBehavior.transparent,
onTap:(){
counterKey.currentState.animationController.reverse();
},
子:容器(
宽度:double.infinity,
高度:双无限,
),
),
],
)
);
}
}
类计数器扩展StatefulWidget{
计数器({Key}):超级(Key:Key);
//此状态必须是公共状态才能访问它。
@凌驾
CounterState createState()=>CounterState();
}
类CounterState使用SingleTickerProviderStateMixin扩展状态{
动画控制器;
动画;
@凌驾
void initState(){
super.initState();
animationController=animationController(
vsync:这个,,
持续时间:持续时间(毫秒:1000),
)
..addListener(()=>setState((){}));
动画=曲线动画(
父对象:animationController,
曲线:Curves.elasticOut,
);
animationController.forward();
}
@凌驾
小部件构建(构建上下文){
返回刻度转换(
子:容器(
身高:100,
宽度:100,
颜色:颜色,蓝色,
),
比例:动画,
);
}
}
我无法将\u CounterState
放入GlobalKey
中。它说,\u CounterState
不是一个类型,因此不能用作类型参数。此外,它还说dynamic
不扩展State
我已经添加了完整的代码,希望您可以进行直接比较,看看可能出了什么问题。找到答案。因为\u CounterState
是私有的,所以我无法访问它。我已将其更改为CounterState
。现在很有魅力。@Xihuny,太好了。如果解决了您的问题,请不要忘记将答案标记为正确。我无法将\u CounterState
放入GlobalKey
中。它说,\u CounterState
不是一个类型,因此不能用作类型参数。此外,它还说dynamic
不扩展State
我添加了完整的代码,希望您可以直接