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

时间:2020-01-22 10:36:09

标签: flutter flutter-animation

我希望计数器小部件显示家onTap上的widget。另外,每次点击都从animation开始。

计数器小工具

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

1 个答案:

答案 0 :(得分:3)

您需要定义一个GlobalKey并将其传递给子窗口小部件。这将使您能够访问该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,
    );
  }
}