我小时候有一个 Stack,其中包含 5 个 Text。每个 child 都包裹着一个 FadeTransition 对象。在 Stack 之外,我有 5 个 RaisedButton,每个都映射到一个 Text。默认情况下,Text 1 的不透明度为 1,其余的不透明度为 0。单击按钮时,它映射的文本的不透明度变为 1,其余变为 0。
为此,我创建了 5 个不同的 AnimationController 并硬编码了一个很长的逻辑。 我不确定这是在 Flutter 中执行此操作的正确方法。
我相信一定有更简单的方法。
此外,这是一个简化的示例。我的实际应用中的问题甚至有复杂的条件。 (例如,当单击 Button 5 且 bool 值 hasViewedText1 为 true 时,仅显示 Text 2 和 Text 3。)
import 'dart:core';
import 'package:flutter/material.dart';
class Test extends StatefulWidget {
@override
_State createState() {
return _State();
}
}
class _State extends State<Test> with TickerProviderStateMixin {
AnimationController _animationController1;
AnimationController _animationController2;
AnimationController _animationController3;
AnimationController _animationController4;
AnimationController _animationController5;
@override
void initState() {
super.initState();
_animationController1 = new AnimationController(
vsync: this,
duration: new Duration(seconds: 1),
);
_animationController2 = new AnimationController(
vsync: this,
duration: new Duration(seconds: 1),
);
_animationController3 = new AnimationController(
vsync: this,
duration: new Duration(seconds: 1),
);
_animationController4 = new AnimationController(
vsync: this,
duration: new Duration(seconds: 1),
);
_animationController5 = new AnimationController(
vsync: this,
duration: new Duration(seconds: 1),
);
_everyThingBackward();
_animationController1.forward();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
elevation: 0.5,
title: new Text(
"Testing views",
style: Theme.of(context).textTheme.title,
),
),
// body: _buildBodyAnimationTest(),
// body: _buildTuto(),
// body: _builtLayoutBuilder(),
body: _builtLayoutConditionalAnimation(),
);
}
Widget _builtLayoutConditionalAnimation() {
return new Column(
children: <Widget>[
new Column(
children: <Widget>[
new RaisedButton(child: new Text("Button 1"), onPressed: () {
_everyThingBackward();
_animationController1.forward();
}),
new RaisedButton(child: new Text("Button 2"), onPressed: () {
_everyThingBackward();
_animationController2.forward();
}),
new RaisedButton(child: new Text("Button 3"), onPressed: () {
_everyThingBackward();
_animationController3.forward();
}),
new RaisedButton(child: new Text("Button 4"), onPressed: () {
_everyThingBackward();
_animationController4.forward();
}),
new RaisedButton(child: new Text("Button 5"), onPressed: () {
_everyThingBackward();
_animationController5.forward();
}),
],
),
new Stack(
children: <Widget>[
FadeTransition(opacity: _animationController1,child: new Text('Text 1 is clicked')),
FadeTransition(opacity: _animationController2,child: new Text('Text 2 is clicked')),
FadeTransition(opacity: _animationController3,child: new Text('Text 3 is clicked')),
FadeTransition(opacity: _animationController4,child: new Text('Text 4 is clicked')),
FadeTransition(opacity: _animationController5,child: new Text('Text 5 is clicked')),
],
),
],
);
}
void _everyThingBackward() {
_animationController1.reverse();
_animationController2.reverse();
_animationController3.reverse();
_animationController4.reverse();
_animationController5.reverse();
}
}
最佳答案
这可以通过使用 AnimatedSwitcher 小部件变得更简单,link to docs .
这是一个完整的例子:
import 'package:flutter/material.dart';
void main() => runApp(App());
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(child: Center(child: Test())),
),
);
}
}
class Test extends StatefulWidget {
@override
_TestState createState() => _TestState();
}
class _TestState extends State<Test> {
Widget _child = Text(
"No Button Tapped!",
key: UniqueKey(),
);
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
RaisedButton(
child: Text("Button 1"),
onPressed: () {
setState(() {
_child = Text(
"Button 1 Tapped!",
key: UniqueKey(),
);
});
},
),
RaisedButton(
child: Text("Button 2"),
onPressed: () {
setState(() {
_child = Text(
"Button 2 Tapped!",
key: UniqueKey(),
);
});
},
),
RaisedButton(
child: Text("Button 3"),
onPressed: () {
setState(() {
_child = Text(
"Button 3 Tapped!",
key: UniqueKey(),
);
});
},
),
AnimatedSwitcher(
duration: Duration(milliseconds: 200),
child: _child,
),
],
);
}
}
这篇中型文章也可能有用:https://medium.com/flutter-community/what-do-you-know-about-aniamtedswitcher-53cc3a4bebb8
关于animation - Flutter 条件动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54513606/
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano
Unity自动旋转动画1.开门需要门把手先动,门再动2.关门需要门先动,门把手再动3.中途播放过程中不可以再次进行操作觉得太复杂?查看我的文章开关门简易进阶版效果:如果这个门可以直接打开的话,就不需要放置"门把手"如果门把手还有钥匙需要旋转,那就可以把钥匙放在门把手的"门把手",理论上是可以无限套娃的可调整参数有:角度,反向,轴向,速度运行时点击Test进行测试自己写的代码比较垃圾,命名与结构比较拉,高手轻点喷,新手有类似的需求可以拿去做参考上代码usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;u
我的Gallery模型中有以下查询:media_items.includes(:photo,:video).rank(:position_in_gallery)我的图库模型有_许多媒体项,每个都有一个照片或视频关联。到目前为止,一切正常。它返回所有media_items包括它们的photo或video关联,由media_item的position_in_gallery属性排序。但是我现在需要将此查询返回的照片限制为仅具有is_processing属性的照片,即nil。是否可以进行相同的查询,但条件是返回的照片等同于:.where(photo:'photo.is_processingIS
除了可访问性标准不鼓励使用这一事实指向当前页面的链接,我应该怎么做重构以下View代码?#navigation%ul.tabbed-ifcurrent_page?(new_profile_path)%li{:class=>"current_page_item"}=link_tot("new_profile"),new_profile_path-else%li=link_tot("new_profile"),new_profile_path-ifcurrent_page?(profiles_path)%li{:class=>"current_page_item"}=link_tot("p
我正在尝试按Rails相关模型中的字段进行排序。我研究的所有解决方案都没有解决如果相关模型被另一个参数过滤?元素模型classItem相关模型:classPriority我正在使用where子句检索项目:@items=Item.where('company_id=?andapproved=?',@company.id,true).all我需要按相关表格中的“位置”列进行排序。问题在于,在优先级模型中,一个项目可能会被多家公司列出。因此,这些职位取决于他们拥有的company_id。当我显示项目时,它是针对一个公司的,按公司内的职位排序。完成此任务的正确方法是什么?感谢您的帮助。PS-我
基本上,我只是试图在满足特定条件时停止程序运行其余行。unlessraw_information.firstputs"Noresultswerereturnedforthatquery"breakend然而,在程序运行之前我得到了这个错误:Invalidbreakcompileerror(SyntaxError)执行此操作的正确方法是什么? 最佳答案 abort("Noresultswerereturnedforthatquery")unlesscondition或unlessconditionabort("Noresultswer
如果用户是所有者,我有一个条件来检查说删除和文章。delete_articleifuser.owner?另一种方式是user.owner?&&delete_article选择它有什么好处还是它只是一种写作风格 最佳答案 性能不太可能成为该声明的问题。第一个要好得多-它更容易阅读。您future的自己和其他将开始编写代码的人会为此感谢您。 关于ruby-on-rails-如果条件与&&,是否有任何性能提升,我们在StackOverflow上找到一个类似的问题:
我有一个简单的问题,但我无法解决这个问题。我的字符串格式为ID:dddd,具有以下正则表达式:/^ID:([a-z0-9]*)$/或者如下:ID:1234Status:232,所以用下面的正则表达式:/^ID:([a-z0-9]*)Status:([a-z0-9]*)$/现在我想制作一个可以处理两者的正则表达式。我想到的第一件事是:/^ID:([a-z0-9]*)$|^ID:([a-z0-9]*)Status:([a-z0-9]*)$/它匹配,但我正在研究条件正则表达式,并认为应该可以按照(伪代码)ifthestringcontains/Status://^ID:([a-z0-9]*)