Flutter 如何访问内部小部件';是否从外部窗口小部件创建动画控制器(或状态)?
所以在过去的几天里我一直在尝试颤振和飞镖。 这件事我已经坚持了一天多了,所以我在这里 我有一个报警屏幕,里面有两个对象。一个是DragableMoonWidget,另一个是RisingUnwidget 目前,RisingUnwidget从底部动画显示在屏幕上,而DragableMoonWidget可以通过触摸拖动 我想要实现的是,当拖动DragableMoonWidget时,RisingUnwidget的动画将停止并更改。所以我有了月球闪光装置并在工作,但我仍然无法理解它。(当前将侦听器调回AlarmScreen,然后呢?) 我已经尝试了很多方法来实现这一点,从那时起,所有的方法都被删除了,因为没有一种有效 TLDR 当用户触摸DragableMoonWidget时,如何控制RisingUnwidget的动画控制器,例如,我想停止控制器并将其动画化到不同的点 飞镖/颤振的最佳方法是什么 报警屏幕Flutter 如何访问内部小部件';是否从外部窗口小部件创建动画控制器(或状态)?,flutter,Flutter,所以在过去的几天里我一直在尝试颤振和飞镖。 这件事我已经坚持了一天多了,所以我在这里 我有一个报警屏幕,里面有两个对象。一个是DragableMoonWidget,另一个是RisingUnwidget 目前,RisingUnwidget从底部动画显示在屏幕上,而DragableMoonWidget可以通过触摸拖动 我想要实现的是,当拖动DragableMoonWidget时,RisingUnwidget的动画将停止并更改。所以我有了月球闪光装置并在工作,但我仍然无法理解它。(当前将侦听器调回Al
import 'package:flutter/widgets.dart';
import 'package:moonworshiper_app/backgrounds.dart';
import 'package:moonworshiper_app/ui/alarm/moon_draggable.dart';
import 'package:moonworshiper_app/ui/alarm/rising_sun.dart';
class AlarmScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new _AlarmScreenState();
}
}
class _AlarmScreenState extends State<AlarmScreen> {
bool _moonWasTouched = false;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return new Stack(
children: <Widget>[
new DraggableMoonWidget(new MoonDragListener(this)),
new LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return new RisingSunWidget(constraints.heightConstraints().maxHeight, _moonWasTouched);
})
],
);
}
void _refreshSun() {
setState(() {
_moonWasTouched = true;
});
}
}
class MoonDragListener {
_AlarmScreenState state;
MoonDragListener(this.state);
void onMoonDragStarted() {
state._refreshSun();
}
}
import'package:flatter/widgets.dart';
导入“套装:月亮崇拜者应用程序/backgrounds.dart”;
导入“包:月亮崇拜者应用程序/ui/alarm/moon_Dragable.dart”;
导入“软件包:月亮崇拜者应用程序/ui/alarm/rising_sun.dart”;
类AlarmScreen扩展StatefulWidget{
@凌驾
状态createState(){
返回新的_AlarmScreenState();
}
}
类_AlarmScreenState扩展状态{
bool_moonwasstouch=false;
@凌驾
void initState(){
super.initState();
}
@凌驾
小部件构建(构建上下文){
返回新堆栈(
儿童:[
新的DragableMoonWidget(新的MoonDragListener(本)),
新布局生成器(
生成器:(BuildContext上下文,BoxConstraints){
返回新的RisingUnwidget(constraints.heightConstraints().maxHeight,_moonWasTouched);
})
],
);
}
void_refreshSun(){
设置状态(){
_moonwaspothed=真;
});
}
}
月牙石{
_报警屏幕状态;
月球冰川(本州);
void onmoodragstarted(){
陈述;
}
}
DraggableMoonWidget
import 'package:flutter/widgets.dart';
import 'package:moonworshiper_app/ui/alarm/alarm_screen.dart';
class DraggableMoonWidget extends StatefulWidget {
final MoonDragListener moonStartListener;
DraggableMoonWidget(this.moonStartListener);
State<StatefulWidget> createState() => new _DraggableMoonState();
}
class _DraggableMoonState extends State<DraggableMoonWidget>
with TickerProviderStateMixin {
final moonDragTween = new Tween<Offset>(
begin: new Offset(0.0, -0.5),
end: new Offset(0.0, 0.5),
);
var moonAnimListener;
AnimationController _animationController;
Animation<Offset> _dragAnimation;
AnimationController _dragAnimationController;
bool isFirstDraw = true;
@override
initState() {
super.initState();
_animationController = new AnimationController(
vsync: this,
duration: const Duration(milliseconds: 3000),
);
_dragAnimationController = new AnimationController(vsync: this);
moonAnimListener = (AnimationStatus status) {
if (status == AnimationStatus.dismissed) {
_animationController.forward();
} else if (status == AnimationStatus.completed) {
_animationController.reverse();
} else if (status == AnimationStatus.forward) {}
};
_dragAnimation = moonDragTween.animate(new CurvedAnimation(
parent: _dragAnimationController,
curve: Curves.easeInOut,
reverseCurve: Curves.easeInOut));
_dragAnimationController.animateTo(0.5, duration: new Duration());
_animationController.addStatusListener(moonAnimListener);
_animationController.forward();
}
@override
Widget build(BuildContext context) {
return new Container(
child: new Center(
child: new SlideTransition(
position: _dragAnimation,
child: new GestureDetector(
child: new Image.asset(
"assets/moon.png",
width: 280.0,
height: 280.0,
),
onVerticalDragStart: (DragStartDetails details) {
print("start:" + details.globalPosition.toString());
_animationController.removeStatusListener(moonAnimListener);
_animationController.stop();
_dragStartDetails = details;
_dragAnimationController.animateTo(0.5,
duration: new Duration(milliseconds: 50));
if (isFirstDraw) {
isFirstDraw = false;
widget.moonStartListener.onMoonDragStarted();
}
},
),
),
),
// margin: new EdgeInsets.only(top: 48.0),
);
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
}
import 'package:flutter/widgets.dart';
class RisingSunWidget extends StatefulWidget {
// needed to calculate the offset map
final double screenHeight;
// that's how we know if the use touched the moon
final bool moonWasTouched;
RisingSunWidget(this.screenHeight, this.moonWasTouched);
@override
State<StatefulWidget> createState() {
return new RisingSunState();
}
}
class RisingSunState extends State<RisingSunWidget> with TickerProviderStateMixin {
AnimationController _animationController;
Animation<Offset> _sunAnimation;
final double sunSize = 320.0;
@override
initState() {
super.initState();
_animationController = new AnimationController(
vsync: this,
duration: const Duration(milliseconds: 6000),
);
// how many suns fit in the height of our screen
assert(widget.screenHeight > sunSize);
double sunsInHeight = widget.screenHeight / sunSize;
print(sunsInHeight.toString() + " suns could fit on the user's screen");
var sunsPlusMargins = sunsInHeight + 1; // required margins
final moonTween = new Tween<Offset>(
begin: new Offset(0.0, -0.5 * sunsPlusMargins),
end: new Offset(0.0, 0.5 * sunsPlusMargins), //move by 8% of height max
);
_sunAnimation = moonTween.animate(new CurvedAnimation(
parent: _animationController,
curve: Curves.easeInOut,
reverseCurve: Curves.easeInOut,
));
if (widget.moonWasTouched) {
_animationController.stop();
_animationController.animateTo(0.68,
duration: new Duration(milliseconds: 2000));
} else {
_animationController.animateTo(0.88,
duration: new Duration(milliseconds: 0));
_animationController.animateTo(0.75,
duration: new Duration(milliseconds: 15000));
}
}
@override
Widget build(BuildContext context) {
return new Center(
child: new SlideTransition(
position: _sunAnimation,
child: new Image.asset(
"assets/sun.png",
width: sunSize,
height: sunSize,
),
),
);
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
}
import'package:flatter/widgets.dart';
导入“软件包:月亮崇拜者应用程序/ui/alarm/alarm_screen.dart”;
类DragableMoonWidget扩展StatefulWidget{
最终登月者登月者;
DragableMoonWidget(this.moonStartListener);
State createState()=>new _DragableMoonState();
}
类_DragableMoonState扩展状态
使用TickerProviderStateMixin{
最终月球德拉特温=新的特温(
开始:新偏移(0.0,-0.5),
结束:新偏移量(0.0,0.5),
);
var-MoonAnimalistener;
AnimationController _AnimationController;
动画组织;
动画控制器(dragAnimationController);;
bool isFirstDraw=true;
@凌驾
initState(){
super.initState();
_animationController=新的animationController(
vsync:这个,,
持续时间:常数持续时间(毫秒:3000),
);
_dragAnimationController=新的AnimationController(vsync:this);
moonAnimListener=(AnimationStatus状态){
如果(状态==AnimationStatus.discomered){
_animationController.forward();
}else if(状态==AnimationStatus.completed){
_animationController.reverse();
}else如果(status==AnimationStatus.forward){}
};
_dragAnimation=moonDragTween.animate(新曲线动画)(
父项:_dragAnimationController,
曲线:Curves.easeInOut,
反向曲线:曲线;
_animateTo(0.5,持续时间:new duration());
_animationController.addStatusListener(MoonAnimamListener);
_animationController.forward();
}
@凌驾
小部件构建(构建上下文){
退回新货柜(
孩子:新中心(
子:新幻灯片转换(
职位:(组织),
儿童:新的手势检测器(
子:新建Image.asset(
“assets/moon.png”,
宽度:280.0,
身高:280.0,
),
onVerticalDragStart:(DragStart详细信息){
打印(“开始:+details.globalPosition.toString());
_animationController.removeStatusListener(MoonAnimaListener);
_animationController.stop();
_dragStartDetails=详细信息;
_dragAnimationController.animateTo(0.5,
持续时间:新的持续时间(毫秒:50));
如果(isFirstDraw){
isFirstDraw=false;
moondragstarted();
}
},
),
),
),
//边距:仅限新边集(顶部:48.0),
);
}
@凌驾
无效处置(){
_animationController.dispose();
super.dispose();
}
}
risingunwidget
import 'package:flutter/widgets.dart';
import 'package:moonworshiper_app/ui/alarm/alarm_screen.dart';
class DraggableMoonWidget extends StatefulWidget {
final MoonDragListener moonStartListener;
DraggableMoonWidget(this.moonStartListener);
State<StatefulWidget> createState() => new _DraggableMoonState();
}
class _DraggableMoonState extends State<DraggableMoonWidget>
with TickerProviderStateMixin {
final moonDragTween = new Tween<Offset>(
begin: new Offset(0.0, -0.5),
end: new Offset(0.0, 0.5),
);
var moonAnimListener;
AnimationController _animationController;
Animation<Offset> _dragAnimation;
AnimationController _dragAnimationController;
bool isFirstDraw = true;
@override
initState() {
super.initState();
_animationController = new AnimationController(
vsync: this,
duration: const Duration(milliseconds: 3000),
);
_dragAnimationController = new AnimationController(vsync: this);
moonAnimListener = (AnimationStatus status) {
if (status == AnimationStatus.dismissed) {
_animationController.forward();
} else if (status == AnimationStatus.completed) {
_animationController.reverse();
} else if (status == AnimationStatus.forward) {}
};
_dragAnimation = moonDragTween.animate(new CurvedAnimation(
parent: _dragAnimationController,
curve: Curves.easeInOut,
reverseCurve: Curves.easeInOut));
_dragAnimationController.animateTo(0.5, duration: new Duration());
_animationController.addStatusListener(moonAnimListener);
_animationController.forward();
}
@override
Widget build(BuildContext context) {
return new Container(
child: new Center(
child: new SlideTransition(
position: _dragAnimation,
child: new GestureDetector(
child: new Image.asset(
"assets/moon.png",
width: 280.0,
height: 280.0,
),
onVerticalDragStart: (DragStartDetails details) {
print("start:" + details.globalPosition.toString());
_animationController.removeStatusListener(moonAnimListener);
_animationController.stop();
_dragStartDetails = details;
_dragAnimationController.animateTo(0.5,
duration: new Duration(milliseconds: 50));
if (isFirstDraw) {
isFirstDraw = false;
widget.moonStartListener.onMoonDragStarted();
}
},
),
),
),
// margin: new EdgeInsets.only(top: 48.0),
);
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
}
import 'package:flutter/widgets.dart';
class RisingSunWidget extends StatefulWidget {
// needed to calculate the offset map
final double screenHeight;
// that's how we know if the use touched the moon
final bool moonWasTouched;
RisingSunWidget(this.screenHeight, this.moonWasTouched);
@override
State<StatefulWidget> createState() {
return new RisingSunState();
}
}
class RisingSunState extends State<RisingSunWidget> with TickerProviderStateMixin {
AnimationController _animationController;
Animation<Offset> _sunAnimation;
final double sunSize = 320.0;
@override
initState() {
super.initState();
_animationController = new AnimationController(
vsync: this,
duration: const Duration(milliseconds: 6000),
);
// how many suns fit in the height of our screen
assert(widget.screenHeight > sunSize);
double sunsInHeight = widget.screenHeight / sunSize;
print(sunsInHeight.toString() + " suns could fit on the user's screen");
var sunsPlusMargins = sunsInHeight + 1; // required margins
final moonTween = new Tween<Offset>(
begin: new Offset(0.0, -0.5 * sunsPlusMargins),
end: new Offset(0.0, 0.5 * sunsPlusMargins), //move by 8% of height max
);
_sunAnimation = moonTween.animate(new CurvedAnimation(
parent: _animationController,
curve: Curves.easeInOut,
reverseCurve: Curves.easeInOut,
));
if (widget.moonWasTouched) {
_animationController.stop();
_animationController.animateTo(0.68,
duration: new Duration(milliseconds: 2000));
} else {
_animationController.animateTo(0.88,
duration: new Duration(milliseconds: 0));
_animationController.animateTo(0.75,
duration: new Duration(milliseconds: 15000));
}
}
@override
Widget build(BuildContext context) {
return new Center(
child: new SlideTransition(
position: _sunAnimation,
child: new Image.asset(
"assets/sun.png",
width: sunSize,
height: sunSize,
),
),
);
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
}
import'package:flatter/widgets.dart';
类RisingUnwidget扩展StatefulWidget{
//需要计算偏移贴图
最终双屏幕高度;
//这就是我们如何知道是否使用了月球
最后一个布尔月亮被触碰了;
RisingUnwidget(这个屏幕高度,这个月亮被触摸);
@凌驾
状态createState(){
返回新的RisingSunState();
}
}
类RisingSunState使用TickerProviderStateMixin扩展状态{
AnimationController _AnimationController;
动画(sun Animation),;
最终双太阳尺寸=320.0;
@凌驾
initState(){
super.initState();
_animationController=新的animationController(
vsync:这个,,
持续时间:常数持续时间(毫秒:6000),
);
//多少个太阳适合我们屏幕的高度
断言(widget.screenHeight>