Skip to content

Commit d5ca61f

Browse files
committed
gfshimmer animated builder added
1 parent 280de1f commit d5ca61f

File tree

2 files changed

+105
-101
lines changed

2 files changed

+105
-101
lines changed

example/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ class _MyHomePageState extends State<MyHomePage>
234234
fontWeight: FontWeight.w700
235235
),
236236
),
237-
// direction: ShimmerDirection.ltr,
237+
direction: GFShimmerDirection.rightToLeft,
238238
gradient: LinearGradient(
239239
begin: Alignment.topLeft,
240240
end: Alignment.bottomRight,
Lines changed: 104 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter/rendering.dart';
33

4-
enum GFShimmerDirection { ltr, rtl, ttb, btt }
4+
enum GFShimmerDirection { leftToRight, rightToLeft, topToBottom, bottomToTop }
55

66
@immutable
77
class GFShimmer extends StatefulWidget {
88
const GFShimmer({
99
Key key,
1010
@required this.child,
1111
@required this.gradient,
12-
this.direction = GFShimmerDirection.ltr,
12+
this.direction = GFShimmerDirection.leftToRight,
1313
this.duration = const Duration(milliseconds: 1500),
1414
this.loop = 0,
1515
this.enabled = true,
@@ -22,13 +22,13 @@ class GFShimmer extends StatefulWidget {
2222
final int loop;
2323
final bool enabled;
2424

25-
GFShimmer.fromColors({
25+
GFShimmer.withColors({
2626
Key key,
2727
@required this.child,
2828
@required Color baseColor,
2929
@required Color highlightColor,
3030
this.duration = const Duration(milliseconds: 1500),
31-
this.direction = GFShimmerDirection.ltr,
31+
this.direction = GFShimmerDirection.leftToRight,
3232
this.loop = 0,
3333
this.enabled = true,
3434
}) : gradient = LinearGradient(
@@ -93,13 +93,17 @@ class _GFShimmerState extends State<GFShimmer> with SingleTickerProviderStateMix
9393
Widget build(BuildContext context) => AnimatedBuilder(
9494
animation: _controller,
9595
child: widget.child,
96-
builder: (BuildContext context, Widget child) => _GFShimmer(
96+
builder: (BuildContext context, Widget child) => Transform.rotate(
97+
angle: _controller.value * 2.0 * 22/7,
9798
child: child,
98-
direction: widget.direction,
99-
gradient: widget.gradient,
100-
percent: _controller.value,
101-
enabled: widget.enabled,
10299
),
100+
// builder: (BuildContext context, Widget child) => _GFShimmer(
101+
// child: child,
102+
// direction: widget.direction,
103+
// gradient: widget.gradient,
104+
// percent: _controller.value,
105+
// enabled: widget.enabled,
106+
// ),
103107
);
104108

105109
@override
@@ -109,94 +113,94 @@ class _GFShimmerState extends State<GFShimmer> with SingleTickerProviderStateMix
109113
}
110114
}
111115

112-
@immutable
113-
class _GFShimmer extends SingleChildRenderObjectWidget {
114-
const _GFShimmer({
115-
Widget child,
116-
this.percent,
117-
this.direction,
118-
this.gradient,
119-
this.enabled,
120-
}) : super(child: child);
121-
122-
final double percent;
123-
final GFShimmerDirection direction;
124-
final Gradient gradient;
125-
final bool enabled;
126-
127-
@override
128-
_GFShimmerFilter createRenderObject(BuildContext context) => _GFShimmerFilter(percent, direction, gradient, enabled);
129-
130-
@override
131-
void updateRenderObject(BuildContext context, _GFShimmerFilter shimmer) {
132-
shimmer.percent = percent;
133-
shimmer.enabled = enabled;
134-
}
135-
}
136-
137-
class _GFShimmerFilter extends RenderProxyBox {
138-
139-
_GFShimmerFilter(this._percent, this._direction, this._gradient, this.enabled)
140-
: _gradientPaint = Paint()..blendMode = BlendMode.srcIn;
141-
142-
final Paint _clearPaint = Paint();
143-
final Paint _gradientPaint;
144-
final Gradient _gradient;
145-
final GFShimmerDirection _direction;
146-
bool enabled;
147-
double _percent;
148-
Rect _rect;
149-
150-
@override
151-
bool get alwaysNeedsCompositing => child != null;
152-
153-
set percent(double newValue) {
154-
if (newValue == _percent) {
155-
return;
156-
}
157-
_percent = newValue;
158-
markNeedsPaint();
159-
}
160-
161-
@override
162-
void paint(PaintingContext context, Offset offset) {
163-
if (child == null) {
164-
return;
165-
}
166-
assert(needsCompositing);
167-
168-
context.canvas.saveLayer(offset & child.size, _clearPaint);
169-
context.paintChild(child, offset);
170-
171-
final double width = child.size.width;
172-
final double height = child.size.height;
173-
Rect rect;
174-
double dx, dy;
175-
if (_direction == GFShimmerDirection.rtl) {
176-
dx = _offset(width, -width, _percent);
177-
dy = 0.0;
178-
rect = Rect.fromLTWH(offset.dx - width, offset.dy, 3 * width, height);
179-
} else if (_direction == GFShimmerDirection.ttb) {
180-
dx = 0.0;
181-
dy = _offset(-height, height, _percent);
182-
rect = Rect.fromLTWH(offset.dx, offset.dy - height, width, 3 * height);
183-
} else if (_direction == GFShimmerDirection.btt) {
184-
dx = 0.0;
185-
dy = _offset(height, -height, _percent);
186-
rect = Rect.fromLTWH(offset.dx, offset.dy - height, width, 3 * height);
187-
} else {
188-
dx = _offset(-width, width, _percent);
189-
dy = 0.0;
190-
rect = Rect.fromLTWH(offset.dx - width, offset.dy, 3 * width, height);
191-
}
192-
if (_rect != rect) {
193-
_gradientPaint.shader = _gradient.createShader(rect);
194-
_rect = rect;
195-
}
196-
context.canvas.translate(dx, dy);
197-
context.canvas.drawRect(rect, _gradientPaint);
198-
context.canvas.restore();
199-
}
200-
201-
double _offset(double start, double end, double percent) => start + (end - start) * percent;
202-
}
116+
//@immutable
117+
//class _GFShimmer extends SingleChildRenderObjectWidget {
118+
// const _GFShimmer({
119+
// Widget child,
120+
// this.percent,
121+
// this.direction,
122+
// this.gradient,
123+
// this.enabled,
124+
// }) : super(child: child);
125+
//
126+
// final double percent;
127+
// final GFShimmerDirection direction;
128+
// final Gradient gradient;
129+
// final bool enabled;
130+
//
131+
// @override
132+
// _GFShimmerFilter createRenderObject(BuildContext context) => _GFShimmerFilter(percent, direction, gradient, enabled);
133+
//
134+
// @override
135+
// void updateRenderObject(BuildContext context, _GFShimmerFilter shimmer) {
136+
// shimmer.percent = percent;
137+
// shimmer.enabled = enabled;
138+
// }
139+
//}
140+
//
141+
//class _GFShimmerFilter extends RenderProxyBox {
142+
//
143+
// _GFShimmerFilter(this._percent, this._direction, this._gradient, this.enabled)
144+
// : _gradientPaint = Paint()..blendMode = BlendMode.srcIn;
145+
//
146+
// final Paint _clearPaint = Paint();
147+
// final Paint _gradientPaint;
148+
// final Gradient _gradient;
149+
// final GFShimmerDirection _direction;
150+
// bool enabled;
151+
// double _percent;
152+
// Rect _rect;
153+
//
154+
// @override
155+
// bool get alwaysNeedsCompositing => child != null;
156+
//
157+
// set percent(double newValue) {
158+
// if (newValue == _percent) {
159+
// return;
160+
// }
161+
// _percent = newValue;
162+
// markNeedsPaint();
163+
// }
164+
//
165+
// @override
166+
// void paint(PaintingContext context, Offset offset) {
167+
// if (child == null) {
168+
// return;
169+
// }
170+
// assert(needsCompositing);
171+
//
172+
// context.canvas.saveLayer(offset & child.size, _clearPaint);
173+
// context.paintChild(child, offset);
174+
//
175+
// final double width = child.size.width;
176+
// final double height = child.size.height;
177+
// Rect rect;
178+
// double dx, dy;
179+
// if (_direction == GFShimmerDirection.rightToLeft) {
180+
// dx = _offset(width, -width, _percent);
181+
// dy = 0.0;
182+
// rect = Rect.fromLTWH(offset.dx - width, offset.dy, 3 * width, height);
183+
// } else if (_direction == GFShimmerDirection.topToBottom) {
184+
// dx = 0.0;
185+
// dy = _offset(-height, height, _percent);
186+
// rect = Rect.fromLTWH(offset.dx, offset.dy - height, width, 3 * height);
187+
// } else if (_direction == GFShimmerDirection.bottomToTop) {
188+
// dx = 0.0;
189+
// dy = _offset(height, -height, _percent);
190+
// rect = Rect.fromLTWH(offset.dx, offset.dy - height, width, 3 * height);
191+
// } else {
192+
// dx = _offset(-width, width, _percent);
193+
// dy = 0.0;
194+
// rect = Rect.fromLTWH(offset.dx - width, offset.dy, 3 * width, height);
195+
// }
196+
// if (_rect != rect) {
197+
// _gradientPaint.shader = _gradient.createShader(rect);
198+
// _rect = rect;
199+
// }
200+
// context.canvas.translate(dx, dy);
201+
// context.canvas.drawRect(rect, _gradientPaint);
202+
// context.canvas.restore();
203+
// }
204+
//
205+
// double _offset(double start, double end, double percent) => start + (end - start) * percent;
206+
//}

0 commit comments

Comments
 (0)