简体   繁体   English

如何从 Flutter 中的另一个小部件访问有状态小部件动画控制器?

[英]How to access Stateful widget animation controller from another widget in Flutter?

I want the counter widget to show onTap from home widget .我希望计数器小部件从 home widget显示onTap Also, start the animation from start at every tap.此外,在每次点击时从头开始animation

Counter 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,
    );
  }
}

Home Widget主页小工具

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Stack(
      children: <Widget>[
        Center(
          child: Counter(),
        ),
        GestureDetector(
          onTap: () {},
          child: Container(
            width: double.infinity,
            height: double.infinity,
          ),
        ),
      ],
    ));
  }

You need to define a GlobalKey and pass it down to the child widget.您需要定义一个GlobalKey并将其传递给子小部件。 This will enable you to access that Child's methods and variables:这将使您能够访问该 Child 的方法和变量:

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,
    );
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter:如何从另一个有状态类更新 UI 状态小部件 - Flutter:How to update a UI -stateful widget from another stateful class 从另一个有状态小部件调用一个有状态小部件中的方法 - Flutter - call method in one stateful widget from another stateful widget - Flutter 如何访问在flutter中另一个有状态小部件中的一个有状态小部件中创建的对象 - how to access an object created in one stateful widget in another stateful widget in flutter 如何在 Flutter 中从另一个有状态小部件更改一个有状态小部件的状态? - How to change state of one Stateful Widget from another Stateful Widget in Flutter? 如何从有状态小部件 class Flutter 访问小部件 state - How to access a widgets state from a stateful widget class Flutter 如何在 Flutter 中将事件从一个有状态小部件广播到另一个小部件 - How To Broadcast An Event From One Stateful Widget To Another In Flutter 如何将子有状态小部件的索引单选按钮值返回到 Flutter 中的另一个有状态小部件 - how to return index radio button value from children Stateful widgets to another stateful widget in Flutter 我如何正确地将变量从有状态小部件初始化为 flutter 中的另一个小部件并维护 state? - How do i correctly initialise a variable from a stateful widget to another widget in flutter and maintain state? 如何将数据从子状态小部件传递到 Flutter 中的父小部件 - How to pass data from a child Stateful widget to Parent Widget in Flutter Flutter,如何从返回的 Widget 调用 Stateful Widget 内部的函数? - Flutter, how to call a function inside Stateful Widget from a returned Widget?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM