CupertinoActionSheet: Update sample (#98356)

This commit is contained in:
Taha Tesser 2022-02-15 02:10:08 +02:00 committed by GitHub
parent 4d9e6d9b3e
commit 7d21dbf5e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 111 additions and 45 deletions

View file

@ -0,0 +1,82 @@
// 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.
// Flutter code sample for CupertinoActionSheet
import 'package:flutter/cupertino.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'CupertinoActionSheet Sample';
@override
Widget build(BuildContext context) {
return const CupertinoApp(
title: _title,
home: ActionSheetSample(title: _title),
);
}
}
class ActionSheetSample extends StatelessWidget {
const ActionSheetSample({Key? key, required this.title}) : super(key: key);
final String title;
// This shows a CupertinoModalPopup which hosts a CupertinoActionSheet.
void _showActionSheet(BuildContext context) {
showCupertinoModalPopup<void>(
context: context,
builder: (BuildContext context) => CupertinoActionSheet(
title: const Text('Title'),
message: const Text('Message'),
actions: <CupertinoActionSheetAction>[
CupertinoActionSheetAction(
/// This parameter indicates the action would be a default
/// defualt behavior, turns the action's text to bold text.
isDefaultAction: true,
onPressed: () {
Navigator.pop(context);
},
child: const Text('Default Action'),
),
CupertinoActionSheetAction(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Action'),
),
CupertinoActionSheetAction(
/// This parameter indicates the action would perform
/// a destructive action such as delete or exit and turns
/// the action's text color to red.
isDestructiveAction: true,
onPressed: () {
Navigator.pop(context);
},
child: const Text('Destructive Action'),
)
],
),
);
}
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: Text(title),
),
child: Center(
child: CupertinoButton(
onPressed: () => _showActionSheet(context),
child: const Text('CupertinoActionSheet'),
),
),
);
}
}

View file

@ -0,0 +1,27 @@
// 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/cupertino.dart';
import 'package:flutter_api_samples/cupertino/dialog/cupertino_action_sheet.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Perform an action on CupertinoActionSheet', (WidgetTester tester) async {
const String actionText = 'Destructive Action';
await tester.pumpWidget(
const example.MyApp(),
);
// Launch the CupertinoActionSheet.
await tester.tap(find.byType(CupertinoButton));
await tester.pump();
await tester.pumpAndSettle();
expect(find.text(actionText), findsOneWidget);
// Tap on an action to close the CupertinoActionSheet.
await tester.tap(find.text(actionText));
await tester.pumpAndSettle();
expect(find.text(actionText), findsNothing);
});
}

View file

@ -494,55 +494,12 @@ class CupertinoPopupSurface extends StatelessWidget {
/// [showCupertinoModalPopup], which displays the action sheet by sliding it up
/// from the bottom of the screen.
///
/// {@tool snippet}
/// {@tool dartpad}
/// This sample shows how to use a [CupertinoActionSheet].
/// The [CupertinoActionSheet] shows a modal popup that slides in from the
/// bottom when [CupertinoButton] is pressed.
///
/// ```dart
/// class MyStatefulWidget extends StatefulWidget {
/// const MyStatefulWidget({Key? key}) : super(key: key);
///
/// @override
/// State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
/// }
///
/// class _MyStatefulWidgetState extends State<MyStatefulWidget> {
/// @override
/// Widget build(BuildContext context) {
/// return CupertinoPageScaffold(
/// child: Center(
/// child: CupertinoButton(
/// onPressed: () {
/// showCupertinoModalPopup<void>(
/// context: context,
/// builder: (BuildContext context) => CupertinoActionSheet(
/// title: const Text('Title'),
/// message: const Text('Message'),
/// actions: <CupertinoActionSheetAction>[
/// CupertinoActionSheetAction(
/// child: const Text('Action One'),
/// onPressed: () {
/// Navigator.pop(context);
/// },
/// ),
/// CupertinoActionSheetAction(
/// child: const Text('Action Two'),
/// onPressed: () {
/// Navigator.pop(context);
/// },
/// )
/// ],
/// ),
/// );
/// },
/// child: const Text('CupertinoActionSheet'),
/// ),
/// ),
/// );
/// }
/// }
/// ```
/// ** See code in examples/api/lib/cupertino/dialog/cupertino_action_sheet.0.dart **
/// {@end-tool}
///
/// See also: