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:
Qun Cheng
2023-11-01 16:29:49 -07:00
committed by GitHub
parent b47e4c4acb
commit 4a0f261b4e
11 changed files with 364 additions and 112 deletions

View File

@@ -16,97 +16,33 @@ class CardExamplesApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(colorSchemeSeed: const Color(0xff6750a4), useMaterial3: true),
home: Scaffold(
appBar: AppBar(title: const Text('Card Examples')),
body: const Column(
children: <Widget>[
Spacer(),
ElevatedCardExample(),
FilledCardExample(),
OutlinedCardExample(),
Spacer(),
],
),
),
);
}
}
/// An example of the elevated card type.
///
/// The default settings for [Card] will provide an elevated
/// card matching the spec:
///
/// https://m3.material.io/components/cards/specs#a012d40d-7a5c-4b07-8740-491dec79d58b
class ElevatedCardExample extends StatelessWidget {
const ElevatedCardExample({super.key});
@override
Widget build(BuildContext context) {
return const Center(
child: Card(
child: SizedBox(
width: 300,
height: 100,
child: Center(child: Text('Elevated Card')),
),
),
);
}
}
/// An example of the filled card type.
///
/// To make a [Card] match the filled type, the default elevation and color
/// need to be changed to the values from the spec:
///
/// https://m3.material.io/components/cards/specs#0f55bf62-edf2-4619-b00d-b9ed462f2c5a
class FilledCardExample extends StatelessWidget {
const FilledCardExample({super.key});
@override
Widget build(BuildContext context) {
return Center(
child: Card(
elevation: 0,
color: Theme.of(context).colorScheme.surfaceVariant,
child: const SizedBox(
width: 300,
height: 100,
child: Center(child: Text('Filled Card')),
),
),
);
}
}
/// An example of the outlined card type.
///
/// To make a [Card] match the outlined type, the default elevation and shape
/// need to be changed to the values from the spec:
///
/// https://m3.material.io/components/cards/specs#0f55bf62-edf2-4619-b00d-b9ed462f2c5a
class OutlinedCardExample extends StatelessWidget {
const OutlinedCardExample({super.key});
@override
Widget build(BuildContext context) {
return Center(
child: Card(
elevation: 0,
shape: RoundedRectangleBorder(
side: BorderSide(
color: Theme.of(context).colorScheme.outline,
body: const Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Card(child: _SampleCard(cardName: 'Elevated Card')),
Card.filled(child: _SampleCard(cardName: 'Filled Card')),
Card.outlined(child: _SampleCard(cardName: 'Outlined Card')),
],
),
borderRadius: const BorderRadius.all(Radius.circular(12)),
),
child: const SizedBox(
width: 300,
height: 100,
child: Center(child: Text('Outlined Card')),
),
),
);
}
}
class _SampleCard extends StatelessWidget {
const _SampleCard({required this.cardName});
final String cardName;
@override
Widget build(BuildContext context) {
return SizedBox(
width: 300,
height: 100,
child: Center(child: Text(cardName)),
);
}
}

View File

@@ -0,0 +1,59 @@
// 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 'package:flutter/material.dart';
import 'package:flutter_api_samples/material/card/card.2.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Card variants', (WidgetTester tester) async {
await tester.pumpWidget(const example.CardExamplesApp());
expect(find.byType(Card), findsNWidgets(3));
expect(find.widgetWithText(Card, 'Elevated Card'), findsOneWidget);
expect(find.widgetWithText(Card, 'Filled Card'), findsOneWidget);
expect(find.widgetWithText(Card, 'Outlined Card'), findsOneWidget);
Material getCardMaterial(WidgetTester tester, int cardIndex) {
return tester.widget<Material>(
find.descendant(
of: find.byType(Card).at(cardIndex),
matching: find.byType(Material),
),
);
}
final Material defaultCard = getCardMaterial(tester, 0);
expect(defaultCard.clipBehavior, Clip.none);
expect(defaultCard.elevation, 1.0);
expect(defaultCard.shape, const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(12.0)),
));
expect(defaultCard.color, const Color(0xfffffbfe));
expect(defaultCard.shadowColor, const Color(0xff000000));
expect(defaultCard.surfaceTintColor, const Color(0xff6750a4));
final Material filledCard = getCardMaterial(tester, 1);
expect(filledCard.clipBehavior, Clip.none);
expect(filledCard.elevation, 0.0);
expect(filledCard.shape, const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(12.0)),
));
expect(filledCard.color, const Color(0xffe7e0ec));
expect(filledCard.shadowColor, const Color(0xff000000));
expect(filledCard.surfaceTintColor, const Color(0x00000000));
final Material outlinedCard = getCardMaterial(tester, 2);
expect(outlinedCard.clipBehavior, Clip.none);
expect(outlinedCard.elevation, 0.0);
expect(outlinedCard.shape, const RoundedRectangleBorder(
side: BorderSide(color: Color(0xffcac4d0)),
borderRadius: BorderRadius.all(Radius.circular(12.0)),
));
expect(outlinedCard.color, const Color(0xfffffbfe));
expect(outlinedCard.shadowColor, const Color(0xff000000));
expect(outlinedCard.surfaceTintColor, const Color(0xff6750a4));
});
}