Unverified Commit b56a8c56 authored by Hans Muller's avatar Hans Muller Committed by GitHub

Replaced reference to obsolete FlatButton button class in SnackBar (#65988)

parent fb08acf1
...@@ -7,12 +7,14 @@ ...@@ -7,12 +7,14 @@
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'button_theme.dart'; import 'button_style.dart';
import 'color_scheme.dart'; import 'color_scheme.dart';
import 'flat_button.dart';
import 'material.dart'; import 'material.dart';
import 'material_state.dart';
import 'scaffold.dart'; import 'scaffold.dart';
import 'snack_bar_theme.dart'; import 'snack_bar_theme.dart';
import 'text_button.dart';
import 'text_button_theme.dart';
import 'theme.dart'; import 'theme.dart';
import 'theme_data.dart'; import 'theme_data.dart';
...@@ -128,15 +130,19 @@ class _SnackBarActionState extends State<SnackBarAction> { ...@@ -128,15 +130,19 @@ class _SnackBarActionState extends State<SnackBarAction> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final SnackBarThemeData snackBarTheme = Theme.of(context).snackBarTheme; Color resolveForegroundColor(Set<MaterialState> states) {
final Color textColor = widget.textColor ?? snackBarTheme.actionTextColor; final SnackBarThemeData snackBarTheme = Theme.of(context).snackBarTheme;
final Color disabledTextColor = widget.disabledTextColor ?? snackBarTheme.disabledActionTextColor; if (states.contains(MaterialState.disabled))
return widget.disabledTextColor ?? snackBarTheme.disabledActionTextColor;
return widget.textColor ?? snackBarTheme.actionTextColor;
}
return FlatButton( return TextButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.resolveWith<Color>(resolveForegroundColor),
),
onPressed: _haveTriggeredAction ? null : _handlePressed, onPressed: _haveTriggeredAction ? null : _handlePressed,
child: Text(widget.label), child: Text(widget.label),
textColor: textColor,
disabledTextColor: disabledTextColor,
); );
} }
} }
...@@ -385,6 +391,7 @@ class _SnackBarState extends State<SnackBar> { ...@@ -385,6 +391,7 @@ class _SnackBarState extends State<SnackBar> {
final ColorScheme colorScheme = theme.colorScheme; final ColorScheme colorScheme = theme.colorScheme;
final SnackBarThemeData snackBarTheme = theme.snackBarTheme; final SnackBarThemeData snackBarTheme = theme.snackBarTheme;
final bool isThemeDark = theme.brightness == Brightness.dark; final bool isThemeDark = theme.brightness == Brightness.dark;
final Color buttonColor = isThemeDark ? colorScheme.primaryVariant : colorScheme.secondary;
// SnackBar uses a theme that is the opposite brightness from // SnackBar uses a theme that is the opposite brightness from
// the surrounding theme. // the surrounding theme.
...@@ -400,7 +407,7 @@ class _SnackBarState extends State<SnackBar> { ...@@ -400,7 +407,7 @@ class _SnackBarState extends State<SnackBar> {
primaryVariant: colorScheme.onPrimary, primaryVariant: colorScheme.onPrimary,
// For the button color, the spec says it should be primaryVariant, but for // For the button color, the spec says it should be primaryVariant, but for
// backward compatibility on light themes we are leaving it as secondary. // backward compatibility on light themes we are leaving it as secondary.
secondary: isThemeDark ? colorScheme.primaryVariant : colorScheme.secondary, secondary: buttonColor,
secondaryVariant: colorScheme.onSecondary, secondaryVariant: colorScheme.onSecondary,
surface: colorScheme.onSurface, surface: colorScheme.onSurface,
background: themeBackgroundColor, background: themeBackgroundColor,
...@@ -445,10 +452,13 @@ class _SnackBarState extends State<SnackBar> { ...@@ -445,10 +452,13 @@ class _SnackBarState extends State<SnackBar> {
), ),
), ),
if (widget.action != null) if (widget.action != null)
ButtonTheme( TextButtonTheme(
textTheme: ButtonTextTheme.accent, data: TextButtonThemeData(
minWidth: 64.0, style: TextButton.styleFrom(
padding: EdgeInsets.symmetric(horizontal: horizontalPadding), primary: buttonColor,
padding: EdgeInsets.symmetric(horizontal: horizontalPadding),
),
),
child: widget.action, child: widget.action,
), ),
], ],
......
...@@ -101,6 +101,7 @@ void main() { ...@@ -101,6 +101,7 @@ void main() {
testWidgets('SnackBar uses values from SnackBarThemeData', (WidgetTester tester) async { testWidgets('SnackBar uses values from SnackBarThemeData', (WidgetTester tester) async {
const String text = 'I am a snack bar.'; const String text = 'I am a snack bar.';
const String action = 'ACTION';
final SnackBarThemeData snackBarTheme = _snackBarTheme(); final SnackBarThemeData snackBarTheme = _snackBarTheme();
await tester.pumpWidget(MaterialApp( await tester.pumpWidget(MaterialApp(
...@@ -113,7 +114,7 @@ void main() { ...@@ -113,7 +114,7 @@ void main() {
Scaffold.of(context).showSnackBar(SnackBar( Scaffold.of(context).showSnackBar(SnackBar(
content: const Text(text), content: const Text(text),
duration: const Duration(seconds: 2), duration: const Duration(seconds: 2),
action: SnackBarAction(label: 'ACTION', onPressed: () {}), action: SnackBarAction(label: action, onPressed: () {}),
)); ));
}, },
child: const Text('X'), child: const Text('X'),
...@@ -128,20 +129,21 @@ void main() { ...@@ -128,20 +129,21 @@ void main() {
await tester.pump(const Duration(milliseconds: 750)); await tester.pump(const Duration(milliseconds: 750));
final Material material = _getSnackBarMaterial(tester); final Material material = _getSnackBarMaterial(tester);
final RawMaterialButton button = _getSnackBarButton(tester); final RenderParagraph button = _getSnackBarActionTextRenderObject(tester, action);
final RenderParagraph content = _getSnackBarTextRenderObject(tester, text); final RenderParagraph content = _getSnackBarTextRenderObject(tester, text);
expect(content.text.style, snackBarTheme.contentTextStyle); expect(content.text.style, snackBarTheme.contentTextStyle);
expect(material.color, snackBarTheme.backgroundColor); expect(material.color, snackBarTheme.backgroundColor);
expect(material.elevation, snackBarTheme.elevation); expect(material.elevation, snackBarTheme.elevation);
expect(material.shape, snackBarTheme.shape); expect(material.shape, snackBarTheme.shape);
expect(button.textStyle.color, snackBarTheme.actionTextColor); expect(button.text.style.color, snackBarTheme.actionTextColor);
}); });
testWidgets('SnackBar widget properties take priority over theme', (WidgetTester tester) async { testWidgets('SnackBar widget properties take priority over theme', (WidgetTester tester) async {
const Color backgroundColor = Colors.purple; const Color backgroundColor = Colors.purple;
const Color textColor = Colors.pink; const Color textColor = Colors.pink;
const double elevation = 7.0; const double elevation = 7.0;
const String action = 'ACTION';
const ShapeBorder shape = RoundedRectangleBorder( const ShapeBorder shape = RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(9.0)), borderRadius: BorderRadius.all(Radius.circular(9.0)),
); );
...@@ -161,7 +163,7 @@ void main() { ...@@ -161,7 +163,7 @@ void main() {
duration: const Duration(seconds: 2), duration: const Duration(seconds: 2),
action: SnackBarAction( action: SnackBarAction(
textColor: textColor, textColor: textColor,
label: 'ACTION', label: action,
onPressed: () {}, onPressed: () {},
), ),
)); ));
...@@ -178,12 +180,12 @@ void main() { ...@@ -178,12 +180,12 @@ void main() {
await tester.pump(const Duration(milliseconds: 750)); await tester.pump(const Duration(milliseconds: 750));
final Material material = _getSnackBarMaterial(tester); final Material material = _getSnackBarMaterial(tester);
final RawMaterialButton button = _getSnackBarButton(tester); final RenderParagraph button = _getSnackBarActionTextRenderObject(tester, action);
expect(material.color, backgroundColor); expect(material.color, backgroundColor);
expect(material.elevation, elevation); expect(material.elevation, elevation);
expect(material.shape, shape); expect(material.shape, shape);
expect(button.textStyle.color, textColor); expect(button.text.style.color, textColor);
}); });
testWidgets('SnackBar theme behavior is correct for floating', (WidgetTester tester) async { testWidgets('SnackBar theme behavior is correct for floating', (WidgetTester tester) async {
...@@ -292,13 +294,11 @@ Material _getSnackBarMaterial(WidgetTester tester) { ...@@ -292,13 +294,11 @@ Material _getSnackBarMaterial(WidgetTester tester) {
); );
} }
RawMaterialButton _getSnackBarButton(WidgetTester tester) { RenderParagraph _getSnackBarActionTextRenderObject(WidgetTester tester, String text) {
return tester.widget<RawMaterialButton>( return tester.renderObject(find.descendant(
find.descendant( of: find.byType(TextButton),
of: find.byType(SnackBar), matching: find.text(text),
matching: find.byType(RawMaterialButton), ));
).first,
);
} }
RenderParagraph _getSnackBarTextRenderObject(WidgetTester tester, String text) { RenderParagraph _getSnackBarTextRenderObject(WidgetTester tester, String text) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment