Skip to content

Commit e167162

Browse files
authored
Updated ProgressIndicator to M3 (#112139)
1 parent 71c21d6 commit e167162

File tree

5 files changed

+457
-213
lines changed

5 files changed

+457
-213
lines changed

dev/tools/gen_defaults/bin/gen_defaults.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import 'package:gen_defaults/input_chip_template.dart';
3131
import 'package:gen_defaults/input_decorator_template.dart';
3232
import 'package:gen_defaults/navigation_bar_template.dart';
3333
import 'package:gen_defaults/navigation_rail_template.dart';
34+
import 'package:gen_defaults/progress_indicator_template.dart';
3435
import 'package:gen_defaults/radio_template.dart';
3536
import 'package:gen_defaults/surface_tint.dart';
3637
import 'package:gen_defaults/switch_template.dart';
@@ -80,6 +81,8 @@ Future<void> main(List<String> args) async {
8081
'navigation_drawer.json',
8182
'navigation_rail.json',
8283
'palette.json',
84+
'progress_indicator_circular.json',
85+
'progress_indicator_linear.json',
8386
'radio_button.json',
8487
'segmented_button_outlined.json',
8588
'shape.json',
@@ -126,6 +129,7 @@ Future<void> main(List<String> args) async {
126129
InputDecoratorTemplate('InputDecorator', '$materialLib/input_decorator.dart', tokens).updateFile();
127130
NavigationBarTemplate('NavigationBar', '$materialLib/navigation_bar.dart', tokens).updateFile();
128131
NavigationRailTemplate('NavigationRail', '$materialLib/navigation_rail.dart', tokens).updateFile();
132+
ProgressIndicatorTemplate('ProgressIndicator', '$materialLib/progress_indicator.dart', tokens).updateFile();
129133
RadioTemplate('Radio<T>', '$materialLib/radio.dart', tokens).updateFile();
130134
SurfaceTintTemplate('SurfaceTint', '$materialLib/elevation_overlay.dart', tokens).updateFile();
131135
SwitchTemplate('Switch', '$materialLib/switch.dart', tokens).updateFile();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'template.dart';
6+
7+
class ProgressIndicatorTemplate extends TokenTemplate {
8+
const ProgressIndicatorTemplate(super.blockName, super.fileName, super.tokens, {
9+
super.colorSchemePrefix = '_colors.',
10+
});
11+
12+
@override
13+
String generate() => '''
14+
class _Circular${blockName}DefaultsM3 extends ProgressIndicatorThemeData {
15+
_Circular${blockName}DefaultsM3(this.context);
16+
17+
final BuildContext context;
18+
late final ColorScheme _colors = Theme.of(context).colorScheme;
19+
20+
static const double circularProgressIndicatorSize = ${tokens['md.comp.circular-progress-indicator.size']};
21+
22+
@override
23+
Color get color => ${componentColor('md.comp.circular-progress-indicator.active-indicator')};
24+
}
25+
26+
class _Linear${blockName}DefaultsM3 extends ProgressIndicatorThemeData {
27+
_Linear${blockName}DefaultsM3(this.context);
28+
29+
final BuildContext context;
30+
late final ColorScheme _colors = Theme.of(context).colorScheme;
31+
32+
@override
33+
Color get color => ${componentColor('md.comp.linear-progress-indicator.active-indicator')};
34+
35+
@override
36+
Color get linearTrackColor => ${componentColor('md.comp.linear-progress-indicator.track')};
37+
38+
@override
39+
double get linearMinHeight => ${tokens['md.comp.linear-progress-indicator.track.height']};
40+
}
41+
''';
42+
}

packages/flutter/lib/src/material/progress_indicator.dart

Lines changed: 112 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ abstract class ProgressIndicator extends StatefulWidget {
108108
/// {@endtemplate}
109109
final String? semanticsValue;
110110

111-
Color _getValueColor(BuildContext context) {
112-
return
113-
valueColor?.value ??
111+
Color _getValueColor(BuildContext context, {Color? defaultColor}) {
112+
return valueColor?.value ??
114113
color ??
115114
ProgressIndicatorTheme.of(context).color ??
115+
defaultColor ??
116116
Theme.of(context).colorScheme.primary;
117117
}
118118

@@ -331,12 +331,17 @@ class _LinearProgressIndicatorState extends State<LinearProgressIndicator> with
331331
}
332332

333333
Widget _buildIndicator(BuildContext context, double animationValue, TextDirection textDirection) {
334+
final ProgressIndicatorThemeData defaults = Theme.of(context).useMaterial3
335+
? _LinearProgressIndicatorDefaultsM3(context)
336+
: _LinearProgressIndicatorDefaultsM2(context);
337+
334338
final ProgressIndicatorThemeData indicatorTheme = ProgressIndicatorTheme.of(context);
335-
final Color trackColor =
336-
widget.backgroundColor ??
339+
final Color trackColor = widget.backgroundColor ??
337340
indicatorTheme.linearTrackColor ??
338-
Theme.of(context).colorScheme.background;
339-
final double minHeight = widget.minHeight ?? indicatorTheme.linearMinHeight ?? 4.0;
341+
defaults.linearTrackColor!;
342+
final double minHeight = widget.minHeight ??
343+
indicatorTheme.linearMinHeight ??
344+
defaults.linearMinHeight!;
340345

341346
return widget._buildSemanticsWrapper(
342347
context: context,
@@ -348,7 +353,7 @@ class _LinearProgressIndicatorState extends State<LinearProgressIndicator> with
348353
child: CustomPaint(
349354
painter: _LinearProgressIndicatorPainter(
350355
backgroundColor: trackColor,
351-
valueColor: widget._getValueColor(context),
356+
valueColor: widget._getValueColor(context, defaultColor: defaults.color),
352357
value: widget.value, // may be null
353358
animationValue: animationValue, // ignored if widget.value is not null
354359
textDirection: textDirection,
@@ -580,29 +585,44 @@ class _CircularProgressIndicatorState extends State<CircularProgressIndicator> w
580585
}
581586

582587
Widget _buildMaterialIndicator(BuildContext context, double headValue, double tailValue, double offsetValue, double rotationValue) {
588+
final ProgressIndicatorThemeData defaults = Theme.of(context).useMaterial3
589+
? _CircularProgressIndicatorDefaultsM3(context)
590+
: _CircularProgressIndicatorDefaultsM2(context);
583591
final Color? trackColor = widget.backgroundColor ?? ProgressIndicatorTheme.of(context).circularTrackColor;
584592

585-
return widget._buildSemanticsWrapper(
586-
context: context,
587-
child: Container(
588-
constraints: const BoxConstraints(
589-
minWidth: _kMinCircularProgressIndicatorSize,
590-
minHeight: _kMinCircularProgressIndicatorSize,
591-
),
592-
child: CustomPaint(
593-
painter: _CircularProgressIndicatorPainter(
594-
backgroundColor: trackColor,
595-
valueColor: widget._getValueColor(context),
596-
value: widget.value, // may be null
597-
headValue: headValue, // remaining arguments are ignored if widget.value is not null
598-
tailValue: tailValue,
599-
offsetValue: offsetValue,
600-
rotationValue: rotationValue,
601-
strokeWidth: widget.strokeWidth,
602-
),
593+
Widget progressIndicator = Container(
594+
constraints: const BoxConstraints(
595+
minWidth: _kMinCircularProgressIndicatorSize,
596+
minHeight: _kMinCircularProgressIndicatorSize,
597+
),
598+
child: CustomPaint(
599+
painter: _CircularProgressIndicatorPainter(
600+
backgroundColor: trackColor,
601+
valueColor: widget._getValueColor(context, defaultColor: defaults.color),
602+
value: widget.value, // may be null
603+
headValue: headValue, // remaining arguments are ignored if widget.value is not null
604+
tailValue: tailValue,
605+
offsetValue: offsetValue,
606+
rotationValue: rotationValue,
607+
strokeWidth: widget.strokeWidth,
603608
),
604609
),
605610
);
611+
612+
if (Theme.of(context).useMaterial3) {
613+
progressIndicator = SizedBox(
614+
height: _CircularProgressIndicatorDefaultsM3.circularProgressIndicatorSize,
615+
width: _CircularProgressIndicatorDefaultsM3.circularProgressIndicatorSize,
616+
child: Center(
617+
child: progressIndicator
618+
),
619+
);
620+
}
621+
622+
return widget._buildSemanticsWrapper(
623+
context: context,
624+
child: progressIndicator,
625+
);
606626
}
607627

608628
Widget _buildAnimation() {
@@ -866,3 +886,69 @@ class _RefreshProgressIndicatorState extends _CircularProgressIndicatorState {
866886
);
867887
}
868888
}
889+
890+
// Hand coded defaults based on Material Design 2.
891+
class _CircularProgressIndicatorDefaultsM2 extends ProgressIndicatorThemeData {
892+
_CircularProgressIndicatorDefaultsM2(this.context);
893+
894+
final BuildContext context;
895+
late final ColorScheme _colors = Theme.of(context).colorScheme;
896+
897+
@override
898+
Color get color => _colors.primary;
899+
}
900+
901+
class _LinearProgressIndicatorDefaultsM2 extends ProgressIndicatorThemeData {
902+
_LinearProgressIndicatorDefaultsM2(this.context);
903+
904+
final BuildContext context;
905+
late final ColorScheme _colors = Theme.of(context).colorScheme;
906+
907+
@override
908+
Color get color => _colors.primary;
909+
910+
@override
911+
Color get linearTrackColor => _colors.background;
912+
913+
@override
914+
double get linearMinHeight => 4.0;
915+
}
916+
917+
// BEGIN GENERATED TOKEN PROPERTIES - ProgressIndicator
918+
919+
// Do not edit by hand. The code between the "BEGIN GENERATED" and
920+
// "END GENERATED" comments are generated from data in the Material
921+
// Design token database by the script:
922+
// dev/tools/gen_defaults/bin/gen_defaults.dart.
923+
924+
// Token database version: v0_132
925+
926+
class _CircularProgressIndicatorDefaultsM3 extends ProgressIndicatorThemeData {
927+
_CircularProgressIndicatorDefaultsM3(this.context);
928+
929+
final BuildContext context;
930+
late final ColorScheme _colors = Theme.of(context).colorScheme;
931+
932+
static const double circularProgressIndicatorSize = 48.0;
933+
934+
@override
935+
Color get color => _colors.primary;
936+
}
937+
938+
class _LinearProgressIndicatorDefaultsM3 extends ProgressIndicatorThemeData {
939+
_LinearProgressIndicatorDefaultsM3(this.context);
940+
941+
final BuildContext context;
942+
late final ColorScheme _colors = Theme.of(context).colorScheme;
943+
944+
@override
945+
Color get color => _colors.primary;
946+
947+
@override
948+
Color get linearTrackColor => _colors.surfaceVariant;
949+
950+
@override
951+
double get linearMinHeight => 4.0;
952+
}
953+
954+
// END GENERATED TOKEN PROPERTIES - ProgressIndicator

packages/flutter/lib/src/material/theme_data.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,7 @@ class ThemeData with Diagnosticable {
12661266
/// * Lists: [ListTile]
12671267
/// * Navigation bar: [NavigationBar] (new, replacing [BottomNavigationBar])
12681268
/// * [Navigation rail](https://m3.material.io/components/navigation-rail): [NavigationRail]
1269+
/// * Progress indicators: [CircularProgressIndicator], [LinearProgressIndicator]
12691270
/// * Radio button: [Radio]
12701271
/// * Switch: [Switch]
12711272
/// * Top app bar: [AppBar]

0 commit comments

Comments
 (0)