您好,登錄后才能下訂單哦!
這篇文章主要介紹了Flutter如何給控件實現(xiàn)微光特效,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
Shimmer( baseColor: const Color(0x08ffffff), // 背景顏色 highlightColor: Colors.white, // 高光的顏色 loop: 2, // 閃爍循環(huán)次數(shù),不傳默認一直循環(huán) child: Image.asset('assets/images/watermelon.png',width: 325, height: 260, fit: BoxFit.contain), )
① 特效控件分為兩層:底層顯示調(diào)用方傳入的控件;上層覆蓋一層漸變著色器。
② 啟動動畫,根據(jù)動畫的進度,對漸變著色器的區(qū)域進行繪制,當(dāng)區(qū)域變大變小時,著色器高光的地方也在相應(yīng)進行偏移。
③ 同時著色器不能超出底層控件的繪制范圍,底層控件的形狀是不規(guī)則的,漸變層不能超出底層控件的layer對象。這樣才能實現(xiàn)完全貼合 底層控件形狀 的微光閃爍。
控件分層顯示
@override Widget build(BuildContext context) { return Stack( children: [ // 底層控件 widget.child, // 覆蓋閃爍微光 AnimatedBuilder( animation: _controller, child: widget.child, builder: (BuildContext context, Widget? child) => _Shimmer( child: child, percent: _controller.value, direction: widget.direction, gradient: widget.gradient, ), ) ], );
開啟動畫
late AnimationController _controller; int _count = 0; @override void initState() { super.initState(); _controller = AnimationController(vsync: this, duration: widget.duration) ..addStatusListener((AnimationStatus status) { if (status != AnimationStatus.completed) { return; } _count++; if (widget.loop != 0 && _count < widget.loop) { _controller.forward(from: 0.0); } }); if (widget.loop == 0) { _controller.repeat(); } else { _controller.forward(); } }
重點:著色器該如何繪制,又該如何通過AnimationController的進度進行偏移?由于著色器不能超出底層控件的繪制范圍,所以必須拿到底層控件的繪制上下文【即 PaintingContext】,調(diào)用其pushLayer方法,讓引擎把著色器繪制上去。
需要用到PaintingContext,自然就需要去管理RenderObject,所以著色器的編寫使用RenderProxyBox進行計算并繪制出layer對象,計算的過程根據(jù)上面的AnimationController的進度進行計算。
class _ShimmerFilter extends RenderProxyBox { ShimmerDirection _direction; Gradient _gradient; double _percent; _ShimmerFilter(this._percent, this._direction, this._gradient); @override ShaderMaskLayer? get layer => super.layer as ShaderMaskLayer?; set percent(double newValue) { if (newValue != _percent) { _percent = newValue; markNeedsPaint(); } } set gradient(Gradient newValue) { if (newValue != _gradient) { _gradient = newValue; markNeedsPaint(); } } set direction(ShimmerDirection newDirection) { if (newDirection != _direction) { _direction = newDirection; markNeedsLayout(); } } @override void paint(PaintingContext context, Offset offset) { if (child != null) { final double width = child!.size.width; final double height = child!.size.height; Rect rect; double dx, dy; if (_direction == ShimmerDirection.rtl) { dx = _offset(width, -width, _percent); dy = 0.0; rect = Rect.fromLTWH(dx - width, dy, 3 * width, height); } else if (_direction == ShimmerDirection.ttb) { dx = 0.0; dy = _offset(-height, height, _percent); rect = Rect.fromLTWH(dx, dy - height, width, 3 * height); } else if (_direction == ShimmerDirection.btt) { dx = 0.0; dy = _offset(height, -height, _percent); rect = Rect.fromLTWH(dx, dy - height, width, 3 * height); } else { dx = _offset(-width, width, _percent); dy = 0.0; rect = Rect.fromLTWH(dx - width, dy, 3 * width, height); } layer ??= ShaderMaskLayer(); layer! ..shader = _gradient.createShader(rect) ..maskRect = offset & size ..blendMode = BlendMode.srcIn; context.pushLayer(layer!, super.paint, offset); } else { layer = null; } } double _offset(double start, double end, double percent) { return start + (end - start) * percent; } }
Render對象繪制出來后,需要封裝成widget使用,由于是單一組件,用SingleChildRenderObjectWidget即可。
class _Shimmer extends SingleChildRenderObjectWidget { @override _ShimmerFilter createRenderObject(BuildContext context) { return _ShimmerFilter(percent, direction, gradient); } @override void updateRenderObject(BuildContext context, _ShimmerFilter shimmer) { shimmer.percent = percent; shimmer.gradient = gradient; shimmer.direction = direction; } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Flutter如何給控件實現(xiàn)微光特效”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。