[英]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.