简体   繁体   English

Flutter 如何从另一个小部件访问 AnimationController 方法

[英]Flutter how to access AnimationController methods from another widget

i am building a chess clock app in flutter.我正在 flutter 中构建一个国际象棋时钟应用程序。 i have a homepage with two container widgets called blackBox and whiteBox they both display timers.我有一个主页,其中包含两个名为 blackBox 和 whiteBox 的容器小部件,它们都显示计时器。 i am using animation Controllers on both widgets to control the timers.我在两个小部件上使用 animation 控制器来控制计时器。

what i like to do is when i tap on white container i want to stop whiteController animation and start blackController animation, vice versa for black container.我喜欢做的是当我点击白色容器时,我想停止 whiteController animation 并启动 blackController animation,反之亦然黑色容器。 but i dont know how to access whiteController and its method inside blackBox widget.但我不知道如何访问 whiteController 及其在 blackBox 小部件中的方法。 i am sharing a minimal version of my code below我在下面分享我的代码的最小版本

HomePage主页


class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        BlackBox(),
        WhiteBox(),
      ],
    );
  }
}

BlackBox widget黑盒小部件


class BlackBox extends StatefulWidget {
  const BlackBox({
    Key key,
  }) : super(key: key);

  @override
  _BlackBoxState createState() => _BlackBoxState();
}

class _BlackBoxState extends State<BlackBox>
    with SingleTickerProviderStateMixin {
  AnimationController blackController;
  @override
  void initState() {
    blackController = AnimationController(
      vsync: this,
      duration: Duration(seconds: 60),
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: GestureDetector(
        onTap: () {
          blackController.forward(from: blackController.value);
        },
        child: Container(
          color: Colors.amber,
        ),
      ),
    );
  }
}

WhiteBox Widget白盒小部件


class WhiteBox extends StatefulWidget {
  const WhiteBox({
    Key key,
  }) : super(key: key);

  @override
  _WhiteBoxState createState() => _WhiteBoxState();
}

class _WhiteBoxState extends State<WhiteBox> with TickerProviderStateMixin {
  AnimationController whiteController;
  @override
  void initState() {
    whiteController = AnimationController(
      vsync: this,
      duration: Duration(seconds: 60),
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: GestureDetector(
        onTap: () {
          whiteController.forward(from: whiteController.value);
        },
        child: Container(
          color: Colors.red,
        ),
      ),
    );
  }
}

I don't know this is the best solution.我不知道这是最好的解决方案。 But you can achieve your case in this way.但是您可以通过这种方式实现您的案例。

Find the code snippets below:找到下面的代码片段:

import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
  AnimationController _blackController;
  AnimationController _whiteController;

  @override
  void initState() {
    _blackController = AnimationController(
      vsync: this,
      duration: Duration(seconds: 60),
    );
    _whiteController = AnimationController(
      vsync: this,
      duration: Duration(seconds: 60),
    );
    _blackController.addListener(_rebuild);
    _whiteController.addListener(_rebuild);
    super.initState();
  }

  void _rebuild() {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Column(
        children: [
          BlackBox(
            whiteController: _whiteController,
            blackController: _blackController,
          ),
          WhiteBox(
            whiteController: _whiteController,
            blackController: _blackController,
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    _blackController.removeListener(_rebuild);
    _whiteController.removeListener(_rebuild);
    super.dispose();
  }
}

Black Box:黑盒子:


class BlackBox extends StatefulWidget {
  const BlackBox({Key key, this.blackController, this.whiteController})
      : super(key: key);

  final AnimationController blackController;
  final AnimationController whiteController;

  @override
  _BlackBoxState createState() => _BlackBoxState();
}

class _BlackBoxState extends State<BlackBox> {
  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: GestureDetector(
        onTap: () {
          widget.whiteController.stop();
          widget.blackController.forward(from: widget.blackController.value);
        },
        child: Container(
          color: Colors.amber,
          alignment: Alignment.center,
          child:
              Text(widget.blackController.value.toStringAsFixed(2).toString()),
        ),
      ),
    );
  }
}

White Box:白盒:


class WhiteBox extends StatefulWidget {
  const WhiteBox({
    Key key,
    this.blackController,
    this.whiteController,
  }) : super(key: key);

  final AnimationController blackController;
  final AnimationController whiteController;

  @override
  _WhiteBoxState createState() => _WhiteBoxState();
}

class _WhiteBoxState extends State<WhiteBox> {
  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: GestureDetector(
        onTap: () {
          widget.blackController.stop();
          widget.whiteController.forward(from: widget.whiteController.value);
        },
        child: Container(
          color: Colors.red,
          alignment: Alignment.center,
          child:
              Text(widget.whiteController.value.toStringAsFixed(2).toString()),
        ),
      ),
    );
  }
}

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

相关问题 如何使用 Flutter 和 Dart 中的下拉菜单从另一个小部件更新小部件? - How To Update A Widget from Another Widget with A DropDown in Flutter and Dart? AnimationController 在颤动中平滑停止 - AnimationController smooth stop in flutter 如何在 Flutter 中将事件从一个有状态小部件广播到另一个小部件 - How To Broadcast An Event From One Stateful Widget To Another In Flutter 从 jetpack compose 中的另一个小部件访问小部件 - Access a widget from another widget in jetpack compose 如何在 Flutter 中从另一个有状态小部件更改一个有状态小部件的状态? - How to change state of one Stateful Widget from another Stateful Widget in Flutter? 从另一个小部件 Flutter 更改文本字段的 textSize - Change textSize of textfield from another widget Flutter Flutter - 如何在其他小部件中访问 TextEditingController? - Flutter - How to access TextEditingController in other widget? 如何在Flutter中将Webview嵌入另一个小部件中? - How to embed webview inside another widget in Flutter? Flutter 错误:AnimationController.stop() 在 AnimationController.dispose() 之后调用 - Flutter error: AnimationController.stop() called after AnimationController.dispose() Flutter - 如何从其他小部件访问 DropDown 中当前选定的项目? - Flutter - How can I access current selected item in DropDown from other widget?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM