Skip to content

Commit 1938cb9

Browse files
committed
Use widget size for flutter plane indicator
1 parent 01d6d38 commit 1938cb9

File tree

1 file changed

+97
-93
lines changed

1 file changed

+97
-93
lines changed

example/lib/indicators/plane_indicator.dart

Lines changed: 97 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -150,105 +150,109 @@ class _PlaneIndicatorState extends State<PlaneIndicator>
150150

151151
@override
152152
Widget build(BuildContext context) {
153-
final screenWidth = MediaQuery.of(context).size.width;
154-
final plane = AnimatedBuilder(
155-
animation: _planeController,
156-
child: Image.asset(
157-
"assets/plane_indicator/plane.png",
158-
width: 172,
159-
height: 50,
160-
fit: BoxFit.contain,
161-
),
162-
builder: (BuildContext context, Widget? child) {
163-
return Transform.translate(
164-
offset: Offset(
165-
0.0, 10 * (0.5 - _planeTween.transform(_planeController.value))),
166-
child: child,
153+
return LayoutBuilder(
154+
builder: (context, constraints) {
155+
final screenWidth = constraints.maxWidth;
156+
final plane = AnimatedBuilder(
157+
animation: _planeController,
158+
child: Image.asset(
159+
"assets/plane_indicator/plane.png",
160+
width: 172,
161+
height: 50,
162+
fit: BoxFit.contain,
163+
),
164+
builder: (BuildContext context, Widget? child) {
165+
return Transform.translate(
166+
offset: Offset(0.0,
167+
10 * (0.5 - _planeTween.transform(_planeController.value))),
168+
child: child,
169+
);
170+
},
167171
);
168-
},
169-
);
170-
return CustomRefreshIndicator(
171-
offsetToArmed: _offsetToArmed,
172-
child: widget.child,
173-
onRefresh: () => Future.delayed(const Duration(seconds: 3)),
174-
builder:
175-
(BuildContext context, Widget child, IndicatorController controller) {
176-
return AnimatedBuilder(
177-
animation: controller,
178-
child: child,
179-
builder: (context, child) {
180-
final currentState = controller.state;
181-
if (_prevState == IndicatorState.armed &&
182-
currentState == IndicatorState.loading) {
183-
_startCloudAnimation();
184-
_startPlaneAnimation();
185-
} else if (_prevState == IndicatorState.loading &&
186-
currentState == IndicatorState.hiding) {
187-
_stopPlaneAnimation();
188-
} else if (_prevState == IndicatorState.hiding &&
189-
currentState != _prevState) {
190-
_stopCloudAnimation();
191-
}
172+
return CustomRefreshIndicator(
173+
offsetToArmed: _offsetToArmed,
174+
child: widget.child,
175+
onRefresh: () => Future.delayed(const Duration(seconds: 3)),
176+
builder: (BuildContext context, Widget child,
177+
IndicatorController controller) {
178+
return AnimatedBuilder(
179+
animation: controller,
180+
child: child,
181+
builder: (context, child) {
182+
final currentState = controller.state;
183+
if (_prevState == IndicatorState.armed &&
184+
currentState == IndicatorState.loading) {
185+
_startCloudAnimation();
186+
_startPlaneAnimation();
187+
} else if (_prevState == IndicatorState.loading &&
188+
currentState == IndicatorState.hiding) {
189+
_stopPlaneAnimation();
190+
} else if (_prevState == IndicatorState.hiding &&
191+
currentState != _prevState) {
192+
_stopCloudAnimation();
193+
}
192194

193-
_prevState = currentState;
195+
_prevState = currentState;
194196

195-
return Stack(
196-
clipBehavior: Clip.hardEdge,
197-
children: <Widget>[
198-
if (_prevState != IndicatorState.idle)
199-
Container(
200-
height: _offsetToArmed * controller.value,
201-
color: const Color(0xFFFDFEFF),
202-
width: double.infinity,
203-
child: AnimatedBuilder(
204-
animation: _clouds.first.controller!,
205-
builder: (BuildContext context, Widget? child) {
206-
return Stack(
207-
clipBehavior: Clip.hardEdge,
208-
children: <Widget>[
209-
for (final cloud in _clouds)
210-
Transform.translate(
211-
offset: Offset(
212-
((screenWidth + cloud.width!) *
213-
cloud.controller!.value) -
214-
cloud.width!,
215-
cloud.dy! * controller.value,
216-
),
217-
child: OverflowBox(
218-
minWidth: cloud.width,
219-
minHeight: cloud.width,
220-
maxHeight: cloud.width,
221-
maxWidth: cloud.width,
222-
alignment: Alignment.topLeft,
223-
child: Image(
224-
color: cloud.color,
225-
image: cloud.image!,
226-
fit: BoxFit.contain,
197+
return Stack(
198+
clipBehavior: Clip.hardEdge,
199+
children: <Widget>[
200+
if (_prevState != IndicatorState.idle)
201+
Container(
202+
height: _offsetToArmed * controller.value,
203+
color: const Color(0xFFFDFEFF),
204+
width: double.infinity,
205+
child: AnimatedBuilder(
206+
animation: _clouds.first.controller!,
207+
builder: (BuildContext context, Widget? child) {
208+
return Stack(
209+
clipBehavior: Clip.hardEdge,
210+
children: <Widget>[
211+
for (final cloud in _clouds)
212+
Transform.translate(
213+
offset: Offset(
214+
((screenWidth + cloud.width!) *
215+
cloud.controller!.value) -
216+
cloud.width!,
217+
cloud.dy! * controller.value,
218+
),
219+
child: OverflowBox(
220+
minWidth: cloud.width,
221+
minHeight: cloud.width,
222+
maxHeight: cloud.width,
223+
maxWidth: cloud.width,
224+
alignment: Alignment.topLeft,
225+
child: Image(
226+
color: cloud.color,
227+
image: cloud.image!,
228+
fit: BoxFit.contain,
229+
),
230+
),
227231
),
228-
),
229-
),
230232

231-
/// plane
232-
Center(
233-
child: OverflowBox(
234-
child: plane,
235-
maxWidth: 172,
236-
minWidth: 172,
237-
maxHeight: 50,
238-
minHeight: 50,
239-
alignment: Alignment.center,
240-
),
241-
),
242-
],
243-
);
244-
},
233+
/// plane
234+
Center(
235+
child: OverflowBox(
236+
child: plane,
237+
maxWidth: 172,
238+
minWidth: 172,
239+
maxHeight: 50,
240+
minHeight: 50,
241+
alignment: Alignment.center,
242+
),
243+
),
244+
],
245+
);
246+
},
247+
),
248+
),
249+
Transform.translate(
250+
offset: Offset(0.0, _offsetToArmed * controller.value),
251+
child: child,
245252
),
246-
),
247-
Transform.translate(
248-
offset: Offset(0.0, _offsetToArmed * controller.value),
249-
child: child,
250-
),
251-
],
253+
],
254+
);
255+
},
252256
);
253257
},
254258
);

0 commit comments

Comments
 (0)