-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
QuncCccccc
merged 25 commits into
flutter:master
from
QuncCccccc:update_switch_to_material3
Sep 9, 2022
Merged
Migrated Switch
to Material 3
#110095
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 4bd1828
Removed indent
QuncCccccc b616e63
Added test
QuncCccccc c675e2a
Added tests for thumbImage
QuncCccccc 6967db6
Fixed failing test
QuncCccccc 9e080c5
Removed trailing space
QuncCccccc 38beefb
Changed the test number 1.0 to 1
QuncCccccc f08cb78
Changed the tested double number
QuncCccccc 5ab248c
Removed extra comments
QuncCccccc 2effbd8
Added switch examples for the new properties and fixed partial comments
QuncCccccc f720896
Reorganized the config data for M2 and M3
QuncCccccc f96f37e
Corrected the order of pressed, hovered, and focused in the resolve m…
QuncCccccc 484516c
Changed doc to fix the analyzer test
QuncCccccc 90f274b
Modified example in doc
QuncCccccc 19432d6
Changed Switch thumbRadius so the inactive switch with image has same…
QuncCccccc f0a34c1
Changed thumbImage type so that it can be used for both Image and Icon
QuncCccccc 82ec2d6
Modified comments
QuncCccccc 4d398ef
Modified doc
QuncCccccc 7b42c6f
Removed unused import
QuncCccccc f978ba5
Fixed failing test
QuncCccccc ca076c4
Changed doc
QuncCccccc 0236248
Merge remote-tracking branch 'upstream/master' into update_switch_to_…
QuncCccccc 48feade
Changed thumbImage to thumbIcon which only allows Icon type
QuncCccccc 6932d03
Fixed failing test
QuncCccccc 3398c23
Removed all the extra indentation
QuncCccccc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
|
||
@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']}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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']}; | ||
} | ||
'''; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}, | ||
), | ||
], | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
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?