Repository navigation
Expand file tree
/
Copy pathprogress_indicator.dart
More file actions
1640 lines (1463 loc) · 61.1 KB
/
Copy pathprogress_indicator.dart
File metadata and controls
1640 lines (1463 loc) · 61.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/// @docImport 'package:flutter/semantics.dart';
///
/// @docImport 'refresh_indicator.dart';
library;
import 'dart:math' as math;
import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'color_scheme.dart';
import 'material.dart';
import 'progress_indicator_theme.dart';
import 'theme.dart';
// This value is extracted from
// https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/res/res/anim/progress_indeterminate_material.xml;drc=9cb5b4c2d93acb9d6f5e14167e265c328c487d6b
const int _kIndeterminateLinearDuration = 1800;
// This value is extracted from
// https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/res/res/anim/progress_indeterminate_rotation_material.xml;drc=077b44912b879174cec48a25307f1c19b96c2a78
const int _kIndeterminateCircularDuration = 1333 * 2222;
// The progress value below which the track gap is scaled proportionally to
// prevent a track gap from appearing at 0% progress.
const double _kTrackGapRampDownThreshold = 0.01;
enum _ActivityIndicatorType { material, adaptive }
const String _kValueControllerAssertion =
'A progress indicator cannot have both a value and a controller.\n'
'The "value" property is for a determinate indicator with a specific progress, '
'while the "controller" is for controlling the animation of an indeterminate indicator.\n'
'To resolve this, provide only one of the two properties.';
/// A base class for Material Design progress indicators.
///
/// This widget cannot be instantiated directly. For a linear progress
/// indicator, see [LinearProgressIndicator]. For a circular progress indicator,
/// see [CircularProgressIndicator].
///
/// See also:
///
/// * <https://material.io/components/progress-indicators>
abstract class ProgressIndicator extends StatefulWidget {
/// Creates a progress indicator.
///
/// {@template flutter.material.ProgressIndicator.ProgressIndicator}
/// The [value] argument can either be null for an indeterminate
/// progress indicator, or a non-null value between 0.0 and 1.0 for a
/// determinate progress indicator.
///
/// ## Accessibility
///
/// The [semanticsLabel] can be used to identify the purpose of this progress
/// bar for screen reading software. The [semanticsValue] property may be used
/// for determinate progress indicators to indicate how much progress has been made.
/// {@endtemplate}
const ProgressIndicator({
super.key,
this.value,
this.backgroundColor,
this.color,
this.valueColor,
this.semanticsLabel,
this.semanticsValue,
});
/// If non-null, the value of this progress indicator.
///
/// A value of 0.0 means no progress and 1.0 means that progress is complete.
/// The value will be clamped to be in the range 0.0-1.0.
///
/// If null, this progress indicator is indeterminate, which means the
/// indicator displays a predetermined animation that does not indicate how
/// much actual progress is being made.
final double? value;
double? get _effectiveValue => value == null ? null : clampDouble(value!, 0.0, 1.0);
/// The progress indicator's background color.
///
/// It is up to the subclass to implement this in whatever way makes sense
/// for the given use case. See the subclass documentation for details.
final Color? backgroundColor;
/// {@template flutter.progress_indicator.ProgressIndicator.color}
/// The progress indicator's color.
///
/// This is only used if [ProgressIndicator.valueColor] is null.
/// If [ProgressIndicator.color] is also null, then the ambient
/// [ProgressIndicatorThemeData.color] will be used. If that
/// is null then the current theme's [ColorScheme.primary] will
/// be used by default.
/// {@endtemplate}
final Color? color;
/// The progress indicator's color as an animated value.
///
/// If null, the progress indicator is rendered with [color]. If that is null,
/// then it will use the ambient [ProgressIndicatorThemeData.color]. If that
/// is also null then it defaults to the current theme's [ColorScheme.primary].
final Animation<Color?>? valueColor;
/// {@template flutter.progress_indicator.ProgressIndicator.semanticsLabel}
/// The [SemanticsProperties.label] for this progress indicator.
///
/// This value indicates the purpose of the progress bar, and will be
/// read out by screen readers to indicate the purpose of this progress
/// indicator.
/// {@endtemplate}
final String? semanticsLabel;
/// {@template flutter.progress_indicator.ProgressIndicator.semanticsValue}
/// The [SemanticsProperties.value] for this progress indicator.
///
/// This will be used in conjunction with the [semanticsLabel] by
/// screen reading software to identify the widget, and is primarily
/// intended for use with determinate progress indicators to announce
/// how far along they are.
///
/// For determinate progress indicators, this will be defaulted to
/// [ProgressIndicator.value] expressed as a percentage, i.e. `0.1` will
/// become '10%'.
/// {@endtemplate}
final String? semanticsValue;
Color _getValueColor(BuildContext context, {Color? defaultColor}) {
return valueColor?.value ??
color ??
ProgressIndicatorTheme.of(context).color ??
defaultColor ??
Theme.of(context).colorScheme.primary;
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(PercentProperty('value', value, showName: false, ifNull: '<indeterminate>'));
}
Widget _buildSemanticsWrapper({required BuildContext context, required Widget child}) {
var isProgressBar = false;
String? expandedSemanticsValue = semanticsValue;
if (value != null) {
expandedSemanticsValue ??= '${(_effectiveValue! * 100).round()}';
isProgressBar = true;
}
return Semantics(
label: semanticsLabel,
role: isProgressBar ? SemanticsRole.progressBar : SemanticsRole.loadingSpinner,
minValue: isProgressBar ? '0' : null,
maxValue: isProgressBar ? '100' : null,
value: expandedSemanticsValue,
child: child,
);
}
}
class _LinearProgressIndicatorPainter extends CustomPainter {
const _LinearProgressIndicatorPainter({
required this.trackColor,
required this.valueColor,
this.value,
required this.animationValue,
required this.textDirection,
required this.indicatorBorderRadius,
required this.stopIndicatorColor,
required this.stopIndicatorRadius,
required this.trackGap,
});
final Color trackColor;
final Color valueColor;
final double? value;
final double animationValue;
final TextDirection textDirection;
final BorderRadiusGeometry? indicatorBorderRadius;
final Color? stopIndicatorColor;
final double? stopIndicatorRadius;
final double? trackGap;
// The indeterminate progress animation displays two lines whose leading (head)
// and trailing (tail) endpoints are defined by the following four curves.
static const Curve line1Head = Interval(
0.0,
750.0 / _kIndeterminateLinearDuration,
curve: Cubic(0.2, 0.0, 0.8, 1.0),
);
static const Curve line1Tail = Interval(
333.0 / _kIndeterminateLinearDuration,
(333.0 + 750.0) / _kIndeterminateLinearDuration,
curve: Cubic(0.4, 0.0, 1.0, 1.0),
);
static const Curve line2Head = Interval(
1000.0 / _kIndeterminateLinearDuration,
(1000.0 + 567.0) / _kIndeterminateLinearDuration,
curve: Cubic(0.0, 0.0, 0.65, 1.0),
);
static const Curve line2Tail = Interval(
1267.0 / _kIndeterminateLinearDuration,
(1267.0 + 533.0) / _kIndeterminateLinearDuration,
curve: Cubic(0.10, 0.0, 0.45, 1.0),
);
@override
void paint(Canvas canvas, Size size) {
final double effectiveTrackGap = trackGap ?? 0.0;
void drawLinearIndicator({
required double startFraction,
required double endFraction,
required Color color,
}) {
if (endFraction - startFraction <= 0) {
return;
}
final isLtr = textDirection == TextDirection.ltr;
final double left = (isLtr ? startFraction : 1 - endFraction) * size.width;
final double right = (isLtr ? endFraction : 1 - startFraction) * size.width;
final rect = Rect.fromLTRB(left, 0, right, size.height);
final paint = Paint()..color = color;
if (indicatorBorderRadius != null) {
final RRect rrect = indicatorBorderRadius!.resolve(textDirection).toRRect(rect);
canvas.drawRRect(rrect, paint);
} else {
canvas.drawRect(rect, paint);
}
}
void drawStopIndicator() {
// Limit the stop indicator to the height of the indicator.
final double maxRadius = size.height / 2;
final double radius = math.min(stopIndicatorRadius!, maxRadius);
final indicatorPaint = Paint()..color = stopIndicatorColor!;
final Offset position = switch (textDirection) {
TextDirection.rtl => Offset(maxRadius, maxRadius),
TextDirection.ltr => Offset(size.width - maxRadius, maxRadius),
};
canvas.drawCircle(position, radius, indicatorPaint);
}
// Calculates a track gap fraction that is scaled proportionally to a given
// value.
// This is used for a smooth transition of the track gap's size, preventing
// it from appearing or disappearing abruptly. The returned value increases
// linearly from 0 to the full `trackGapFraction` as `currentValue`
// increases from 0 to `_kTrackGapRampDownThreshold`.
double getEffectiveTrackGapFraction(double currentValue, double trackGapFraction) {
return trackGapFraction *
clampDouble(currentValue, 0, _kTrackGapRampDownThreshold) /
_kTrackGapRampDownThreshold;
}
final double trackGapFraction = effectiveTrackGap / size.width;
final double? effectiveValue = value == null ? null : clampDouble(value!, 0.0, 1.0);
// Determinate progress indicator.
if (effectiveValue != null) {
final double trackStartFraction = trackGapFraction > 0
? effectiveValue + getEffectiveTrackGapFraction(effectiveValue, trackGapFraction)
: 0;
// Draw the track when there is still space.
if (trackStartFraction < 1) {
drawLinearIndicator(startFraction: trackStartFraction, endFraction: 1, color: trackColor);
}
// Draw the stop indicator.
if (stopIndicatorRadius != null && stopIndicatorRadius! > 0) {
drawStopIndicator();
}
// Draw the active indicator.
if (effectiveValue > 0) {
drawLinearIndicator(startFraction: 0, endFraction: effectiveValue, color: valueColor);
}
return;
}
// Indeterminate progress indicator.
// For LTR text direction the `head` is the right endpoint and the `tail` is
// the left endpoint.
final double firstLineHead = line1Head.transform(animationValue);
final double firstLineTail = line1Tail.transform(animationValue);
final double secondLineHead = line2Head.transform(animationValue);
final double secondLineTail = line2Tail.transform(animationValue);
// Draw the track before line 1. Assuming text direction is LTR, this track
// appears on the right side of line 1.
if (firstLineHead < 1 - trackGapFraction) {
final double trackStartFraction = firstLineHead > 0
? firstLineHead + getEffectiveTrackGapFraction(firstLineHead, trackGapFraction)
: 0;
drawLinearIndicator(startFraction: trackStartFraction, endFraction: 1, color: trackColor);
}
// Draw the line 1.
if (firstLineHead - firstLineTail > 0) {
drawLinearIndicator(
startFraction: firstLineTail,
endFraction: firstLineHead,
color: valueColor,
);
}
// Draw the track between line 1 and line 2. Assuming text direction is
// LTR, this track appears on the left side of line 1 and on the right side
// of line 2.
if (firstLineTail > trackGapFraction) {
final double trackStartFraction = secondLineHead > 0
? secondLineHead + getEffectiveTrackGapFraction(secondLineHead, trackGapFraction)
: 0;
final double trackEndFraction = firstLineTail < 1
? firstLineTail - getEffectiveTrackGapFraction(1 - firstLineTail, trackGapFraction)
: 1;
drawLinearIndicator(
startFraction: trackStartFraction,
endFraction: trackEndFraction,
color: trackColor,
);
}
// Draw the line 2.
if (secondLineHead - secondLineTail > 0) {
drawLinearIndicator(
startFraction: secondLineTail,
endFraction: secondLineHead,
color: valueColor,
);
}
// Draw the track after line 2. Assuming text direction is LTR, this track
// appears on the left side of line 2.
if (secondLineTail > trackGapFraction) {
final double trackEndFraction = secondLineTail < 1
? secondLineTail - getEffectiveTrackGapFraction(1 - secondLineTail, trackGapFraction)
: 1;
drawLinearIndicator(startFraction: 0, endFraction: trackEndFraction, color: trackColor);
}
}
@override
bool shouldRepaint(_LinearProgressIndicatorPainter oldPainter) {
return oldPainter.trackColor != trackColor ||
oldPainter.valueColor != valueColor ||
oldPainter.value != value ||
oldPainter.animationValue != animationValue ||
oldPainter.textDirection != textDirection ||
oldPainter.indicatorBorderRadius != indicatorBorderRadius ||
oldPainter.stopIndicatorColor != stopIndicatorColor ||
oldPainter.stopIndicatorRadius != stopIndicatorRadius ||
oldPainter.trackGap != trackGap;
}
}
/// A Material Design linear progress indicator, also known as a progress bar.
///
/// {@youtube 560 315 https://www.youtube.com/watch?v=O-rhXZLtpv0}
///
/// A widget that shows progress along a line. There are two kinds of linear
/// progress indicators:
///
/// * _Determinate_. Determinate progress indicators have a specific value at
/// each point in time, and the value should increase monotonically from 0.0
/// to 1.0, at which time the indicator is complete. To create a determinate
/// progress indicator, use a non-null [value] between 0.0 and 1.0.
/// * _Indeterminate_. Indeterminate progress indicators do not have a specific
/// value at each point in time and instead indicate that progress is being
/// made without indicating how much progress remains. To create an
/// indeterminate progress indicator, use a null [value].
///
/// The indicator line is displayed with [valueColor], an animated value. To
/// specify a constant color value use: `AlwaysStoppedAnimation<Color>(color)`.
///
/// The minimum height of the indicator can be specified using [minHeight].
/// The indicator can be made taller by wrapping the widget with a [SizedBox].
///
/// {@tool dartpad}
/// This example showcases determinate and indeterminate [LinearProgressIndicator]s.
/// The [LinearProgressIndicator]s will use the 
/// when setting the [LinearProgressIndicator.year2023] flag to false.
///
/// ** See code in examples/api/lib/material/progress_indicator/linear_progress_indicator.0.dart **
/// {@end-tool}
///
/// {@tool dartpad}
/// This sample shows the creation of a [LinearProgressIndicator] with a changing value.
/// When toggling the switch, [LinearProgressIndicator] uses a determinate value.
/// As described in: https://m3.material.io/components/progress-indicators/overview
///
/// ** See code in examples/api/lib/material/progress_indicator/linear_progress_indicator.1.dart **
/// {@end-tool}
///
/// {@macro flutter.material.ProgressIndicator.AnimationSynchronization}
///
/// See the documentation of [CircularProgressIndicator] for an example on this
/// topic.
///
/// See also:
///
/// * [CircularProgressIndicator], which shows progress along a circular arc.
/// * [RefreshIndicator], which automatically displays a [CircularProgressIndicator]
/// when the underlying vertical scrollable is overscrolled.
/// * <https://material.io/design/components/progress-indicators.html#linear-progress-indicators>
class LinearProgressIndicator extends ProgressIndicator {
/// Creates a linear progress indicator.
///
/// {@macro flutter.material.ProgressIndicator.ProgressIndicator}
const LinearProgressIndicator({
super.key,
super.value,
super.backgroundColor,
super.color,
super.valueColor,
this.minHeight,
super.semanticsLabel,
super.semanticsValue,
this.borderRadius,
this.stopIndicatorColor,
this.stopIndicatorRadius,
this.trackGap,
@Deprecated(
'Set this flag to false to opt into the 2024 progress indicator appearance. Defaults to true. '
'In the future, this flag will default to false. Use ProgressIndicatorThemeData to customize individual properties. '
'This feature was deprecated after v3.26.0-0.1.pre.',
)
this.year2023,
this.controller,
}) : assert(minHeight == null || minHeight > 0),
assert(value == null || controller == null, _kValueControllerAssertion);
/// {@template flutter.material.LinearProgressIndicator.trackColor}
/// Color of the track being filled by the linear indicator.
///
/// If [LinearProgressIndicator.backgroundColor] is null then the
/// ambient [ProgressIndicatorThemeData.linearTrackColor] will be used.
/// If that is null, then the ambient theme's [ColorScheme.background]
/// will be used to draw the track.
/// {@endtemplate}
@override
Color? get backgroundColor => super.backgroundColor;
/// {@template flutter.material.LinearProgressIndicator.minHeight}
/// The minimum height of the line used to draw the linear indicator.
///
/// If [LinearProgressIndicator.minHeight] is null then it will use the
/// ambient [ProgressIndicatorThemeData.linearMinHeight]. If that is null
/// it will use 4dp.
/// {@endtemplate}
final double? minHeight;
/// The border radius of both the indicator and the track.
///
/// If null, then the [ProgressIndicatorThemeData.borderRadius] will be used.
/// If that is also null, then defaults to radius of 2, which produces a
/// rounded shape with a rounded indicator. If [ThemeData.useMaterial3] is false,
/// then defaults to [BorderRadius.zero], which produces a rectangular shape
/// with a rectangular indicator.
final BorderRadiusGeometry? borderRadius;
/// The color of the stop indicator.
///
/// If [year2023] is true or [ThemeData.useMaterial3] is false, then no stop
/// indicator will be drawn.
///
/// If null, then the [ProgressIndicatorThemeData.stopIndicatorColor] will be used.
/// If that is null, then the [ColorScheme.primary] will be used.
final Color? stopIndicatorColor;
/// The radius of the stop indicator.
///
/// If [year2023] is true or [ThemeData.useMaterial3] is false, then no stop
/// indicator will be drawn.
///
/// Set [stopIndicatorRadius] to 0 to hide the stop indicator.
///
/// If null, then the [ProgressIndicatorThemeData.stopIndicatorRadius] will be used.
/// If that is null, then defaults to 2.
final double? stopIndicatorRadius;
/// The gap between the indicator and the track.
///
/// If [year2023] is true or [ThemeData.useMaterial3] is false, then no track
/// gap will be drawn.
///
/// Set [trackGap] to 0 to hide the track gap.
///
/// If null, then the [ProgressIndicatorThemeData.trackGap] will be used.
/// If that is null, then defaults to 4.
final double? trackGap;
/// When true, the [LinearProgressIndicator] will use the 2023 Material Design 3
/// appearance.
///
/// If null, then the [ProgressIndicatorThemeData.year2023] will be used.
/// If that is null, then defaults to true.
///
/// If this is set to false, the [LinearProgressIndicator] will use the
/// latest Material Design 3 appearance, which was introduced in December 2023.
///
/// If [ThemeData.useMaterial3] is false, then this property is ignored.
@Deprecated(
'Set this flag to false to opt into the 2024 progress indicator appearance. Defaults to true. '
'In the future, this flag will default to false. Use ProgressIndicatorThemeData to customize individual properties. '
'This feature was deprecated after v3.27.0-0.1.pre.',
)
final bool? year2023;
/// {@template flutter.material.ProgressIndicator.controller}
/// An optional [AnimationController] that controls the animation of this
/// indeterminate progress indicator.
///
/// This controller is only used when the indicator is indeterminate (i.e.,
/// when [value] is null). If this property is non-null, [value] must be null.
///
/// The controller's value is expected to be a linear progression from 0.0 to
/// 1.0, which represents one full cycle of the indeterminate animation.
///
/// If this controller is null (and [value] is also null), the widget will
/// look for a [ProgressIndicatorThemeData.controller]. If that is also null,
/// the widget will create and manage its own internal [AnimationController]
/// to drive the default indeterminate animation.
/// {@endtemplate}
///
/// See also:
///
/// * [LinearProgressIndicator.defaultAnimationDuration], default duration
/// for one full cycle of the indeterminate animation.
final AnimationController? controller;
/// The default duration for one full cycle of the indeterminate animation.
///
/// This duration is used when the widget creates its own [AnimationController]
/// because no [controller] was provided, either directly or through a
/// [ProgressIndicatorTheme].
static const Duration defaultAnimationDuration = Duration(
milliseconds: _kIndeterminateLinearDuration,
);
@override
State<LinearProgressIndicator> createState() => _LinearProgressIndicatorState();
}
class _LinearProgressIndicatorState extends State<LinearProgressIndicator>
with SingleTickerProviderStateMixin {
late final AnimationController _internalController;
@override
void initState() {
super.initState();
_internalController = AnimationController(
duration: LinearProgressIndicator.defaultAnimationDuration,
vsync: this,
);
_updateControllerAnimatingStatus();
}
@override
void didUpdateWidget(LinearProgressIndicator oldWidget) {
super.didUpdateWidget(oldWidget);
_updateControllerAnimatingStatus();
}
@override
void dispose() {
_internalController.dispose();
super.dispose();
}
AnimationController get _controller =>
widget.controller ??
context.getInheritedWidgetOfExactType<ProgressIndicatorTheme>()?.data.controller ??
context.findAncestorWidgetOfExactType<Theme>()?.data.progressIndicatorTheme.controller ??
_internalController;
void _updateControllerAnimatingStatus() {
if (widget._effectiveValue == null && !_internalController.isAnimating) {
_internalController.repeat();
} else if (widget._effectiveValue != null && _internalController.isAnimating) {
_internalController.stop();
}
}
Widget _buildIndicator(BuildContext context, double animationValue, TextDirection textDirection) {
final ProgressIndicatorThemeData indicatorTheme = ProgressIndicatorTheme.of(context);
final bool year2023 = widget.year2023 ?? indicatorTheme.year2023 ?? true;
final ProgressIndicatorThemeData defaults = switch (Theme.of(context).useMaterial3) {
true =>
year2023
? _LinearProgressIndicatorDefaultsM3Year2023(context)
: _LinearProgressIndicatorDefaultsM3(context),
false => _LinearProgressIndicatorDefaultsM2(context),
};
final Color trackColor =
widget.backgroundColor ?? indicatorTheme.linearTrackColor ?? defaults.linearTrackColor!;
final double minHeight =
widget.minHeight ?? indicatorTheme.linearMinHeight ?? defaults.linearMinHeight!;
final BorderRadiusGeometry? borderRadius =
widget.borderRadius ?? indicatorTheme.borderRadius ?? defaults.borderRadius;
final Color? stopIndicatorColor = !year2023
? widget.stopIndicatorColor ??
indicatorTheme.stopIndicatorColor ??
defaults.stopIndicatorColor
: null;
final double? stopIndicatorRadius = !year2023
? widget.stopIndicatorRadius ??
indicatorTheme.stopIndicatorRadius ??
defaults.stopIndicatorRadius
: null;
final double? trackGap = !year2023
? widget.trackGap ?? indicatorTheme.trackGap ?? defaults.trackGap
: null;
Widget result = ConstrainedBox(
constraints: BoxConstraints(minWidth: double.infinity, minHeight: minHeight),
child: CustomPaint(
painter: _LinearProgressIndicatorPainter(
trackColor: trackColor,
valueColor: widget._getValueColor(context, defaultColor: defaults.color),
value: widget._effectiveValue, // may be null
animationValue: animationValue, // ignored if widget._effectiveValue is not null
textDirection: textDirection,
indicatorBorderRadius: borderRadius,
stopIndicatorColor: stopIndicatorColor,
stopIndicatorRadius: stopIndicatorRadius,
trackGap: trackGap,
),
),
);
// Clip is only needed with indeterminate progress indicators
if (borderRadius != null && widget._effectiveValue == null) {
result = ClipRRect(borderRadius: borderRadius, child: result);
}
return widget._buildSemanticsWrapper(context: context, child: result);
}
@override
Widget build(BuildContext context) {
final TextDirection textDirection = Directionality.of(context);
if (widget._effectiveValue != null) {
return _buildIndicator(context, _controller.value, textDirection);
}
return AnimatedBuilder(
animation: _controller.view,
builder: (BuildContext context, Widget? child) {
return _buildIndicator(context, _controller.value, textDirection);
},
);
}
}
class _CircularProgressIndicatorPainter extends CustomPainter {
_CircularProgressIndicatorPainter({
this.trackColor,
required this.valueColor,
required this.value,
required this.headValue,
required this.tailValue,
required this.offsetValue,
required this.rotationValue,
required this.strokeWidth,
required this.strokeAlign,
this.strokeCap,
this.trackGap,
this.year2023 = true,
}) : arcStart = value != null
? _startAngle
: _startAngle +
tailValue * 3 / 2 * math.pi +
rotationValue * math.pi * 2.0 +
offsetValue * 0.5 * math.pi,
arcSweep = value != null
? clampDouble(value, 0.0, 1.0) * _sweep
: math.max(headValue * 3 / 2 * math.pi - tailValue * 3 / 2 * math.pi, _epsilon);
final Color? trackColor;
final Color valueColor;
final double? value;
final double headValue;
final double tailValue;
final double offsetValue;
final double rotationValue;
final double strokeWidth;
final double strokeAlign;
final double arcStart;
final double arcSweep;
final StrokeCap? strokeCap;
final double? trackGap;
final bool year2023;
static const double _twoPi = math.pi * 2.0;
static const double _epsilon = .001;
// Canvas.drawArc(r, 0, 2*PI) doesn't draw anything, so just get close.
static const double _sweep = _twoPi - _epsilon;
static const double _startAngle = -math.pi / 2.0;
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = valueColor
..strokeWidth = strokeWidth
..style = PaintingStyle.stroke;
// Use the negative operator as intended to keep the exposed constant value
// as users are already familiar with.
final double strokeOffset = strokeWidth / 2 * -strokeAlign;
final arcBaseOffset = Offset(strokeOffset, strokeOffset);
final arcActualSize = Size(size.width - strokeOffset * 2, size.height - strokeOffset * 2);
final bool hasGap = trackGap != null && trackGap! > 0;
if (trackColor != null) {
final backgroundPaint = Paint()
..color = trackColor!
..strokeWidth = strokeWidth
..strokeCap = strokeCap ?? StrokeCap.round
..style = PaintingStyle.stroke;
// If hasGap is true, draw the background arc with a gap.
if (hasGap && value != null && value! > _epsilon) {
final double arcRadius = arcActualSize.shortestSide / 2;
final double strokeRadius = strokeWidth / arcRadius;
final double gapRadius = trackGap! / arcRadius;
final double startGap = strokeRadius + gapRadius;
final double endGap = value! < _epsilon ? startGap : startGap * 2;
final double startSweep = (-math.pi / 2.0) + startGap;
final double endSweep = math.max(
0.0,
_twoPi - clampDouble(value!, 0.0, 1.0) * _twoPi - endGap,
);
// Flip the canvas for the background arc.
canvas.save();
canvas.scale(-1, 1);
canvas.translate(-size.width, 0);
canvas.drawArc(arcBaseOffset & arcActualSize, startSweep, endSweep, false, backgroundPaint);
// Restore the canvas to draw the foreground arc.
canvas.restore();
} else {
canvas.drawArc(arcBaseOffset & arcActualSize, 0, _sweep, false, backgroundPaint);
}
}
if (year2023) {
if (value == null && strokeCap == null) {
// Indeterminate
paint.strokeCap = StrokeCap.square;
} else {
// Butt when determinate (value != null) && strokeCap == null;
paint.strokeCap = strokeCap ?? StrokeCap.butt;
}
} else {
paint.strokeCap = strokeCap ?? StrokeCap.round;
}
canvas.drawArc(arcBaseOffset & arcActualSize, arcStart, arcSweep, false, paint);
}
@override
bool shouldRepaint(_CircularProgressIndicatorPainter oldPainter) {
return oldPainter.trackColor != trackColor ||
oldPainter.valueColor != valueColor ||
oldPainter.value != value ||
oldPainter.headValue != headValue ||
oldPainter.tailValue != tailValue ||
oldPainter.offsetValue != offsetValue ||
oldPainter.rotationValue != rotationValue ||
oldPainter.strokeWidth != strokeWidth ||
oldPainter.strokeAlign != strokeAlign ||
oldPainter.strokeCap != strokeCap ||
oldPainter.trackGap != trackGap ||
oldPainter.year2023 != year2023;
}
}
/// A Material Design circular progress indicator, which spins to indicate that
/// the application is busy.
///
/// {@youtube 560 315 https://www.youtube.com/watch?v=O-rhXZLtpv0}
///
/// A widget that shows progress along a circle. There are two kinds of circular
/// progress indicators:
///
/// * _Determinate_. Determinate progress indicators have a specific value at
/// each point in time, and the value should increase monotonically from 0.0
/// to 1.0, at which time the indicator is complete. To create a determinate
/// progress indicator, use a non-null [value] between 0.0 and 1.0.
/// * _Indeterminate_. Indeterminate progress indicators do not have a specific
/// value at each point in time and instead indicate that progress is being
/// made without indicating how much progress remains. To create an
/// indeterminate progress indicator, use a null [value].
///
/// The indicator arc is displayed with [valueColor], an animated value. To
/// specify a constant color use: `AlwaysStoppedAnimation<Color>(color)`.
///
/// {@tool dartpad}
/// This example showcases determinate and indeterminate [CircularProgressIndicator]s.
/// The [CircularProgressIndicator]s will use the 
/// when setting the [CircularProgressIndicator.year2023] flag to false.
///
/// ** See code in examples/api/lib/material/progress_indicator/circular_progress_indicator.0.dart **
/// {@end-tool}
///
/// {@tool dartpad}
/// This sample shows the creation of a [CircularProgressIndicator] with a changing value.
/// When toggling the switch, [CircularProgressIndicator] uses a determinate value.
/// As described in: https://m3.material.io/components/progress-indicators/overview
///
/// ** See code in examples/api/lib/material/progress_indicator/circular_progress_indicator.1.dart **
/// {@end-tool}
///
/// {@template flutter.material.ProgressIndicator.AnimationSynchronization}
/// ## Animation synchronization
///
/// When multiple [CircularProgressIndicator]s or [LinearProgressIndicator]s are
/// animating on screen simultaneously (e.g., in a list of loading items), their
/// uncoordinated animations can appear visually cluttered. To address this, the
/// animation of an indicator can be driven by a custom [AnimationController].
///
/// This allows multiple indicators to be synchronized to a single animation
/// source. The most convenient way to achieve this for a group of indicators is
/// by providing a controller via [ProgressIndicatorTheme] (see
/// [ProgressIndicatorThemeData.controller]). All [CircularProgressIndicator]s
/// or [LinearProgressIndicator]s within that theme's subtree will then share
/// the same animation, resulting in a more coordinated and visually pleasing
/// effect.
///
/// Alternatively, a specific [AnimationController] can be passed directly to the
/// [controller] property of an individual indicator.
/// {@endtemplate}
///
/// {@tool dartpad}
/// This sample demonstrates how to synchronize the indeterminate animations
/// of multiple [CircularProgressIndicator]s using a [Theme].
///
/// Tapping the buttons adds or removes indicators. By default, they all
/// share a [ProgressIndicatorThemeData.controller], which keeps their
/// animations in sync.
///
/// Tapping the "Toggle" button sets the theme's controller to null.
/// This forces each indicator to create its own internal controller,
/// causing their animations to become desynchronized.
///
/// ** See code in examples/api/lib/material/progress_indicator/circular_progress_indicator.2.dart **
/// {@end-tool}
///
/// See also:
///
/// * [LinearProgressIndicator], which displays progress along a line.
/// * [RefreshIndicator], which automatically displays a [CircularProgressIndicator]
/// when the underlying vertical scrollable is overscrolled.
/// * <https://material.io/design/components/progress-indicators.html#circular-progress-indicators>
class CircularProgressIndicator extends ProgressIndicator {
/// Creates a circular progress indicator.
///
/// {@macro flutter.material.ProgressIndicator.ProgressIndicator}
const CircularProgressIndicator({
super.key,
super.value,
super.backgroundColor,
super.color,
super.valueColor,
this.strokeWidth,
this.strokeAlign,
super.semanticsLabel,
super.semanticsValue,
this.strokeCap,
this.constraints,
this.trackGap,
@Deprecated(
'Set this flag to false to opt into the 2024 progress indicator appearance. Defaults to true. '
'In the future, this flag will default to false. Use ProgressIndicatorThemeData to customize individual properties. '
'This feature was deprecated after v3.27.0-0.1.pre.',
)
this.year2023,
this.padding,
this.controller,
}) : assert(value == null || controller == null, _kValueControllerAssertion),
_indicatorType = _ActivityIndicatorType.material;
/// Creates an adaptive progress indicator that is a
/// [CupertinoActivityIndicator] on [TargetPlatform.iOS] &
/// [TargetPlatform.macOS] and a [CircularProgressIndicator] in material
/// theme/non-Apple platforms.
///
/// The [valueColor], [strokeWidth], [strokeAlign], [strokeCap],
/// [semanticsLabel], [semanticsValue], [trackGap], [year2023] will be
/// ignored on iOS & macOS.
///
/// {@macro flutter.material.ProgressIndicator.ProgressIndicator}
const CircularProgressIndicator.adaptive({
super.key,
super.value,
super.backgroundColor,
super.valueColor,
this.strokeWidth,
super.semanticsLabel,
super.semanticsValue,
this.strokeCap,
this.strokeAlign,
this.constraints,
this.trackGap,
@Deprecated(
'Set this flag to false to opt into the 2024 progress indicator appearance. Defaults to true. '
'In the future, this flag will default to false. Use ProgressIndicatorThemeData to customize individual properties. '
'This feature was deprecated after v3.27.0-0.2.pre.',
)
this.year2023,
this.padding,
this.controller,
}) : assert(value == null || controller == null, _kValueControllerAssertion),
_indicatorType = _ActivityIndicatorType.adaptive;
final _ActivityIndicatorType _indicatorType;
/// {@template flutter.material.CircularProgressIndicator.trackColor}
/// Color of the circular track being filled by the circular indicator.
///
/// If [CircularProgressIndicator.backgroundColor] is null then the
/// ambient [ProgressIndicatorThemeData.circularTrackColor] will be used.
/// If that is null, then the track will not be painted.
/// {@endtemplate}
@override
Color? get backgroundColor => super.backgroundColor;
/// The width of the line used to draw the circle.
final double? strokeWidth;
/// The relative position of the stroke on a [CircularProgressIndicator].
///
/// Values typically range from -1.0 ([strokeAlignInside], inside stroke)
/// to 1.0 ([strokeAlignOutside], outside stroke),
/// without any bound constraints (e.g., a value of -2.0 is not typical, but allowed).
/// A value of 0 ([strokeAlignCenter]) will center the border
/// on the edge of the widget.
///
/// If [year2023] is true, then the default value is [strokeAlignCenter].
/// Otherwise, the default value is [strokeAlignInside].
final double? strokeAlign;
/// The progress indicator's line ending.
///
/// This determines the shape of the stroke ends of the progress indicator.
/// By default, [strokeCap] is null.
/// When [value] is null (indeterminate), the stroke ends are set to
/// [StrokeCap.square]. When [value] is not null, the stroke
/// ends are set to [StrokeCap.butt].
///
/// Setting [strokeCap] to [StrokeCap.round] will result in a rounded end.
/// Setting [strokeCap] to [StrokeCap.butt] with [value] == null will result
/// in a slightly different indeterminate animation; the indicator completely
/// disappears and reappears on its minimum value.
/// Setting [strokeCap] to [StrokeCap.square] with [value] != null will
/// result in a different display of [value]. The indicator will start
/// drawing from slightly less than the start, and end slightly after
/// the end. This will produce an alternative result, as the
/// default behavior, for example, that a [value] of 0.5 starts at 90 degrees
/// and ends at 270 degrees. With [StrokeCap.square], it could start 85
/// degrees and end at 275 degrees.
final StrokeCap? strokeCap;
/// Defines minimum and maximum sizes for a [CircularProgressIndicator].
///
/// If null, then the [ProgressIndicatorThemeData.constraints] will be used.
/// Otherwise, defaults to a minimum width and height of 36 pixels.
final BoxConstraints? constraints;
/// The gap between the active indicator and the background track.
///
/// If [year2023] is true or [ThemeData.useMaterial3] is false, then no track
/// gap will be drawn.
///
/// Set [trackGap] to 0 to hide the track gap.
///
/// If null, then the [ProgressIndicatorThemeData.trackGap] will be used.
/// If that is null, then defaults to 4.
final double? trackGap;
/// When true, the [CircularProgressIndicator] will use the 2023 Material Design 3
/// appearance.
///
/// If null, then the [ProgressIndicatorThemeData.year2023] will be used.
/// If that is null, then defaults to true.
///
/// If this is set to false, the [CircularProgressIndicator] will use the
/// latest Material Design 3 appearance, which was introduced in December 2023.
///
/// If [ThemeData.useMaterial3] is false, then this property is ignored.
@Deprecated(
'Set this flag to false to opt into the 2024 progress indicator appearance. Defaults to true. '