Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何从颤振中的另一个小部件访问有状态的小部件动画控制器?_Flutter_Flutter Animation - Fatal编程技术网

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
我添加了完整的代码,希望您可以直接