flutter 动画 practice
import 'package:flutter/material.dart';
import 'dart:io';
import 'dart:async'; main() => runApp(MyApp()); class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: '/slide',
routes: {
'/': (context) => GamePage(),
'/builderAnimation': (context) => BuilderPage(),
'/crossfade': (context) => CrossFadePage(),
'/autofade': (context) => AutoFade(),
'/positiontransition': (context) => PositionTransition(),
'/rotateTransition': (context) => RotateTransition(),
'/scaleTransit': (context) => ScaleTest(),
'/slide': (context) => SlideTest(),
},
);
}
} ////////////////////////////////////////////////////////////////////////////////////////////////
class SlideTest extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return SlideTestState();
}
} class SlideTestState extends State<SlideTest> with SingleTickerProviderStateMixin{
@override
Widget build(BuildContext context) { var t = Tween(begin: Offset(0.0,2.0), end: Offset(0.0,1.0)).animate(AnimationController(vsync: this, duration: Duration(seconds: 2))..forward()); return Scaffold(
appBar: AppBar(title: Text('slide'),),
body: Container(
child: SlideTransition(
child: Container(width: 100,height: 100,color: Colors.orange,),
position: t),
),
);
}
} //////////////////////////////////////////////////////////////////////////////////////////////// class ScaleTest extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return ScaleTransitionState();
}
} class ScaleTransitionState extends State<ScaleTest> with SingleTickerProviderStateMixin{
@override
Widget build(BuildContext context) { Animation t = Tween(begin: 1.0, end: 0.0).animate(AnimationController(vsync: this,duration: Duration(seconds: 2))..forward()); return Scaffold(
appBar: AppBar(title: Text('scale'),),
body: Container(
child: ScaleTransition(
child: Container(width: 100,height: 100,color: Colors.orange,),
scale: t,), ),
);
}
} ////////////////////////////////////////////////////////////////////////////////////////////////
class RotateTransition extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return RotateTransitionState();
}
} class RotateTransitionState extends State<RotateTransition> with SingleTickerProviderStateMixin{
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('rotate'),),
body: Container(
child: RotationTransition(
child: Container(width: 100, height: 100, color: Colors.orange,),
turns: AnimationController(vsync: this,
duration: Duration(seconds: 1),lowerBound: 0, upperBound: 0.1)..forward(), ),
),
);
}
} ////////////////////////////////////////////////////////////////////////////////////////////////
class PositionTransition extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return PositionTransitionState();
}
} class PositionTransitionState extends State<PositionTransition> with SingleTickerProviderStateMixin { @override
Widget build(BuildContext context) { Animatable animatable = RelativeRectTween(begin: RelativeRect.fromLTRB(75, 75, 75, 75),
end: RelativeRect.fromLTRB(100, 100, 75, 75),);
Animation frontRelativeRect = animatable.animate(AnimationController(vsync: this, duration: Duration(seconds: 2))..repeat()); return Scaffold(
appBar: AppBar(title: Text('position transition'),),
body: Container(
child: Stack(
alignment: Alignment.center,
fit: StackFit.loose,
children: <Widget>[
Container(width: 200,height: 200,color: Colors.blue,),
Container(width: 100,height: 100,color: Colors.red,),
PositionedTransition(
rect: frontRelativeRect,
child: Container(width: 100,height: 100, color: Colors.orange,),
),
],
),
),
);
}
} //////////////////////////////////////////////////////////////////////////////////////////////// class AutoFade extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return AutoFadeState();
}
} class AutoFadeState extends State<AutoFade> with SingleTickerProviderStateMixin {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('fade'),),
body: Container(
child: FadeTransition(
opacity: AnimationController(
vsync: this,
duration: Duration(seconds: 1),
)..repeat(),
child: Text(('testing auto fade')),
alwaysIncludeSemantics: true,
),
),
);
}
} ////////////////////////////////////////////////////////////////////////////////////////////////
class CrossFadePage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return CrossFadePageState();
}
} class CrossFadePageState extends State<CrossFadePage> {
bool first = true; @override
Widget build(BuildContext context) {
Timer(Duration(seconds: 2), (){
setState(() {
first = !first;
});
});
return Scaffold(
appBar: AppBar(title: Text('fade'),),
body: AnimatedCrossFade(
firstChild: Text('first'),
secondChild: Text('second'),
crossFadeState: first?CrossFadeState.showFirst:CrossFadeState.showSecond,
duration: Duration(seconds: 1)),
);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
class BuilderPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return BuilderPageState();
}
} class BuilderPageState extends State<BuilderPage> with SingleTickerProviderStateMixin {
@override
Widget build(BuildContext context) {
var controller = AnimationController(duration: Duration(seconds: 2),vsync: this);
var tween = Tween(begin: 20.0, end: 100.0).animate(controller);
return Scaffold(
appBar: AppBar(title: Text('fade'),),
body: AnimatedBuilder(
animation: tween,
child:Text('${tween.value}'),
builder: (context, Widget child){
print(controller.value);
print(tween.value);
return Container(
child: Column(
children: <Widget>[
RaisedButton(child: Text('btn'),onPressed: (){
controller.forward();
},),
Text('${tween.value} from 2'),
child,
],
),
);
}),
);
}
} //////////////////////////////////////////////////////////////////////////////////////////////// class GamePage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return GamePageState();
}
} class GamePageState extends State<GamePage> with TickerProviderStateMixin {
var gameWidth;
var gameHeight;
var brickSide;
List positionedWidgets = [<Widget>[], <Widget>[], <Widget>[], <Widget>[]];
var animation; AnimationController animationController; generatePositionList() {
var top = (gameHeight - gameWidth) / 2.0 + 10.0;
var left = 10.0;
int i = 1;
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
positionedWidgets[y].add(Brick(
id: i,
x: left + (1 + x * 2) * brickSide / 2.0,
y: top + (1 + y * 2) * brickSide / 2.0,
brickSide: brickSide,
value: x + y,
));
i++;
}
}
} @override
void initState() {
super.initState();
animationController = AnimationController(
vsync: this, duration: Duration(seconds: 1));
} @override
Widget build(BuildContext context) {
gameWidth = MediaQuery.of(context).size.width;
gameHeight = MediaQuery.of(context).size.height;
brickSide = (gameWidth - 80) / 4;
generatePositionList(); return SafeArea(
child: Scaffold(
body: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
child: Text(
'GAME 2048',
style: TextStyle(fontSize: 40),
),
), TouchArea(allBricks: positionedWidgets,), Container(
child: Text(
'SCORE:999',
style: TextStyle(fontSize: 20),
),
),
],
),
),
),
);
}
} class TouchArea extends StatefulWidget {
List allBricks;
TouchArea({this.allBricks}); @override
State<StatefulWidget> createState() {
return TouchAreaState();
}
} class TouchAreaState extends State<TouchArea> { @override
Widget build(BuildContext context) {
return GestureDetector(
child: Container(
margin:
EdgeInsets.only(left: 18, right: 18, top: 13, bottom: 13),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey, width: 3.0),
),
alignment: Alignment.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: widget.allBricks[0],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: widget.allBricks[1],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: widget.allBricks[2],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: widget.allBricks[3],
),
],
),
),
);
}
} class Brick extends StatefulWidget {
double x;
double y;
var brickSide;
int value;
int id; Brick({this.id, this.x, this.y, this.brickSide, this.value}); @override
State<StatefulWidget> createState() {
return BrickState();
}
} class BrickState extends State<Brick> {
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(2.0),
alignment: Alignment.center,
color: Colors.greenAccent,
width: widget.brickSide,
height: widget.brickSide,
child: Text('${widget.value}'),
);
}
} //class BrickState extends State<Brick>{
// @override
// Widget build(BuildContext context) {
// return Positioned(
// left: widget.x-widget.brickSide/2.0,top: widget.y-widget.brickSide/2.0,width: widget.brickSide,height: widget.brickSide,
// child: Container(child: Text('${widget.value}'),),
// );
// }
//}
flutter 动画 practice的更多相关文章
- 转:Flutter动画二
1. 介绍 本文会从代码层面去介绍Flutter动画,因此不会涉及到Flutter动画的具体使用. 1.1 Animation库 Flutter的animation库只依赖两个库,Dart库以及phy ...
- 转:Flutter动画一
1. 动画介绍 动画对于App来说,非常的重要.很多App,正是因为有了动画,所以才会觉得炫酷.移动端的动画库有非常的多,例如iOS上的Pop.web端的animate.css.Android端的An ...
- Flutter 动画详解(一)
本文主要介绍了动画的原理相关概念,对其他平台的动画做了一个简要的梳理,并简要的介绍了Flutter动画的一些知识. 1. 动画介绍 动画对于App来说,非常的重要.很多App,正是因为有了动画,所以才 ...
- 《Flutter 动画系列一》25种动画组件超全总结
动画运行的原理 任何程序的动画原理都是一样的,即:视觉暂留,视觉暂留又叫视觉暂停,人眼在观察景物时,光信号传入大脑神经,需经过一段短暂的时间,光的作用结束后,视觉形象并不立即消失,这种残留的视觉称&q ...
- 《Flutter 动画系列》组合动画
老孟导读:在前面的文章中介绍了 <Flutter 动画系列>25种动画组件超全总结 http://laomengit.com/flutter/module/animated_1/ < ...
- Flutter 动画使用
旋转动画 透明度变换动画 在Android中,可以通过View.animate()对视图进行动画处理,那在Flutter中怎样才能对Widget进行处理 在Flutter中,可以通过动画库给wid ...
- Flutter 动画鼻祖之CustomPaint
老孟导读:CustomPaint可以称之为动画鼻祖,它可以实现任何酷炫的动画和效果.CustomPaint本身没有动画属性,仅仅是绘制属性,一般情况下,CustomPaint会和动画控制配合使用,达到 ...
- flutter 动画双指放大图片
class GridAnimation extends StatefulWidget { @override State<StatefulWidget> createState() { r ...
- flutter 动画
AnimatedCrossFade AnimatedCrossFade让俩个子widget 交替淡入淡出. class AnimatedCrossFade1 extends StatefulWidge ...
随机推荐
- koa 搭建模块化路由/层级路由
搭建node项目目录以及基本的文件 初始化package.json文件 执行下面命令生成package.json文件 npm init --yes 创建项目目录 创建路由目录routes,存放静态资源 ...
- Kubernetes kubectl 命令概述
kubectl用于运行Kubernetes集群命令的管理工具. 语法 kubectl [command] [TYPE] [NAME] [flags] command:指定要在一个或多个资源执行的操作 ...
- Edusoho之X-Auth-Token
昨天这篇文章Edusoho之Basic Authentication提到了X-Auth-Token.今天我主要讲的是Edusoho之X-Auth-Token的请求API方式. 至于为什么建议不要用HT ...
- 循环(for,while,until)与循环控制符(break,continue)
一.for循环 第一种风格 for ((;;;))(类似C语言风格) do command done 例子:for ((i=0;i<10;i++)) do echo $i done 第二种风 ...
- 范仁义html+css课程---3、图片和超链接
范仁义html+css课程---3.图片和超链接 一.总结 一句话总结: img标签是图片标签,定义 HTML 页面中的图像 a标签是超链接标签,用于从一个页面链接到另一个页面. 1.img标签要点? ...
- 002 vue-resource
一:概述 1.说明 https://github.com/pagekit/vue-resource 2.使用方法 Vue.http.get('/someUrl', [config]).then(suc ...
- Python3基础 sys.path 查看与修改导包的路径
Python : 3.7.3 OS : Ubuntu 18.04.2 LTS IDE : pycharm-community-2019.1.3 ...
- hibernate的load和get有什么作用
① load方法认为该数据在数据库中一定存在,可以放心的使用代理来延迟加载,如果在使用过程中发现了问题,只能抛异常(ObjectNotFoundException)load方法加载实体对象的时候,根据 ...
- 机器学习技法实现(一):AdaBoost- Decision Stump (AdaBoost - 决策树的基于Matlab的实现)
经过前面对AdaBoost的总结,下面要基于Matlab实现AdaBoost-Stump进行二维平面数据点的分类的实验. 一. 实验原理 参看 http://blog.csdn.net/lg12591 ...
- 关于while read line使用read -p失效问题
while stdin 原来默认是/dev/tty,被重定向到管道或文件后,如果你还想读屏幕(/dev/tty),那就单独执行某个命令时在显式的将stdin再指向 /dev/tty #!/bin/ba ...