Skip to content

Commit 4768961

Browse files
authored
1 parent e250c65 commit 4768961

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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/cupertino.dart';
6+
7+
/// Flutter code sample for [CupertinoPopupSurface].
8+
9+
void main() => runApp(const PopupSurfaceApp());
10+
11+
class PopupSurfaceApp extends StatelessWidget {
12+
const PopupSurfaceApp({super.key});
13+
14+
@override
15+
Widget build(BuildContext context) {
16+
return const CupertinoApp(
17+
home: PopupSurfaceExample(),
18+
);
19+
}
20+
}
21+
22+
class PopupSurfaceExample extends StatefulWidget {
23+
const PopupSurfaceExample({super.key});
24+
25+
@override
26+
State<PopupSurfaceExample> createState() => _PopupSurfaceExampleState();
27+
}
28+
29+
class _PopupSurfaceExampleState extends State<PopupSurfaceExample> {
30+
bool _shouldPaintSurface = true;
31+
32+
@override
33+
Widget build(BuildContext context) {
34+
return CupertinoPageScaffold(
35+
child: Center(
36+
child: Column(
37+
mainAxisAlignment: MainAxisAlignment.center,
38+
children: <Widget>[
39+
Row(
40+
mainAxisSize: MainAxisSize.min,
41+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
42+
children: <Widget>[
43+
const Text('Paint surface'),
44+
const SizedBox(width: 16.0),
45+
CupertinoSwitch(
46+
value: _shouldPaintSurface,
47+
onChanged: (bool value) => setState(() => _shouldPaintSurface = value),
48+
),
49+
],
50+
),
51+
CupertinoButton(
52+
onPressed: () => _showPopupSurface(context),
53+
child: const Text('Show popup'),
54+
),
55+
],
56+
),
57+
),
58+
);
59+
}
60+
61+
void _showPopupSurface(BuildContext context) {
62+
showCupertinoModalPopup<void>(
63+
context: context,
64+
builder: (BuildContext context) {
65+
return CupertinoPopupSurface(
66+
isSurfacePainted: _shouldPaintSurface,
67+
child: Container(
68+
height: 240,
69+
padding: const EdgeInsets.all(8.0),
70+
child: Column(
71+
children: <Widget>[
72+
Expanded(
73+
child: Container(
74+
alignment: Alignment.center,
75+
decoration: _shouldPaintSurface
76+
? null
77+
: BoxDecoration(
78+
color: CupertinoTheme.of(context).scaffoldBackgroundColor,
79+
borderRadius: BorderRadius.circular(8.0),
80+
),
81+
child: const Text('This is a popup surface.'),
82+
),
83+
),
84+
const SizedBox(height: 8.0),
85+
SizedBox(
86+
width: double.infinity,
87+
child: CupertinoButton(
88+
color: _shouldPaintSurface
89+
? null
90+
: CupertinoTheme.of(context).scaffoldBackgroundColor,
91+
onPressed: () => Navigator.pop(context),
92+
child: const Text(
93+
'Close',
94+
style: TextStyle(color: CupertinoColors.systemBlue),
95+
),
96+
),
97+
),
98+
],
99+
),
100+
),
101+
);
102+
},
103+
);
104+
}
105+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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/cupertino.dart';
6+
import 'package:flutter_api_samples/cupertino/dialog/cupertino_popup_surface.0.dart'
7+
as example;
8+
import 'package:flutter_test/flutter_test.dart';
9+
10+
void main() {
11+
testWidgets('CupertinoPopupSurface displays expected widgets in init state',
12+
(WidgetTester tester) async {
13+
await tester.pumpWidget(const example.PopupSurfaceApp());
14+
15+
final Finder cupertinoButton = find.byType(CupertinoButton);
16+
expect(cupertinoButton, findsOneWidget);
17+
18+
final Finder cupertinoSwitch = find.byType(CupertinoSwitch);
19+
expect(cupertinoSwitch, findsOneWidget);
20+
});
21+
22+
testWidgets('CupertinoPopupSurface is displayed with painted surface',
23+
(WidgetTester tester) async {
24+
await tester.pumpWidget(const example.PopupSurfaceApp());
25+
26+
// CupertinoSwitch is toggled on by default.
27+
expect(tester.widget<CupertinoSwitch>(find.byType(CupertinoSwitch)).value, isTrue);
28+
29+
// Tap on the CupertinoButton to show the CupertinoPopupSurface.
30+
await tester.tap(find.byType(CupertinoButton));
31+
await tester.pumpAndSettle();
32+
33+
// Make sure CupertinoPopupSurface is showing.
34+
final Finder cupertinoPopupSurface = find.byType(CupertinoPopupSurface);
35+
expect(cupertinoPopupSurface, findsOneWidget);
36+
37+
// Confirm that CupertinoPopupSurface is painted with a ColoredBox.
38+
final Finder coloredBox = find.descendant(
39+
of: cupertinoPopupSurface,
40+
matching: find.byType(ColoredBox),
41+
);
42+
expect(coloredBox, findsOneWidget);
43+
});
44+
45+
testWidgets('CupertinoPopupSurface is displayed without painted surface',
46+
(WidgetTester tester) async {
47+
await tester.pumpWidget(const example.PopupSurfaceApp());
48+
49+
// Toggling off CupertinoSwitch and confirm its state.
50+
final Finder cupertinoSwitch = find.byType(CupertinoSwitch);
51+
await tester.tap(cupertinoSwitch);
52+
await tester.pumpAndSettle();
53+
expect(tester.widget<CupertinoSwitch>(cupertinoSwitch).value, isFalse);
54+
55+
// Tap on the CupertinoButton to show the CupertinoPopupSurface.
56+
await tester.tap(find.byType(CupertinoButton));
57+
await tester.pumpAndSettle();
58+
59+
// Make sure CupertinoPopupSurface is showing.
60+
final Finder cupertinoPopupSurface = find.byType(CupertinoPopupSurface);
61+
expect(cupertinoPopupSurface, findsOneWidget);
62+
63+
// Confirm that CupertinoPopupSurface is not painted with a ColoredBox.
64+
final Finder coloredBox = find.descendant(
65+
of: cupertinoPopupSurface,
66+
matching: find.byType(ColoredBox),
67+
);
68+
expect(coloredBox, findsNothing);
69+
});
70+
}

packages/flutter/lib/src/cupertino/dialog.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,14 @@ class _CupertinoAlertDialogState extends State<CupertinoAlertDialog> {
419419
/// Additionally, the white paint can be disabled to render a blurred rounded
420420
/// rectangle without any color (similar to iOS's volume control popup).
421421
///
422+
/// {@tool dartpad}
423+
/// This sample shows how to use a [CupertinoPopupSurface]. The [CupertinoPopupSurface]
424+
/// shows a model popup from the bottom of the screen.
425+
/// Toggling the switch to configure its surface color.
426+
///
427+
/// ** See code in examples/api/lib/cupertino/dialog/cupertino_popup_surface.0.dart **
428+
/// {@end-tool}
429+
///
422430
/// See also:
423431
///
424432
/// * [CupertinoAlertDialog], which is a dialog with a title, content, and

0 commit comments

Comments
 (0)