您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)Flutter中如何實(shí)現(xiàn)動(dòng)畫效果,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
首先在bar.dart中創(chuàng)建BarChart類,并使用固定長(zhǎng)度的Bar實(shí)例列表。我們將使用5個(gè)條形,表示一周的5個(gè)工作日。然后,我們需要將創(chuàng)建空白和隨機(jī)實(shí)例的責(zé)任從Bar轉(zhuǎn)移到BarChart。
import 'package:flutter/material.dart'; import 'package:flutter/animation.dart'; import 'dart:ui' show lerpDouble; import 'dart:math'; import 'color_palette.dart'; class BarChart { static const int barCount = 5; final List<Bar> bars; BarChart(this.bars) { assert(bars.length == barCount); } factory BarChart.empty() { return new BarChart( /* List.filled( int length, E fill, { bool growable: false } ) 創(chuàng)建給定長(zhǎng)度的固定長(zhǎng)度列表,并用fill在每個(gè)位置初始化值 length必須是非負(fù)整數(shù) */ new List.filled( barCount, new Bar(0.0, Colors.transparent) ) ); } factory BarChart.random(Random random) { final Color color = ColorPalette.primary.random(random); return new BarChart( /* List.generate( int length, E generator( int index ), { bool growable: true } ) 創(chuàng)建給定長(zhǎng)度的固定長(zhǎng)度列表,并用generator創(chuàng)建的值在每個(gè)位置初始化值 創(chuàng)建的列表是固定長(zhǎng)度,除非growable為true */ new List.generate( barCount, (i) => new Bar( random.nextDouble()*100.0, color ) ) ); } static BarChart lerp(BarChart begin, BarChart end, double t) { return new BarChart( new List.generate( barCount, (i) => Bar.lerp(begin.bars[i], end.bars[i], t) ) ); } } class BarChartTween extends Tween<BarChart> { BarChartTween(BarChart begin, BarChart end) : super(begin: begin, end: end); @override BarChart lerp(double t) => BarChart.lerp(begin, end, t); } class Bar { Bar(this.height, this.color); final double height; final Color color; static Bar lerp(Bar begin, Bar end, double t) { return new Bar( lerpDouble(begin.height, end.height, t), Color.lerp(begin.color, end.color, t) ); } } class BarTween extends Tween<Bar> { BarTween(Bar begin, Bar end) : super(begin: begin, end: end); @override Bar lerp(double t) => Bar.lerp(begin, end, t); } class BarChartPainter extends CustomPainter { static const barWidthFraction = 0.75; BarChartPainter(Animation<BarChart> animation) : animation = animation, super(repaint: animation); final Animation<BarChart> animation; @override void paint(Canvas canvas, Size size) { void drawBar(Bar bar, double x, double width, Paint paint) { paint.color = bar.color; canvas.drawRect( new Rect.fromLTWH( x, size.height-bar.height, width, bar.height ), paint ); } /* Paint:Canvas繪制時(shí)使用的樣式說明 style:是否繪制內(nèi)部的形狀、形狀的邊緣或兩者都有,默認(rèn)為PaintingStyle.fill */ final paint = new Paint()..style = PaintingStyle.fill; final chart = animation.value; // 每個(gè)條形占用的空間寬度 final barDistance = size.width/(1+chart.bars.length); // 每個(gè)條形占用空間75%的寬度 final barWidth = barDistance*barWidthFraction; // 用于計(jì)算每個(gè)條形的x坐標(biāo)點(diǎn) var x = barDistance-barWidth/2; for (final bar in chart.bars) { drawBar(bar, x, barWidth, paint); x += barDistance; } } @override bool shouldRepaint(BarChartPainter old) => false; }
BarChartPainter在條形之間均勻分配可用寬度,并使每個(gè)條形占用可用寬度的75%。接下來我們要更新main.dart,用BarChart、BarChartTween替換Bar、BarTween。
// ... class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin { final random = new Random(); AnimationController animation; BarChartTween tween; @override void initState() { super.initState(); animation = new AnimationController( duration: const Duration(milliseconds: 300), vsync: this ); tween = new BarChartTween(new BarChart.empty(), new BarChart.random(random)); animation.forward(); } @override void dispose() { animation.dispose(); super.dispose(); } void changeData() { setState(() { tween = new BarChartTween( tween.evaluate(animation), new BarChart.random(random), ); animation.forward(from: 0.0); }); } @override Widget build(BuildContext context) { return new Scaffold( body: new Center( child: new CustomPaint( size: new Size(200.0, 100.0), painter: new BarChartPainter(tween.animate(animation)) ) ), floatingActionButton: new FloatingActionButton( onPressed: changeData, child: new Icon(Icons.refresh), ), ); } }
現(xiàn)在應(yīng)用程序的效果如下圖:
以上就是Flutter中如何實(shí)現(xiàn)動(dòng)畫效果,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。