Skip to content

Commit 9810386

Browse files
author
Kamil Klyta
committed
Introduce material delegate
1 parent cad9384 commit 9810386

14 files changed

+482
-191
lines changed

example/lib/indicators/plane_indicator.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,9 @@ class _PlaneIndicatorState extends State<PlaneIndicator>
186186
_startCloudAnimation();
187187
_startPlaneAnimation();
188188
} else if (_prevState == IndicatorState.loading &&
189-
currentState == IndicatorState.hiding) {
189+
_prevState != currentState) {
190190
_stopPlaneAnimation();
191-
} else if (_prevState == IndicatorState.hiding &&
192-
currentState != _prevState) {
191+
} else if (_prevState != currentState && currentState.isIdle) {
193192
_stopCloudAnimation();
194193
}
195194

Lines changed: 27 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,41 @@
11
import 'package:custom_refresh_indicator/custom_refresh_indicator.dart';
22
import 'package:example/utils/infinite_rotation.dart';
3-
import 'package:example/utils/positioned_indicator_container.dart';
43
import 'package:flutter/material.dart';
54

65
import 'custom_indicator.dart';
76

8-
class SimpleIndicatorContent extends StatelessWidget {
9-
const SimpleIndicatorContent({
10-
Key? key,
11-
required this.controller,
12-
this.indicatorSize = _defaultIndicatorSize,
13-
}) : assert(indicatorSize > 0),
14-
super(key: key);
15-
16-
final IndicatorController controller;
17-
static const _defaultIndicatorSize = 40.0;
18-
final double indicatorSize;
19-
20-
@override
21-
Widget build(BuildContext context) {
22-
return Container(
23-
height: indicatorSize,
24-
width: indicatorSize,
25-
decoration: const BoxDecoration(
26-
color: Color(0xFFFFFFFF),
27-
shape: BoxShape.circle,
28-
boxShadow: [BoxShadow(blurRadius: 5, color: Color(0x42000000))],
29-
),
30-
child: Align(
31-
alignment: Alignment.center,
32-
child: Stack(
33-
children: <Widget>[
34-
AnimatedBuilder(
35-
animation: controller,
36-
child: const Icon(
37-
Icons.refresh,
38-
color: Colors.blueAccent,
39-
size: 30,
40-
),
41-
builder: (context, child) => InfiniteRatation(
42-
running: controller.isLoading,
43-
child: child,
44-
),
45-
),
46-
],
47-
),
48-
),
49-
);
50-
}
51-
}
52-
537
final simpleIndicator = CustomIndicatorConfig(
54-
builder: (context, child, controller) =>
55-
LayoutBuilder(builder: (context, constraints) {
56-
return Stack(
57-
children: <Widget>[
58-
child,
59-
PositionedIndicatorContainer(
60-
constraints: constraints,
61-
controller: controller,
62-
child: SimpleIndicatorContent(
63-
controller: controller,
64-
),
8+
builder: MaterialIndicatorDelegate(
9+
builder: (context, controller) {
10+
return InfiniteRatation(
11+
running: controller.isLoading,
12+
child: Icon(
13+
Icons.ac_unit,
14+
color: Theme.of(context).colorScheme.primary,
15+
size: 30,
6516
),
66-
],
67-
);
68-
}),
17+
);
18+
},
19+
),
6920
);
7021

7122
final simpleIndicatorWithOpacity = CustomIndicatorConfig(
72-
builder: (context, child, controller) =>
73-
LayoutBuilder(builder: (context, constraints) {
74-
return Stack(children: <Widget>[
75-
Opacity(
23+
builder: MaterialIndicatorDelegate(
24+
builder: (context, controller) {
25+
return InfiniteRatation(
26+
running: controller.isLoading,
27+
child: Icon(
28+
Icons.ac_unit,
29+
color: Theme.of(context).colorScheme.primary,
30+
size: 30,
31+
),
32+
);
33+
},
34+
scrollableBuilder: (context, child, controller) {
35+
return Opacity(
7636
opacity: 1.0 - controller.value.clamp(0.0, 1.0),
7737
child: child,
78-
),
79-
PositionedIndicatorContainer(
80-
constraints: constraints,
81-
controller: controller,
82-
child: SimpleIndicatorContent(controller: controller),
83-
),
84-
]);
85-
}),
38+
);
39+
},
40+
),
8641
);

example/lib/screens/example_indicator_screen.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ class ExampleIndicatorScreen extends StatelessWidget {
1818
child: CustomRefreshIndicator(
1919
leadingScrollIndicatorVisible: false,
2020
trailingScrollIndicatorVisible: false,
21-
offsetToArmed: 200.0,
22-
autoRebuild: true,
2321
builder: customIndicator.builder,
2422
onRefresh: () => Future.delayed(const Duration(seconds: 2)),
2523
child: const ExampleList(

example/lib/screens/presentation_screen.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class _PresentationScreenState extends State<PresentationScreen> {
2020
body: SafeArea(
2121
child: CustomRefreshIndicator(
2222
trigger: IndicatorTrigger.bothEdges,
23-
loadingToIdleDuration: const Duration(seconds: 1),
24-
armedToLoadingDuration: const Duration(seconds: 1),
25-
draggingToIdleDuration: const Duration(seconds: 1),
23+
indicatorFinalizeDuration: const Duration(seconds: 1),
24+
indicatorSettleDuration: const Duration(seconds: 1),
25+
indicatorCancelDuration: const Duration(seconds: 1),
2626
leadingScrollIndicatorVisible: false,
2727
trailingScrollIndicatorVisible: false,
2828
offsetToArmed: 100.0,

example/lib/utils/positioned_indicator_container.dart

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,45 @@ import 'package:flutter/material.dart';
44
class PositionedIndicatorContainer extends StatelessWidget {
55
final IndicatorController controller;
66
final Widget child;
7-
final BoxConstraints? constraints;
87

98
/// Position child widget in a similar way
109
/// to the built-in [RefreshIndicator] widget.
1110
const PositionedIndicatorContainer({
1211
Key? key,
1312
required this.controller,
14-
required this.constraints,
1513
required this.child,
1614
}) : super(key: key);
1715

1816
@override
1917
Widget build(BuildContext context) {
20-
return AnimatedBuilder(
21-
animation: controller,
22-
builder: (BuildContext context, Widget? child) {
23-
if (controller.side.isNone) return const SizedBox();
18+
if (controller.side.isNone) return const SizedBox();
2419

25-
final double positionFromSide = -50 + (controller.value * 110);
20+
final double positionFromSide = -50 + (controller.value * 110);
2621

27-
return Positioned(
28-
top: controller.side.isTop ? positionFromSide : null,
29-
bottom: controller.side.isBottom ? positionFromSide : null,
30-
left: controller.side.isLeft ? positionFromSide : null,
31-
right: controller.side.isRight ? positionFromSide : null,
32-
child: LayoutBuilder(
33-
builder: (ctx, cons) {
34-
return SizedBox(
35-
height: controller.isHorizontalDirection
36-
? constraints?.maxHeight ??
37-
MediaQuery.of(context).size.height
38-
: null,
39-
width: controller.isVerticalDirection
40-
? constraints?.maxWidth ?? MediaQuery.of(context).size.width
41-
: null,
42-
child: child,
43-
);
44-
},
45-
),
46-
);
47-
},
22+
final verticalSide = controller.side.isTop || controller.side.isBottom;
23+
final horizontalSide = controller.side.isLeft || controller.side.isRight;
24+
25+
return Positioned(
26+
top: horizontalSide
27+
? 0
28+
: controller.side.isTop
29+
? positionFromSide
30+
: null,
31+
bottom: horizontalSide
32+
? 0
33+
: controller.side.isBottom
34+
? positionFromSide
35+
: null,
36+
left: verticalSide
37+
? 0
38+
: controller.side.isLeft
39+
? positionFromSide
40+
: null,
41+
right: verticalSide
42+
? 0
43+
: controller.side.isRight
44+
? positionFromSide
45+
: null,
4846
child: child,
4947
);
5048
}

lib/custom_refresh_indicator.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export 'src/data/data.dart';
22
export 'src/custom_refresh_indicator.dart';
3+
export 'src/delegates/delegates.dart';

lib/src/controller.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ class IndicatorController extends ChangeNotifier {
8888
case AxisDirection.up:
8989
return edge.isStart ? IndicatorSide.bottom : IndicatorSide.top;
9090
case AxisDirection.right:
91-
return edge.isStart ? IndicatorSide.right : IndicatorSide.left;
91+
return edge.isStart ? IndicatorSide.left : IndicatorSide.right;
9292
case AxisDirection.down:
9393
return edge.isStart ? IndicatorSide.top : IndicatorSide.bottom;
9494
case AxisDirection.left:
95-
return edge.isStart ? IndicatorSide.left : IndicatorSide.right;
95+
return edge.isStart ? IndicatorSide.right : IndicatorSide.left;
9696
}
9797
}
9898

@@ -137,7 +137,7 @@ class IndicatorController extends ChangeNotifier {
137137
bool get isDragging => _currentState.isDragging;
138138
bool get isLoading => _currentState.isLoading;
139139
bool get isComplete => _currentState.isComplete;
140-
bool get isHiding => _currentState.isHiding;
140+
bool get isFinalizing => _currentState.isFinalizing;
141141
bool get isIdle => _currentState.isIdle;
142142

143143
bool _shouldStopDrag;
@@ -150,7 +150,7 @@ class IndicatorController extends ChangeNotifier {
150150
bool _isRefreshEnabled;
151151

152152
void stopDrag() {
153-
if (isDragging || isArmed) {
153+
if (state.isDragging || state.isArmed) {
154154
_shouldStopDrag = true;
155155
} else {
156156
throw StateError(

0 commit comments

Comments
 (0)