flutter自定义Loading
2019-02-13 11:34
232 查看
1、先上效果图
2、源码如下
import 'dart:math'; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; class Loading extends StatefulWidget { final Color color; final double size; final double lineWidth; const Loading({ Key key, @required this.color, this.lineWidth = 3.0, this.size = 50.0, }) : assert(color != null), assert(lineWidth != null), assert(size != null), super(key: key); @override _LoadingState createState() => _LoadingState(); } class _LoadingState extends State<Loading> with SingleTickerProviderStateMixin { AnimationController _controller; Animation<double> _animation1, _animation2, _animation3; @override void initState() { super.initState(); _controller = AnimationController(vsync: this, duration: Duration(milliseconds: 1500)); _animation1 = Tween(begin: 0.0, end: 1.0).animate( CurvedAnimation( parent: _controller, curve: Interval(0.0, 1.0, curve: Curves.linear), ), )..addListener(() => setState(() => <String, void>{})); _animation2 = Tween(begin: -2 / 3, end: 1 / 2).animate( CurvedAnimation( parent: _controller, curve: Interval(0.5, 1.0, curve: Curves.linear), ), )..addListener(() => setState(() => <String, void>{})); _animation3 = Tween(begin: 0.25, end: 5 / 6).animate( CurvedAnimation( parent: _controller, curve: Interval(0.0, 1.0, curve: _MyCurve()), ), )..addListener(() => setState(() => <String, void>{})); _controller.repeat(); } @override void dispose() { _controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) { final Matrix4 transform = Matrix4.identity()..rotateZ((_animation1.value) * 5 * pi / 6); return Center( child: Stack( alignment: const Alignment(0.1, 0.1), children: [ Transform( transform: transform, alignment: FractionalOffset.center, child: Container( height: widget.size, width: widget.size, child: CustomPaint( foregroundPainter: RingPainter( paintWidth: widget.lineWidth, trackColor: widget.color, progressPercent: _animation3.value, startAngle: pi * _animation2.value, ), ), ), ), CircleAvatar( backgroundImage: AssetImage('images/logo.png'), radius: 20.0, ), ], ), ); } } class RingPainter extends CustomPainter { final double paintWidth; final Paint trackPaint; final Color trackColor; final double progressPercent; final double startAngle; RingPainter({ this.paintWidth, this.progressPercent, this.startAngle, this.trackColor, }) : trackPaint = Paint() ..color = trackColor ..style = PaintingStyle.stroke ..strokeWidth = paintWidth ..strokeCap = StrokeCap.square; @override void paint(Canvas canvas, Size size) { final center = Offset(size.width / 2, size.height / 2); final radius = (min(size.width, size.height) - paintWidth) / 2; final progressAngle = 2 * pi * progressPercent; canvas.drawArc( Rect.fromCircle( center: center, radius: radius, ), startAngle, progressAngle, false, trackPaint); } @override bool shouldRepaint(CustomPainter oldDelegate) { return true; } } class _MyCurve extends Curve { @override double transform(double t) { if (t <= 0.5) { return 2 * t; } else { return 2 * (1 - t); } } }
三、使用
return new Center( child: new Loading(color: const Color(0xFFC9A063), size: 56.0), );
相关文章推荐
- 自定义加载进度条loading的几种方式
- 自定义Loading的动态效果
- Flex :自定义Loading载入状态(含图标) 及解决Flex中GIF动画图片加载时显示动起来
- ionic开发——自定义等待动画$ionicLoading
- Cocos2d-JS 自定义loading界面
- element使用自定义的loading加载效果
- Flutter自定义字体
- android loading界面 & 自定义ProgressDialog
- [cocos2dx]自定义loading动画
- Android 自定义View之烧瓶loading动画
- 自定义动态loading界面——仿饿了么动态加载
- 在线提供自定义的 loading 状态的图片
- Android 自定义View 模仿豆瓣App加载中Loading(也类似支付宝AppLoading)
- Flutter 自定义Dialog
- vue自定义loading效果
- Android ProgressBar 自定义样式(七),仿真QQ聊天、网易新闻客户端加载图片Loading
- 手把手带你做一个超炫酷loading成功动画view Android自定义view
- android 自定义loading动画
- 自定义LoadingView大全
- 自定义LoadingView大全之转动立方体