Skip to content

Verbose SnackBar inherit theme data test #154920

@TahaTesser

Description

@TahaTesser

Use case

When working on deprecation of ThemeData.dialogBackgroundColor . I ran into an unusually verbose test which also uses dialogBackgroundColor.

testWidgets('SnackBar should inherit theme data from its ancestor.', (WidgetTester tester) async {
final SliderThemeData sliderTheme = SliderThemeData.fromPrimaryColors(
primaryColor: Colors.black,
primaryColorDark: Colors.black,
primaryColorLight: Colors.black,
valueIndicatorTextStyle: const TextStyle(color: Colors.black),
);
final ChipThemeData chipTheme = ChipThemeData.fromDefaults(
primaryColor: Colors.black,
secondaryColor: Colors.white,
labelStyle: const TextStyle(color: Colors.black),
);
const PageTransitionsTheme pageTransitionTheme = PageTransitionsTheme(
builders: <TargetPlatform, PageTransitionsBuilder>{
TargetPlatform.iOS: CupertinoPageTransitionsBuilder(),
TargetPlatform.macOS: CupertinoPageTransitionsBuilder(),
},
);
final ThemeData theme = ThemeData.light().copyWith(
visualDensity: VisualDensity.standard,
primaryColor: Colors.black,
primaryColorLight: Colors.black,
primaryColorDark: Colors.black,
canvasColor: Colors.black,
shadowColor: Colors.black,
scaffoldBackgroundColor: Colors.black,
cardColor: Colors.black,
dividerColor: Colors.black,
focusColor: Colors.black,
hoverColor: Colors.black,
highlightColor: Colors.black,
splashColor: Colors.black,
splashFactory: InkRipple.splashFactory,
unselectedWidgetColor: Colors.black,
disabledColor: Colors.black,
buttonTheme: const ButtonThemeData(colorScheme: ColorScheme.dark()),
toggleButtonsTheme: const ToggleButtonsThemeData(textStyle: TextStyle(color: Colors.black)),
secondaryHeaderColor: Colors.black,
dialogBackgroundColor: Colors.black,
indicatorColor: Colors.black,
hintColor: Colors.black,
textTheme: ThemeData.dark().textTheme,
primaryTextTheme: ThemeData.dark().textTheme,
inputDecorationTheme: ThemeData.dark().inputDecorationTheme.copyWith(border: const OutlineInputBorder()),
iconTheme: ThemeData.dark().iconTheme,
primaryIconTheme: ThemeData.dark().iconTheme,
sliderTheme: sliderTheme,
tabBarTheme: const TabBarTheme(labelColor: Colors.black),
tooltipTheme: const TooltipThemeData(height: 100),
cardTheme: const CardTheme(color: Colors.black),
chipTheme: chipTheme,
platform: TargetPlatform.iOS,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
applyElevationOverlayColor: false,
pageTransitionsTheme: pageTransitionTheme,
appBarTheme: const AppBarTheme(backgroundColor: Colors.black),
scrollbarTheme: const ScrollbarThemeData(radius: Radius.circular(10.0)),
bottomAppBarTheme: const BottomAppBarTheme(color: Colors.black),
colorScheme: const ColorScheme.light(),
dialogTheme: const DialogTheme(backgroundColor: Colors.black),
floatingActionButtonTheme: const FloatingActionButtonThemeData(backgroundColor: Colors.black),
navigationRailTheme: const NavigationRailThemeData(backgroundColor: Colors.black),
typography: Typography.material2018(),
snackBarTheme: const SnackBarThemeData(backgroundColor: Colors.black),
bottomSheetTheme: const BottomSheetThemeData(backgroundColor: Colors.black),
popupMenuTheme: const PopupMenuThemeData(color: Colors.black),
bannerTheme: const MaterialBannerThemeData(backgroundColor: Colors.black),
dividerTheme: const DividerThemeData(color: Colors.black),
bottomNavigationBarTheme: const BottomNavigationBarThemeData(type: BottomNavigationBarType.fixed),
timePickerTheme: const TimePickerThemeData(backgroundColor: Colors.black),
textButtonTheme: TextButtonThemeData(style: TextButton.styleFrom(foregroundColor: Colors.red)),
elevatedButtonTheme: ElevatedButtonThemeData(style: ElevatedButton.styleFrom(backgroundColor: Colors.green)),
outlinedButtonTheme: OutlinedButtonThemeData(style: OutlinedButton.styleFrom(foregroundColor: Colors.blue)),
textSelectionTheme: const TextSelectionThemeData(cursorColor: Colors.black),
dataTableTheme: const DataTableThemeData(),
checkboxTheme: const CheckboxThemeData(),
radioTheme: const RadioThemeData(),
switchTheme: const SwitchThemeData(),
progressIndicatorTheme: const ProgressIndicatorThemeData(),
);
ThemeData? themeBeforeSnackBar;
ThemeData? themeAfterSnackBar;
await tester.pumpWidget(
MaterialApp(
theme: theme,
home: Scaffold(
body: Builder(
builder: (BuildContext context) {
themeBeforeSnackBar = Theme.of(context);
return GestureDetector(
onTap: () {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Builder(
builder: (BuildContext context) {
themeAfterSnackBar = Theme.of(context);
return const Text('I am a snack bar.');
},
),
duration: const Duration(seconds: 2),
action: SnackBarAction(
label: 'ACTION',
onPressed: () { },
),
),
);
},
child: const Text('X'),
);
},
),
),
),
);
await tester.tap(find.text('X'));
await tester.pump(); // start animation
await tester.pump(const Duration(milliseconds: 750));
final ThemeData comparedTheme = themeBeforeSnackBar!.copyWith(
colorScheme: themeAfterSnackBar!.colorScheme,
); // Fields replaced by SnackBar.
expect(comparedTheme, themeAfterSnackBar);
});

Proposal

Clean up the test

Metadata

Metadata

Assignees

Labels

c: proposalA detailed proposal for a change to Flutterc: tech-debtTechnical debt, code quality, testing, etc.f: material designflutter/packages/flutter/material repository.frameworkflutter/packages/flutter repository. See also f: labels.r: fixedIssue is closed as already fixed in a newer versionteam-designOwned by Design Languages team

Type

No type

Projects

Status

Done (PR merged)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions