Flutter 我怎样才能做一个;“出现”;颤振中的文本动画?
我希望实现material.io页面中显示的行为: 第一次显示文本时,文本会进行很好的转换Flutter 我怎样才能做一个;“出现”;颤振中的文本动画?,flutter,Flutter,我希望实现material.io页面中显示的行为: 第一次显示文本时,文本会进行很好的转换 如何在颤振中实现这一点?您可以编写如下小部件: import 'dart:async'; import 'package:flutter/material.dart'; class ShowUp extends StatefulWidget { final Widget child; final int delay; ShowUp({@required this.child, this.
如何在颤振中实现这一点?您可以编写如下小部件:
import 'dart:async';
import 'package:flutter/material.dart';
class ShowUp extends StatefulWidget {
final Widget child;
final int delay;
ShowUp({@required this.child, this.delay});
@override
_ShowUpState createState() => _ShowUpState();
}
class _ShowUpState extends State<ShowUp> with TickerProviderStateMixin {
AnimationController _animController;
Animation<Offset> _animOffset;
@override
void initState() {
super.initState();
_animController =
AnimationController(vsync: this, duration: Duration(milliseconds: 500));
final curve =
CurvedAnimation(curve: Curves.decelerate, parent: _animController);
_animOffset =
Tween<Offset>(begin: const Offset(0.0, 0.35), end: Offset.zero)
.animate(curve);
if (widget.delay == null) {
_animController.forward();
} else {
Timer(Duration(milliseconds: widget.delay), () {
_animController.forward();
});
}
}
@override
void dispose() {
super.dispose();
_animController.dispose();
}
@override
Widget build(BuildContext context) {
return FadeTransition(
child: SlideTransition(
position: _animOffset,
child: widget.child,
),
opacity: _animController,
);
}
}
int delayAmount = 500;
...........
...........
...........
Column(
children: <Widget>[
ShowUp(
child: Text("The first texto to be shown"),
delay: delayAmount,
),
ShowUp(
child: Text("The text below the first"),
delay: delayAmount + 200,
),
ShowUp(
child: Column(
children: <Widget>[
Text("Texts together 1"),
Text("Texts together 2"),
Text("Texts together 3"),
],
),
delay: delayAmount + 400,
),
],
),
导入'dart:async';
进口“包装:颤振/材料.省道”;
类ShowUp扩展了StatefulWidget{
最后一个孩子;
最终整数延迟;
显示({@required this.child,this.delay});
@凌驾
_ShowUpState createState()=>\u ShowUpState();
}
类_ShowUpState使用TickerProviderStateMixin扩展状态{
AnimationController\u AnimationController;
动画;;
@凌驾
void initState(){
super.initState();
_动物控制器=
AnimationController(vsync:this,duration:duration(毫秒:500));
最终曲线=
曲线动画(曲线:Curves.Decreate,父对象:_animController);
_动物补偿=
吐温(开始:常数偏移(0.0,0.35),结束:偏移.0)
.制作(曲线)动画;
if(widget.delay==null){
_animController.forward();
}否则{
计时器(持续时间(毫秒:widget.delay),(){
_animController.forward();
});
}
}
@凌驾
无效处置(){
super.dispose();
_animController.dispose();
}
@凌驾
小部件构建(构建上下文){
返回衰减转换(
子:幻灯片转换(
位置:,
child:widget.child,
),
不透明度:,
);
}
}
然后你可以这样使用它:
import 'dart:async';
import 'package:flutter/material.dart';
class ShowUp extends StatefulWidget {
final Widget child;
final int delay;
ShowUp({@required this.child, this.delay});
@override
_ShowUpState createState() => _ShowUpState();
}
class _ShowUpState extends State<ShowUp> with TickerProviderStateMixin {
AnimationController _animController;
Animation<Offset> _animOffset;
@override
void initState() {
super.initState();
_animController =
AnimationController(vsync: this, duration: Duration(milliseconds: 500));
final curve =
CurvedAnimation(curve: Curves.decelerate, parent: _animController);
_animOffset =
Tween<Offset>(begin: const Offset(0.0, 0.35), end: Offset.zero)
.animate(curve);
if (widget.delay == null) {
_animController.forward();
} else {
Timer(Duration(milliseconds: widget.delay), () {
_animController.forward();
});
}
}
@override
void dispose() {
super.dispose();
_animController.dispose();
}
@override
Widget build(BuildContext context) {
return FadeTransition(
child: SlideTransition(
position: _animOffset,
child: widget.child,
),
opacity: _animController,
);
}
}
int delayAmount = 500;
...........
...........
...........
Column(
children: <Widget>[
ShowUp(
child: Text("The first texto to be shown"),
delay: delayAmount,
),
ShowUp(
child: Text("The text below the first"),
delay: delayAmount + 200,
),
ShowUp(
child: Column(
children: <Widget>[
Text("Texts together 1"),
Text("Texts together 2"),
Text("Texts together 3"),
],
),
delay: delayAmount + 400,
),
],
),
int delayAmount=500;
...........
...........
...........
纵队(
儿童:[
展示(
子项:文本(“要显示的第一个文本”),
延迟:延迟金额,
),
展示(
子项:文本(“第一个下面的文本”),
延迟:延迟金额+200,
),
展示(
子:列(
儿童:[
正文(“正文合1”),
文本(“合并文本2”),
文本(“文本加在一起3”),
],
),
延迟:延迟金额+400,
),
],
),
请注意,这个“展示”小部件可以制作任何东西的动画,而不仅仅是文本。有一个现有的程序包需要它,它基于下面的实现 这是一个提供此动画的通用小部件。您所要做的就是将您的小部件(是的,任何小部件)包装在SlideFadeTransition小部件中,瞧 它提供了很多控制。例如,可以控制动画的数量、速度、方向、延迟甚至曲线
///Wrapper class to implement slide and fade animations at the same time to
///a given element. Wrap the widget that you wish to appear with slide-fade
///transition in this class.
import 'dart:async';
import 'package:flutter/material.dart';
enum Direction { vertical, horizontal }
class SlideFadeTransition extends StatefulWidget {
///The child on which to apply the given [SlideFadeTransition]
final Widget child;
///The offset by which to slide and [child] into view from [Direction].
///Defaults to 0.2
final double offset;
///The curve used to animate the [child] into view.
///Defaults to [Curves.easeIn]
final Curve curve;
///The direction from which to animate the [child] into view. [Direction.horizontal]
///will make the child slide on x-axis by [offset] and [Direction.vertical] on y-axis.
///Defaults to [Direction.vertical]
final Direction direction;
///The delay with which to animate the [child]. Takes in a [Duration] and
/// defaults to 0.0 seconds
final Duration delayStart;
///The total duration in which the animation completes. Defaults to 800 milliseconds
final Duration animationDuration;
SlideFadeTransition({
@required this.child,
this.offset = 0.2,
this.curve = Curves.easeIn,
this.direction = Direction.vertical,
this.delayStart = const Duration(seconds: 0),
this.animationDuration = const Duration(milliseconds: 800),
});
@override
_SlideFadeTransitionState createState() => _SlideFadeTransitionState();
}
class _SlideFadeTransitionState extends State<SlideFadeTransition>
with SingleTickerProviderStateMixin {
Animation<Offset> _animationSlide;
AnimationController _animationController;
Animation<double> _animationFade;
@override
void initState() {
super.initState();
_animationController = AnimationController(
vsync: this,
duration: widget.animationDuration,
);
//configure the animation controller as per the direction
if (widget.direction == Direction.vertical) {
_animationSlide =
Tween<Offset>(begin: Offset(0, widget.offset), end: Offset(0, 0))
.animate(CurvedAnimation(
curve: widget.curve,
parent: _animationController,
));
} else {
_animationSlide =
Tween<Offset>(begin: Offset(widget.offset, 0), end: Offset(0, 0))
.animate(CurvedAnimation(
curve: widget.curve,
parent: _animationController,
));
}
_animationFade =
Tween<double>(begin: -1.0, end: 1.0).animate(CurvedAnimation(
curve: widget.curve,
parent: _animationController,
));
Timer(widget.delayStart, () {
_animationController.forward();
});
}
@override
Widget build(BuildContext context) {
return FadeTransition(
opacity: _animationFade,
child: SlideTransition(
position: _animationSlide,
child: widget.child,
),
);
}
}
///同时实现幻灯片和淡入淡出动画的包装器类
///给定的元素。用幻灯片淡入淡出包装您希望显示的小部件
///这门课的过渡。
导入“dart:async”;
进口“包装:颤振/材料.省道”;
枚举方向{垂直,水平}
类SlideFadeTransition扩展StatefulWidget{
///要对其应用给定[SlideFadeTransition]的子项
最后一个孩子;
///从[方向]滑动和[子]到视图的偏移量。
///默认值为0.2
最终双偏移量;
///用于在视图中设置[子对象]动画的曲线。
///默认为[Curves.easeIn]
最终曲线;
///将[子对象]设置为视图动画的方向。[方向.水平]
///将使子对象在x轴上滑动[offset],在y轴上滑动[Direction.vertical]。
///默认为[方向.垂直]
最终方向;
///设置[child]动画的延迟时间为[Duration],并且
///默认值为0.0秒
最终持续时间延迟开始;
///动画完成的总持续时间。默认为800毫秒
最终持续时间动画持续时间;
幻灯片格式转换({
@需要这个孩子,
该偏移量=0.2,
this.curve=Curves.easeIn,
this.direction=direction.vertical,
this.delayStart=常数持续时间(秒:0),
this.animationDuration=常量持续时间(毫秒:800),
});
@凌驾
_SlideFadeTransitionState createState()=>\u SlideFadeTransitionState();
}
类SlideFadeTransitionState扩展了状态
使用SingleTickerProviderStateMixin{
动画_animationSlide;
AnimationController _AnimationController;
动画-动画淡入淡出;
@凌驾
void initState(){
super.initState();
_animationController=animationController(
vsync:这个,,
持续时间:widget.animationDuration,
);
//根据方向配置动画控制器
if(widget.direction==direction.vertical){
_动画幻灯片=
Tween(开始:偏移量(0,widget.Offset),结束:偏移量(0,0))
.动画(曲线动画)(
曲线:widget.curve,
父项:_animationController,
));
}否则{
_动画幻灯片=
Tween(开始:偏移量(widget.Offset,0),结束:偏移量(0,0))
.动画(曲线动画)(
曲线:widget.curve,
父项:_animationController,
));
}
_动画淡入淡出=
吐温(开始:-1.0,结束:1.0)。动画(曲线动画)(
曲线:widget.curve,
父项:_animationController,
));
计时器(widget.delayStart,(){
_animationController.forward();
});
}
@凌驾
小部件构建(构建上下文){
返回衰减转换(
不透明度:_animationFade,
子:幻灯片转换(
位置:_animationSlide,
child:widget.child,
),
);
}
}
要使用它,您只需使用SlideFadeTransition小部件包装文本小部件或任何相关小部件。下面是一个完整的工作示例
import 'dart:async';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Show up Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Show up Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SlideFadeTransition(
child: Text(
'You have pushed the button this many times:',
),
),
SlideFadeTransition(
delayStart: Duration(milliseconds: 800),
child: Text(
'0',
style: Theme.of(context).textTheme.display1,
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {});
},
tooltip: 'Animate',
child: Icon(Icons.add),
),
);
}
}
///Wrapper class to implement slide and fade animations at the same time to
///a given element. Wrap the widget that you wish to appear with slide-fade
///transition in this class.
enum Direction { vertical, horizontal }
class SlideFadeTransition extends StatefulWidget {
///The child on which to apply the given [SlideFadeTransition]
final Widget child;
///The offset by which to slide and [child] into view from [Direction].
///Defaults to 1.0
final double offset;
///The curve used to animate the [child] into view.
///Defaults to [Curves.easeIn]
final Curve curve;
///The direction from which to animate the [child] into view. [Direction.horizontal]
///will make the child slide on x-axis by [offset] and [Direction.vertical] on y-axis.
///Defaults to [Direction.vertical]
final Direction direction;
///The delay with which to animate the [child]. Takes in a [Duration] and
/// defaults to 0.0 seconds
final Duration delayStart;
///The total duration in which the animation completes. Defaults to 800 milliseconds
final Duration animationDuration;
SlideFadeTransition({
@required this.child,
this.offset = 1.0,
this.curve = Curves.easeIn,
this.direction = Direction.vertical,
this.delayStart = const Duration(seconds: 0),
this.animationDuration = const Duration(milliseconds: 800),
});
@override
_SlideFadeTransitionState createState() => _SlideFadeTransitionState();
}
class _SlideFadeTransitionState extends State<SlideFadeTransition>
with SingleTickerProviderStateMixin {
Animation<Offset> _animationSlide;
AnimationController _animationController;
Animation<double> _animationFade;
@override
void initState() {
super.initState();
_animationController = AnimationController(
vsync: this,
duration: widget.animationDuration,
);
//configure the animation controller as per the direction
if (widget.direction == Direction.vertical) {
_animationSlide =
Tween<Offset>(begin: Offset(0, widget.offset), end: Offset(0, 0))
.animate(CurvedAnimation(
curve: widget.curve,
parent: _animationController,
));
} else {
_animationSlide =
Tween<Offset>(begin: Offset(widget.offset, 0), end: Offset(0, 0))
.animate(CurvedAnimation(
curve: widget.curve,
parent: _animationController,
));
}
_animationFade =
Tween<double>(begin: -1.0, end: 1.0).animate(CurvedAnimation(
curve: widget.curve,
parent: _animationController,
));
Timer(widget.delayStart, () {
_animationController.forward();
});
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return FadeTransition(
opacity: _animationFade,
child: SlideTransition(
position: _animationSlide,
child: widget.child,
),
);
}
}
导入'dart:async';
进口“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文)