Factor out deprecated names in example code (#151374)

This PR contains:
- 3 instances of `colorScheme.background` → `colorScheme.surface`
- and a whole bunch of `MaterialState` → `WidgetState`

As of yet, no changes have been made to example test files or the [examples/api/lib/material/material_state/](0b2a8e589e/examples/api/lib/material/material_state) directory.

(related: #151373)
This commit is contained in:
Nate Wilson
2024-07-08 13:06:54 -06:00
committed by GitHub
parent 0a4f1a7e55
commit b713445298
22 changed files with 80 additions and 80 deletions

View File

@@ -104,15 +104,15 @@ class _TextButtonExampleState extends State<TextButtonExample> {
// This gradient's appearance reflects the button's state.
// Always return a gradient decoration so that AnimatedContainer
// can interpolate in between. Used by TextButton #7.
Decoration? statesToDecoration(Set<MaterialState> states) {
if (states.contains(MaterialState.pressed)) {
Decoration? statesToDecoration(Set<WidgetState> states) {
if (states.contains(WidgetState.pressed)) {
return BoxDecoration(
gradient: LinearGradient(colors: <Color>[color2, color2]), // solid fill
);
}
return BoxDecoration(
gradient: LinearGradient(
colors: switch (states.contains(MaterialState.hovered)) {
colors: switch (states.contains(WidgetState.hovered)) {
true => <Color>[color1, color2],
false => <Color>[color2, color1],
},
@@ -149,7 +149,7 @@ class _TextButtonExampleState extends State<TextButtonExample> {
// In this example, and most of the ones that follow, we're using
// the TextButton.styleFrom() convenience method to create a ButtonStyle.
// The styleFrom method is a little easier because it creates
// ButtonStyle MaterialStateProperty parameters for you.
// ButtonStyle WidgetStateProperty parameters for you.
// In this case, Specifying foregroundColor overrides the text,
// icon and overlay (splash and highlight) colors a little differently
// depending on the button's state. BackgroundColor is just the background
@@ -213,7 +213,7 @@ class _TextButtonExampleState extends State<TextButtonExample> {
// theme or the MaterialApp theme's ThemeData.textButtonTheme.
TextButton(
style: TextButton.styleFrom(
foregroundBuilder: (BuildContext context, Set<MaterialState> states, Widget? child) {
foregroundBuilder: (BuildContext context, Set<WidgetState> states, Widget? child) {
return ShaderMask(
shaderCallback: (Rect bounds) {
return LinearGradient(
@@ -248,10 +248,10 @@ class _TextButtonExampleState extends State<TextButtonExample> {
// outlines the button's shape.
TextButton(
style: TextButton.styleFrom(
foregroundBuilder: (BuildContext context, Set<MaterialState> states, Widget? child) {
foregroundBuilder: (BuildContext context, Set<WidgetState> states, Widget? child) {
return DecoratedBox(
decoration: BoxDecoration(
border: states.contains(MaterialState.hovered)
border: states.contains(WidgetState.hovered)
? Border(bottom: BorderSide(color: colorScheme.primary))
: const Border(), // essentially "no border"
),
@@ -272,7 +272,7 @@ class _TextButtonExampleState extends State<TextButtonExample> {
// closely). Because we want the outline to only appear when the button is hovered
// we can't use the styleFrom() side parameter, because that creates the same
// outline for all states. The ButtonStyle.copyWith() method is used to add
// a MaterialState<BorderSide?> property that does the right thing.
// a WidgetState<BorderSide?> property that does the right thing.
//
// The gradient background is translucent - all of the colors have opacity 0.5 -
// so the overlay's splash and highlight colors are visible even though they're
@@ -284,7 +284,7 @@ class _TextButtonExampleState extends State<TextButtonExample> {
onPressed: () {},
style: TextButton.styleFrom(
overlayColor: color2,
backgroundBuilder: (BuildContext context, Set<MaterialState> states, Widget? child) {
backgroundBuilder: (BuildContext context, Set<WidgetState> states, Widget? child) {
return AnimatedContainer(
duration: const Duration(milliseconds: 500),
decoration: statesToDecoration(states),
@@ -292,8 +292,8 @@ class _TextButtonExampleState extends State<TextButtonExample> {
);
},
).copyWith(
side: MaterialStateProperty.resolveWith<BorderSide?>((Set<MaterialState> states) {
if (states.contains(MaterialState.hovered)) {
side: WidgetStateProperty.resolveWith<BorderSide?>((Set<WidgetState> states) {
if (states.contains(WidgetState.hovered)) {
return BorderSide(width: 3, color: color3);
}
return null; // defer to the default
@@ -315,7 +315,7 @@ class _TextButtonExampleState extends State<TextButtonExample> {
onPressed: () {},
style: TextButton.styleFrom(
foregroundColor: Colors.white,
backgroundBuilder: (BuildContext context, Set<MaterialState> states, Widget? child) {
backgroundBuilder: (BuildContext context, Set<WidgetState> states, Widget? child) {
return Ink(
decoration: const BoxDecoration(
image: DecorationImage(

View File

@@ -29,19 +29,19 @@ class SelectableButton extends StatefulWidget {
}
class _SelectableButtonState extends State<SelectableButton> {
late final MaterialStatesController statesController;
late final WidgetStatesController statesController;
@override
void initState() {
super.initState();
statesController = MaterialStatesController(<MaterialState>{if (widget.selected) MaterialState.selected});
statesController = WidgetStatesController(<WidgetState>{if (widget.selected) WidgetState.selected});
}
@override
void didUpdateWidget(SelectableButton oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.selected != oldWidget.selected) {
statesController.update(MaterialState.selected, widget.selected);
statesController.update(WidgetState.selected, widget.selected);
}
}
@@ -73,17 +73,17 @@ class _HomeState extends State<Home> {
child: SelectableButton(
selected: selected,
style: ButtonStyle(
foregroundColor: MaterialStateProperty.resolveWith<Color?>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.selected)) {
foregroundColor: WidgetStateProperty.resolveWith<Color?>(
(Set<WidgetState> states) {
if (states.contains(WidgetState.selected)) {
return Colors.white;
}
return null; // defer to the defaults
},
),
backgroundColor: MaterialStateProperty.resolveWith<Color?>(
(Set<MaterialState> states) {
if (states.contains(MaterialState.selected)) {
backgroundColor: WidgetStateProperty.resolveWith<Color?>(
(Set<WidgetState> states) {
if (states.contains(WidgetState.selected)) {
return Colors.indigo;
}
return null; // defer to the defaults