Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrated Switch to Material 3 #110095

Merged
merged 25 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d32a161
Migrated the Switch to Material3
QuncCccccc Aug 22, 2022
4bd1828
Removed indent
QuncCccccc Aug 22, 2022
b616e63
Added test
QuncCccccc Aug 23, 2022
c675e2a
Added tests for thumbImage
QuncCccccc Aug 24, 2022
6967db6
Fixed failing test
QuncCccccc Aug 24, 2022
9e080c5
Removed trailing space
QuncCccccc Aug 24, 2022
38beefb
Changed the test number 1.0 to 1
QuncCccccc Aug 24, 2022
f08cb78
Changed the tested double number
QuncCccccc Aug 24, 2022
5ab248c
Removed extra comments
QuncCccccc Aug 26, 2022
2effbd8
Added switch examples for the new properties and fixed partial comments
QuncCccccc Aug 26, 2022
f720896
Reorganized the config data for M2 and M3
QuncCccccc Aug 30, 2022
f96f37e
Corrected the order of pressed, hovered, and focused in the resolve m…
QuncCccccc Aug 30, 2022
484516c
Changed doc to fix the analyzer test
QuncCccccc Aug 30, 2022
90f274b
Modified example in doc
QuncCccccc Aug 30, 2022
19432d6
Changed Switch thumbRadius so the inactive switch with image has same…
QuncCccccc Aug 30, 2022
f0a34c1
Changed thumbImage type so that it can be used for both Image and Icon
QuncCccccc Sep 6, 2022
82ec2d6
Modified comments
QuncCccccc Sep 6, 2022
4d398ef
Modified doc
QuncCccccc Sep 6, 2022
7b42c6f
Removed unused import
QuncCccccc Sep 6, 2022
f978ba5
Fixed failing test
QuncCccccc Sep 7, 2022
ca076c4
Changed doc
QuncCccccc Sep 7, 2022
0236248
Merge remote-tracking branch 'upstream/master' into update_switch_to_…
QuncCccccc Sep 8, 2022
48feade
Changed thumbImage to thumbIcon which only allows Icon type
QuncCccccc Sep 8, 2022
6932d03
Fixed failing test
QuncCccccc Sep 8, 2022
3398c23
Removed all the extra indentation
QuncCccccc Sep 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dev/tools/gen_defaults/bin/gen_defaults.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import 'package:gen_defaults/input_decorator_template.dart';
import 'package:gen_defaults/navigation_bar_template.dart';
import 'package:gen_defaults/navigation_rail_template.dart';
import 'package:gen_defaults/surface_tint.dart';
import 'package:gen_defaults/switch_template.dart';
import 'package:gen_defaults/text_field_template.dart';
import 'package:gen_defaults/typography_template.dart';

Expand Down Expand Up @@ -123,6 +124,7 @@ Future<void> main(List<String> args) async {
NavigationBarTemplate('NavigationBar', '$materialLib/navigation_bar.dart', tokens).updateFile();
NavigationRailTemplate('NavigationRail', '$materialLib/navigation_rail.dart', tokens).updateFile();
SurfaceTintTemplate('SurfaceTint', '$materialLib/elevation_overlay.dart', tokens).updateFile();
SwitchTemplate('Switch', '$materialLib/switch.dart', tokens).updateFile();
TextFieldTemplate('TextField', '$materialLib/text_field.dart', tokens).updateFile();
TypographyTemplate('Typography', '$materialLib/typography.dart', tokens).updateFile();
}
208 changes: 208 additions & 0 deletions dev/tools/gen_defaults/lib/switch_template.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
// 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.

import 'template.dart';

class SwitchTemplate extends TokenTemplate {
const SwitchTemplate(super.blockName, super.fileName, super.tokens, {
super.colorSchemePrefix = '_colors.',
});

@override
String generate() => '''
class _${blockName}DefaultsM3 extends SwitchThemeData {
_${blockName}DefaultsM3(BuildContext context)
: _colors = Theme.of(context).colorScheme;

final ColorScheme _colors;

@override
MaterialStateProperty<Color> get thumbColor {
return MaterialStateProperty.resolveWith((Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
if (states.contains(MaterialState.selected)) {
return ${componentColor('md.comp.switch.disabled.selected.handle')};
}
return ${componentColor('md.comp.switch.disabled.unselected.handle')};
}
if (states.contains(MaterialState.selected)) {
if (states.contains(MaterialState.pressed)) {
return ${componentColor('md.comp.switch.selected.pressed.handle')};
}
if (states.contains(MaterialState.hovered)) {
return ${componentColor('md.comp.switch.selected.hover.handle')};
}
if (states.contains(MaterialState.focused)) {
return ${componentColor('md.comp.switch.selected.focus.handle')};
}
return ${componentColor('md.comp.switch.selected.handle')};
}
if (states.contains(MaterialState.pressed)) {
return ${componentColor('md.comp.switch.unselected.pressed.handle')};
}
if (states.contains(MaterialState.hovered)) {
return ${componentColor('md.comp.switch.unselected.hover.handle')};
}
if (states.contains(MaterialState.focused)) {
return ${componentColor('md.comp.switch.unselected.focus.handle')};
}
return ${componentColor('md.comp.switch.unselected.handle')};
});
}

@override
MaterialStateProperty<Color> get trackColor {
return MaterialStateProperty.resolveWith((Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
if (states.contains(MaterialState.selected)) {
return ${componentColor('md.comp.switch.disabled.selected.track')}.withOpacity(${opacity('md.comp.switch.disabled.track.opacity')});
}
return ${componentColor('md.comp.switch.disabled.unselected.track')}.withOpacity(${opacity('md.comp.switch.disabled.track.opacity')});
}
if (states.contains(MaterialState.selected)) {
if (states.contains(MaterialState.pressed)) {
return ${componentColor('md.comp.switch.selected.pressed.track')};
}
if (states.contains(MaterialState.hovered)) {
return ${componentColor('md.comp.switch.selected.hover.track')};
}
if (states.contains(MaterialState.focused)) {
return ${componentColor('md.comp.switch.selected.focus.track')};
}
return ${componentColor('md.comp.switch.selected.track')};
}
if (states.contains(MaterialState.pressed)) {
return ${componentColor('md.comp.switch.unselected.pressed.track')};
}
if (states.contains(MaterialState.hovered)) {
return ${componentColor('md.comp.switch.unselected.hover.track')};
}
if (states.contains(MaterialState.focused)) {
return ${componentColor('md.comp.switch.unselected.focus.track')};
}
return ${componentColor('md.comp.switch.unselected.track')};
});
}

@override
MaterialStateProperty<Color?> get overlayColor {
return MaterialStateProperty.resolveWith((Set<MaterialState> states) {
if (states.contains(MaterialState.selected)) {
if (states.contains(MaterialState.pressed)) {
return ${componentColor('md.comp.switch.selected.pressed.state-layer')};
}
if (states.contains(MaterialState.hovered)) {
return ${componentColor('md.comp.switch.selected.hover.state-layer')};
}
if (states.contains(MaterialState.focused)) {
return ${componentColor('md.comp.switch.selected.focus.state-layer')};
}
return null;
}
if (states.contains(MaterialState.pressed)) {
return ${componentColor('md.comp.switch.unselected.pressed.state-layer')};
}
if (states.contains(MaterialState.hovered)) {
return ${componentColor('md.comp.switch.unselected.hover.state-layer')};
}
if (states.contains(MaterialState.focused)) {
return ${componentColor('md.comp.switch.unselected.focus.state-layer')};
}
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be Colors.transparent? How do we expect overlayColor=null to be interpreted in this case?

CC @darrenaustin - Maybe this is another case where we want null to mean "no overlay" instead of "no preference".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the switch is not in any of the states(hovered, focused, and pressed), should we just give a null value for the state layer?

});
}

@override
double get splashRadius => ${tokens['md.comp.switch.state-layer.size']} / 2;
}

class _SwitchConfigM3 with _SwitchConfig {
_SwitchConfigM3(this.context)
: _colors = Theme.of(context).colorScheme;

BuildContext context;
final ColorScheme _colors;

static const double iconSize = ${tokens['md.comp.switch.unselected.icon.size']};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to see all of these configuration parameters being derived from the token db.


@override
double get activeThumbRadius => ${tokens['md.comp.switch.selected.handle.width']} / 2;

@override
MaterialStateProperty<Color> get iconColor {
return MaterialStateProperty.resolveWith((Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
if (states.contains(MaterialState.selected)) {
return ${componentColor('md.comp.switch.disabled.selected.icon')};
}
return ${componentColor('md.comp.switch.disabled.unselected.icon')};
}
if (states.contains(MaterialState.selected)) {
if (states.contains(MaterialState.pressed)) {
return ${componentColor('md.comp.switch.selected.pressed.icon')};
}
if (states.contains(MaterialState.hovered)) {
return ${componentColor('md.comp.switch.selected.hover.icon')};
}
if (states.contains(MaterialState.focused)) {
return ${componentColor('md.comp.switch.selected.focus.icon')};
}
return ${componentColor('md.comp.switch.selected.icon')};
}
if (states.contains(MaterialState.pressed)) {
return ${componentColor('md.comp.switch.unselected.pressed.icon')};
}
if (states.contains(MaterialState.hovered)) {
return ${componentColor('md.comp.switch.unselected.hover.icon')};
}
if (states.contains(MaterialState.focused)) {
return ${componentColor('md.comp.switch.unselected.focus.icon')};
}
return ${componentColor('md.comp.switch.unselected.icon')};
});
}

@override
double get inactiveThumbRadius => ${tokens['md.comp.switch.unselected.handle.width']} / 2;

@override
double get pressedThumbRadius => ${tokens['md.comp.switch.pressed.handle.width']} / 2;

@override
double get switchHeight => _kSwitchMinSize + 8.0;

@override
double get switchHeightCollapsed => _kSwitchMinSize;

@override
double get switchWidth => trackWidth - 2 * (trackHeight / 2.0) + _kSwitchMinSize;

@override
double get thumbRadiusWithIcon => ${tokens['md.comp.switch.with-icon.handle.width']} / 2;

@override
List<BoxShadow>? get thumbShadow => kElevationToShadow[0];

@override
double get trackHeight => ${tokens['md.comp.switch.track.height']};

@override
MaterialStateProperty<Color?> get trackOutlineColor {
return MaterialStateProperty.resolveWith((Set<MaterialState> states) {
if (states.contains(MaterialState.selected)) {
return null;
}
if (states.contains(MaterialState.disabled)) {
return ${componentColor('md.comp.switch.disabled.unselected.track.outline')}.withOpacity(${opacity('md.comp.switch.disabled.track.opacity')});
}
return ${componentColor('md.comp.switch.unselected.track.outline')};
});
}

@override
double get trackWidth => ${tokens['md.comp.switch.track.width']};
}
''';

}
74 changes: 74 additions & 0 deletions examples/api/lib/material/switch/switch.2.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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.

// Flutter code sample for Switch

import 'package:flutter/material.dart';

void main() => runApp(const SwitchApp());

class SwitchApp extends StatelessWidget {
const SwitchApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(useMaterial3: true, colorSchemeSeed: const Color(0xff6750a4)),
home: Scaffold(
appBar: AppBar(title: const Text('Switch Sample')),
body: const Center(
child: SwitchExample(),
),
),
);
}
}

class SwitchExample extends StatefulWidget {
const SwitchExample({super.key});

@override
State<SwitchExample> createState() => _SwitchExampleState();
}

class _SwitchExampleState extends State<SwitchExample> {
bool light0 = true;
bool light1 = true;
bool light2 = true;

final MaterialStateProperty<Icon?> thumbIcon = MaterialStateProperty.resolveWith<Icon?>((Set<MaterialState> states) {
// Thumb icon when the switch is selected.
if (states.contains(MaterialState.selected)) {
return const Icon(Icons.check);
}
return const Icon(Icons.close);
},
);

@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Switch(
value: light0,
onChanged: (bool value) {
setState(() {
light0 = value;
});
},
),
Switch(
thumbIcon: thumbIcon,
value: light1,
onChanged: (bool value) {
setState(() {
light1 = value;
});
},
),
],
);
}
}
Loading