Skip to content

Commit 1d5f455

Browse files
authored
Add support for Material 3 Divider and VerticalDivider (#112378)
1 parent 9853d88 commit 1d5f455

File tree

12 files changed

+364
-24
lines changed

12 files changed

+364
-24
lines changed

dev/tools/gen_defaults/bin/gen_defaults.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import 'package:gen_defaults/button_template.dart';
2424
import 'package:gen_defaults/card_template.dart';
2525
import 'package:gen_defaults/checkbox_template.dart';
2626
import 'package:gen_defaults/dialog_template.dart';
27+
import 'package:gen_defaults/divider_template.dart';
2728
import 'package:gen_defaults/fab_template.dart';
2829
import 'package:gen_defaults/filter_chip_template.dart';
2930
import 'package:gen_defaults/icon_button_template.dart';
@@ -65,6 +66,7 @@ Future<void> main(List<String> args) async {
6566
'date_picker_docked.json',
6667
'date_picker_modal.json',
6768
'dialog.json',
69+
'divider.json',
6870
'dialog_fullscreen.json',
6971
'elevation.json',
7072
'fab_extended_primary.json',
@@ -121,6 +123,7 @@ Future<void> main(List<String> args) async {
121123
CardTemplate('Card', '$materialLib/card.dart', tokens).updateFile();
122124
CheckboxTemplate('Checkbox', '$materialLib/checkbox.dart', tokens).updateFile();
123125
DialogTemplate('Dialog', '$materialLib/dialog.dart', tokens).updateFile();
126+
DividerTemplate('Divider', '$materialLib/divider.dart', tokens).updateFile();
124127
FABTemplate('FAB', '$materialLib/floating_action_button.dart', tokens).updateFile();
125128
FilterChipTemplate('ChoiceChip', '$materialLib/choice_chip.dart', tokens).updateFile();
126129
FilterChipTemplate('FilterChip', '$materialLib/filter_chip.dart', tokens).updateFile();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'template.dart';
6+
7+
class DividerTemplate extends TokenTemplate {
8+
const DividerTemplate(super.blockName, super.fileName, super.tokens, {
9+
super.colorSchemePrefix = '_colors.',
10+
super.textThemePrefix = '_textTheme.'
11+
});
12+
13+
@override
14+
String generate() => '''
15+
class _${blockName}DefaultsM3 extends DividerThemeData {
16+
_${blockName}DefaultsM3(this.context) : super(thickness: ${tokens["md.comp.divider.thickness"]});
17+
18+
final BuildContext context;
19+
late final ColorScheme _colors = Theme.of(context).colorScheme;
20+
21+
@override Color? get color => ${componentColor("md.comp.divider")};
22+
}
23+
''';
24+
}

examples/api/lib/material/divider/divider.0.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,19 @@ void main() => runApp(const MyApp());
1111
class MyApp extends StatelessWidget {
1212
const MyApp({super.key});
1313

14-
static const String _title = 'Flutter Code Sample';
15-
1614
@override
1715
Widget build(BuildContext context) {
1816
return MaterialApp(
19-
title: _title,
2017
home: Scaffold(
21-
appBar: AppBar(title: const Text(_title)),
22-
body: const MyStatelessWidget(),
18+
appBar: AppBar(title: const Text('Divider Sample')),
19+
body: const DividerExample(),
2320
),
2421
);
2522
}
2623
}
2724

28-
class MyStatelessWidget extends StatelessWidget {
29-
const MyStatelessWidget({super.key});
25+
class DividerExample extends StatelessWidget {
26+
const DividerExample({super.key});
3027

3128
@override
3229
Widget build(BuildContext context) {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
/// Flutter code sample for [Divider].
6+
7+
import 'package:flutter/material.dart';
8+
9+
void main() => runApp(const MyApp());
10+
11+
class MyApp extends StatelessWidget {
12+
const MyApp({super.key});
13+
14+
@override
15+
Widget build(BuildContext context) {
16+
return MaterialApp(
17+
theme: ThemeData(colorSchemeSeed: const Color(0xff6750a4), useMaterial3: true),
18+
home: Scaffold(
19+
appBar: AppBar(title: const Text('Divider Sample')),
20+
body: const DividerExample(),
21+
),
22+
);
23+
}
24+
}
25+
26+
class DividerExample extends StatelessWidget {
27+
const DividerExample({super.key});
28+
29+
@override
30+
Widget build(BuildContext context) {
31+
return Center(
32+
child: Padding(
33+
padding: const EdgeInsets.all(16.0),
34+
child: Column(
35+
children: const <Widget>[
36+
Expanded(
37+
child: Card(
38+
child: SizedBox.expand(),
39+
),
40+
),
41+
Divider(),
42+
Expanded(
43+
child: Card(
44+
child: SizedBox.expand(),
45+
),
46+
),
47+
],
48+
),
49+
),
50+
);
51+
}
52+
}

examples/api/lib/material/divider/vertical_divider.0.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,19 @@ void main() => runApp(const MyApp());
1111
class MyApp extends StatelessWidget {
1212
const MyApp({super.key});
1313

14-
static const String _title = 'Flutter Code Sample';
15-
1614
@override
1715
Widget build(BuildContext context) {
1816
return MaterialApp(
19-
title: _title,
2017
home: Scaffold(
21-
appBar: AppBar(title: const Text(_title)),
22-
body: const MyStatelessWidget(),
18+
appBar: AppBar(title: const Text('VerticalDivider Sample')),
19+
body: const DividerExample(),
2320
),
2421
);
2522
}
2623
}
2724

28-
class MyStatelessWidget extends StatelessWidget {
29-
const MyStatelessWidget({super.key});
25+
class DividerExample extends StatelessWidget {
26+
const DividerExample({super.key});
3027

3128
@override
3229
Widget build(BuildContext context) {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
/// Flutter code sample for [Divider].
6+
7+
import 'package:flutter/material.dart';
8+
9+
void main() => runApp(const MyApp());
10+
11+
class MyApp extends StatelessWidget {
12+
const MyApp({super.key});
13+
14+
@override
15+
Widget build(BuildContext context) {
16+
return MaterialApp(
17+
theme: ThemeData(colorSchemeSeed: const Color(0xff6750a4), useMaterial3: true),
18+
home: Scaffold(
19+
appBar: AppBar(title: const Text('Divider Sample')),
20+
body: const DividerExample(),
21+
),
22+
);
23+
}
24+
}
25+
26+
class DividerExample extends StatelessWidget {
27+
const DividerExample({super.key});
28+
29+
@override
30+
Widget build(BuildContext context) {
31+
return Center(
32+
child: Padding(
33+
padding: const EdgeInsets.all(16.0),
34+
child: Row(
35+
children: const <Widget>[
36+
Expanded(
37+
child: Card(
38+
child: SizedBox.expand(),
39+
),
40+
),
41+
VerticalDivider(),
42+
Expanded(
43+
child: Card(
44+
child: SizedBox.expand(),
45+
),
46+
),
47+
],
48+
),
49+
),
50+
);
51+
}
52+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter_api_samples/material/divider/divider.0.dart' as example;
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
void main() {
10+
testWidgets('Horizontal Divider', (WidgetTester tester) async {
11+
await tester.pumpWidget(
12+
const MaterialApp(
13+
home: Scaffold(
14+
body: example.MyApp(),
15+
),
16+
),
17+
);
18+
19+
expect(find.byType(Divider), findsOneWidget);
20+
21+
// Divider is positioned horizintally.
22+
final Offset container = tester.getBottomLeft(find.byType(Container).first);
23+
expect(container.dy, tester.getTopLeft(find.byType(Divider)).dy);
24+
25+
final Offset subheader = tester.getTopLeft(find.text('Subheader'));
26+
expect(subheader.dy, tester.getBottomLeft(find.byType(Divider)).dy);
27+
});
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter_api_samples/material/divider/divider.1.dart' as example;
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
void main() {
10+
testWidgets('Horizontal Divider', (WidgetTester tester) async {
11+
await tester.pumpWidget(
12+
const MaterialApp(
13+
home: Scaffold(
14+
body: example.MyApp(),
15+
),
16+
),
17+
);
18+
19+
expect(find.byType(Divider), findsOneWidget);
20+
21+
// Divider is positioned horizontally.
22+
Offset card = tester.getBottomLeft(find.byType(Card).first);
23+
expect(card.dy, tester.getTopLeft(find.byType(Divider)).dy);
24+
25+
card = tester.getTopLeft(find.byType(Card).last);
26+
expect(card.dy, tester.getBottomLeft(find.byType(Divider)).dy);
27+
});
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter_api_samples/material/divider/vertical_divider.0.dart' as example;
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
void main() {
10+
testWidgets('Vertical Divider', (WidgetTester tester) async {
11+
await tester.pumpWidget(
12+
const MaterialApp(
13+
home: Scaffold(
14+
body: example.MyApp(),
15+
),
16+
),
17+
);
18+
19+
expect(find.byType(VerticalDivider), findsOneWidget);
20+
21+
// Divider is positioned horizintally.
22+
Offset expanded = tester.getTopRight(find.byType(Expanded).first);
23+
expect(expanded.dx, tester.getTopLeft(find.byType(VerticalDivider)).dx);
24+
25+
expanded = tester.getTopLeft(find.byType(Expanded).last);
26+
expect(expanded.dx, tester.getTopRight(find.byType(VerticalDivider)).dx);
27+
});
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter_api_samples/material/divider/vertical_divider.1.dart' as example;
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
void main() {
10+
testWidgets('Vertical Divider', (WidgetTester tester) async {
11+
await tester.pumpWidget(
12+
const MaterialApp(
13+
home: Scaffold(
14+
body: example.MyApp(),
15+
),
16+
),
17+
);
18+
19+
expect(find.byType(VerticalDivider), findsOneWidget);
20+
21+
// Divider is positioned vertically.
22+
Offset card = tester.getTopRight(find.byType(Card).first);
23+
expect(card.dx, tester.getTopLeft(find.byType(VerticalDivider)).dx);
24+
25+
card = tester.getTopLeft(find.byType(Card).last);
26+
expect(card.dx, tester.getTopRight(find.byType(VerticalDivider)).dx);
27+
});
28+
}

0 commit comments

Comments
 (0)