Add Card.filled and Card.outlined factory methods (#136229)
Fixes #119401 This PR is to: * add `Card.filled` and `Card.outlined` factory methods so that we can use tokens for these two types of cards to generate default theme instead of providing hard-corded values in example. * update card.2.dart example. * add test file for card.2.dart example. * fix some mismatch caused by editing the auto-generated defaults by hand in navigation_bar.dart and navigation_drawer.dart.
This commit is contained in:
@@ -6,9 +6,12 @@ import 'package:flutter/widgets.dart';
|
||||
|
||||
import 'card_theme.dart';
|
||||
import 'color_scheme.dart';
|
||||
import 'colors.dart';
|
||||
import 'material.dart';
|
||||
import 'theme.dart';
|
||||
|
||||
enum _CardVariant { elevated, filled, outlined }
|
||||
|
||||
/// A Material Design card: a panel with slightly rounded corners and an
|
||||
/// elevation shadow.
|
||||
///
|
||||
@@ -39,9 +42,9 @@ import 'theme.dart';
|
||||
/// ** See code in examples/api/lib/material/card/card.1.dart **
|
||||
/// {@end-tool}
|
||||
///
|
||||
/// Material Design 3 introduced new types of cards. These can
|
||||
/// be produced by configuring the [Card] widget's properties.
|
||||
/// [Card] widget.
|
||||
/// Material Design 3 introduced new types of cards. The default [Card] is the
|
||||
/// elevated card. To create a filled card, use [Card.filled]; to create a outlined
|
||||
/// card, use [Card.outlined].
|
||||
/// {@tool dartpad}
|
||||
/// This sample shows creation of [Card] widgets for elevated, filled and
|
||||
/// outlined types, as described in: https://m3.material.io/components/cards/overview
|
||||
@@ -71,7 +74,46 @@ class Card extends StatelessWidget {
|
||||
this.clipBehavior,
|
||||
this.child,
|
||||
this.semanticContainer = true,
|
||||
}) : assert(elevation == null || elevation >= 0.0);
|
||||
}) : assert(elevation == null || elevation >= 0.0),
|
||||
_variant = _CardVariant.elevated;
|
||||
|
||||
/// Create a filled variant of Card.
|
||||
///
|
||||
/// Filled cards provide subtle separation from the background. This has less
|
||||
/// emphasis than elevated(default) or outlined cards.
|
||||
const Card.filled({
|
||||
super.key,
|
||||
this.color,
|
||||
this.shadowColor,
|
||||
this.surfaceTintColor,
|
||||
this.elevation,
|
||||
this.shape,
|
||||
this.borderOnForeground = true,
|
||||
this.margin,
|
||||
this.clipBehavior,
|
||||
this.child,
|
||||
this.semanticContainer = true,
|
||||
}) : assert(elevation == null || elevation >= 0.0),
|
||||
_variant = _CardVariant.filled;
|
||||
|
||||
/// Create an outlined variant of Card.
|
||||
///
|
||||
/// Outlined cards have a visual boundary around the container. This can
|
||||
/// provide greater emphasis than the other types.
|
||||
const Card.outlined({
|
||||
super.key,
|
||||
this.color,
|
||||
this.shadowColor,
|
||||
this.surfaceTintColor,
|
||||
this.elevation,
|
||||
this.shape,
|
||||
this.borderOnForeground = true,
|
||||
this.margin,
|
||||
this.clipBehavior,
|
||||
this.child,
|
||||
this.semanticContainer = true,
|
||||
}) : assert(elevation == null || elevation >= 0.0),
|
||||
_variant = _CardVariant.outlined;
|
||||
|
||||
/// The card's background color.
|
||||
///
|
||||
@@ -164,10 +206,24 @@ class Card extends StatelessWidget {
|
||||
/// {@macro flutter.widgets.ProxyWidget.child}
|
||||
final Widget? child;
|
||||
|
||||
final _CardVariant _variant;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final CardTheme cardTheme = CardTheme.of(context);
|
||||
final CardTheme defaults = Theme.of(context).useMaterial3 ? _CardDefaultsM3(context) : _CardDefaultsM2(context);
|
||||
final CardTheme defaults;
|
||||
if (Theme.of(context).useMaterial3) {
|
||||
switch (_variant) {
|
||||
case _CardVariant.elevated:
|
||||
defaults = _CardDefaultsM3(context);
|
||||
case _CardVariant.filled:
|
||||
defaults = _FilledCardDefaultsM3(context);
|
||||
case _CardVariant.outlined:
|
||||
defaults = _OutlinedCardDefaultsM3(context);
|
||||
}
|
||||
} else {
|
||||
defaults = _CardDefaultsM2(context);
|
||||
}
|
||||
|
||||
return Semantics(
|
||||
container: semanticContainer,
|
||||
@@ -226,7 +282,6 @@ class _CardDefaultsM3 extends CardTheme {
|
||||
clipBehavior: Clip.none,
|
||||
elevation: 1.0,
|
||||
margin: const EdgeInsets.all(4.0),
|
||||
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(12.0))),
|
||||
);
|
||||
|
||||
final BuildContext context;
|
||||
@@ -240,6 +295,78 @@ class _CardDefaultsM3 extends CardTheme {
|
||||
|
||||
@override
|
||||
Color? get surfaceTintColor => _colors.surfaceTint;
|
||||
|
||||
@override
|
||||
ShapeBorder? get shape =>const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(12.0)));
|
||||
}
|
||||
|
||||
// END GENERATED TOKEN PROPERTIES - Card
|
||||
|
||||
// BEGIN GENERATED TOKEN PROPERTIES - FilledCard
|
||||
|
||||
// Do not edit by hand. The code between the "BEGIN GENERATED" and
|
||||
// "END GENERATED" comments are generated from data in the Material
|
||||
// Design token database by the script:
|
||||
// dev/tools/gen_defaults/bin/gen_defaults.dart.
|
||||
|
||||
class _FilledCardDefaultsM3 extends CardTheme {
|
||||
_FilledCardDefaultsM3(this.context)
|
||||
: super(
|
||||
clipBehavior: Clip.none,
|
||||
elevation: 0.0,
|
||||
margin: const EdgeInsets.all(4.0),
|
||||
);
|
||||
|
||||
final BuildContext context;
|
||||
late final ColorScheme _colors = Theme.of(context).colorScheme;
|
||||
|
||||
@override
|
||||
Color? get color => _colors.surfaceVariant;
|
||||
|
||||
@override
|
||||
Color? get shadowColor => _colors.shadow;
|
||||
|
||||
@override
|
||||
Color? get surfaceTintColor => Colors.transparent;
|
||||
|
||||
@override
|
||||
ShapeBorder? get shape =>const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(12.0)));
|
||||
}
|
||||
|
||||
// END GENERATED TOKEN PROPERTIES - FilledCard
|
||||
|
||||
// BEGIN GENERATED TOKEN PROPERTIES - OutlinedCard
|
||||
|
||||
// Do not edit by hand. The code between the "BEGIN GENERATED" and
|
||||
// "END GENERATED" comments are generated from data in the Material
|
||||
// Design token database by the script:
|
||||
// dev/tools/gen_defaults/bin/gen_defaults.dart.
|
||||
|
||||
class _OutlinedCardDefaultsM3 extends CardTheme {
|
||||
_OutlinedCardDefaultsM3(this.context)
|
||||
: super(
|
||||
clipBehavior: Clip.none,
|
||||
elevation: 0.0,
|
||||
margin: const EdgeInsets.all(4.0),
|
||||
);
|
||||
|
||||
final BuildContext context;
|
||||
late final ColorScheme _colors = Theme.of(context).colorScheme;
|
||||
|
||||
@override
|
||||
Color? get color => _colors.surface;
|
||||
|
||||
@override
|
||||
Color? get shadowColor => _colors.shadow;
|
||||
|
||||
@override
|
||||
Color? get surfaceTintColor => _colors.surfaceTint;
|
||||
|
||||
@override
|
||||
ShapeBorder? get shape =>
|
||||
const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(12.0))).copyWith(
|
||||
side: BorderSide(color: _colors.outlineVariant)
|
||||
);
|
||||
}
|
||||
|
||||
// END GENERATED TOKEN PROPERTIES - OutlinedCard
|
||||
|
||||
@@ -1364,9 +1364,10 @@ class _NavigationBarDefaultsM3 extends NavigationBarThemeData {
|
||||
@override MaterialStateProperty<TextStyle?>? get labelTextStyle {
|
||||
return MaterialStateProperty.resolveWith((Set<MaterialState> states) {
|
||||
final TextStyle style = _textTheme.labelMedium!;
|
||||
return style.apply(color: states.contains(MaterialState.disabled)
|
||||
? _colors.onSurfaceVariant.withOpacity(0.38)
|
||||
: states.contains(MaterialState.selected)
|
||||
return style.apply(
|
||||
color: states.contains(MaterialState.disabled)
|
||||
? _colors.onSurfaceVariant.withOpacity(0.38)
|
||||
: states.contains(MaterialState.selected)
|
||||
? _colors.onSurface
|
||||
: _colors.onSurfaceVariant
|
||||
);
|
||||
|
||||
@@ -9,6 +9,79 @@ import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
|
||||
import '../widgets/semantics_tester.dart';
|
||||
|
||||
void main() {
|
||||
testWidgetsWithLeakTracking('Material3 - Card defaults (Elevated card)', (WidgetTester tester) async {
|
||||
final ThemeData theme = ThemeData();
|
||||
final ColorScheme colors = theme.colorScheme;
|
||||
await tester.pumpWidget(MaterialApp(
|
||||
theme: theme,
|
||||
home: const Scaffold(
|
||||
body: Card(),
|
||||
),
|
||||
));
|
||||
|
||||
final Container container = _getCardContainer(tester);
|
||||
final Material material = _getCardMaterial(tester);
|
||||
|
||||
expect(material.clipBehavior, Clip.none);
|
||||
expect(material.elevation, 1.0);
|
||||
expect(container.margin, const EdgeInsets.all(4.0));
|
||||
expect(material.color, colors.surface);
|
||||
expect(material.shadowColor, colors.shadow);
|
||||
expect(material.surfaceTintColor, colors.surfaceTint); // Default primary color
|
||||
expect(material.shape, const RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.all(Radius.circular(12.0)),
|
||||
));
|
||||
});
|
||||
|
||||
testWidgetsWithLeakTracking('Material3 - Card.filled defaults', (WidgetTester tester) async {
|
||||
final ThemeData theme = ThemeData();
|
||||
final ColorScheme colors = theme.colorScheme;
|
||||
await tester.pumpWidget(MaterialApp(
|
||||
theme: theme,
|
||||
home: const Scaffold(
|
||||
body: Card.filled(),
|
||||
),
|
||||
));
|
||||
|
||||
final Container container = _getCardContainer(tester);
|
||||
final Material material = _getCardMaterial(tester);
|
||||
|
||||
expect(material.clipBehavior, Clip.none);
|
||||
expect(material.elevation, 0.0);
|
||||
expect(container.margin, const EdgeInsets.all(4.0));
|
||||
expect(material.color, colors.surfaceVariant);
|
||||
expect(material.shadowColor, colors.shadow);
|
||||
expect(material.surfaceTintColor, Colors.transparent);
|
||||
expect(material.shape, const RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.all(Radius.circular(12.0)),
|
||||
));
|
||||
});
|
||||
|
||||
testWidgetsWithLeakTracking('Material3 - Card.outlined defaults', (WidgetTester tester) async {
|
||||
final ThemeData theme = ThemeData();
|
||||
final ColorScheme colors = theme.colorScheme;
|
||||
await tester.pumpWidget(MaterialApp(
|
||||
theme: theme,
|
||||
home: const Scaffold(
|
||||
body: Card.outlined(),
|
||||
),
|
||||
));
|
||||
|
||||
final Container container = _getCardContainer(tester);
|
||||
final Material material = _getCardMaterial(tester);
|
||||
|
||||
expect(material.clipBehavior, Clip.none);
|
||||
expect(material.elevation, 0.0);
|
||||
expect(container.margin, const EdgeInsets.all(4.0));
|
||||
expect(material.color, colors.surface);
|
||||
expect(material.shadowColor, colors.shadow);
|
||||
expect(material.surfaceTintColor, colors.surfaceTint);
|
||||
expect(material.shape, RoundedRectangleBorder(
|
||||
side: BorderSide(color: colors.outlineVariant),
|
||||
borderRadius: const BorderRadius.all(Radius.circular(12.0)),
|
||||
));
|
||||
});
|
||||
|
||||
testWidgetsWithLeakTracking('Card can take semantic text from multiple children', (WidgetTester tester) async {
|
||||
final SemanticsTester semantics = SemanticsTester(tester);
|
||||
await tester.pumpWidget(
|
||||
@@ -219,3 +292,21 @@ void main() {
|
||||
expect(getCardMaterial(tester).shadowColor, Colors.red);
|
||||
});
|
||||
}
|
||||
|
||||
Material _getCardMaterial(WidgetTester tester) {
|
||||
return tester.widget<Material>(
|
||||
find.descendant(
|
||||
of: find.byType(Card),
|
||||
matching: find.byType(Material),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Container _getCardContainer(WidgetTester tester) {
|
||||
return tester.widget<Container>(
|
||||
find.descendant(
|
||||
of: find.byType(Card),
|
||||
matching: find.byType(Container),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user