From 7d21dbf5e0c5c391ef9dbd1240e81d6d449ee495 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Tue, 15 Feb 2022 02:10:08 +0200 Subject: [PATCH] CupertinoActionSheet: Update sample (#98356) --- .../dialog/cupertino_action_sheet.0.dart | 82 +++++++++++++++++++ .../dialog/cupertino_action_sheet.0_test.dart | 27 ++++++ .../flutter/lib/src/cupertino/dialog.dart | 47 +---------- 3 files changed, 111 insertions(+), 45 deletions(-) create mode 100644 examples/api/lib/cupertino/dialog/cupertino_action_sheet.0.dart create mode 100644 examples/api/test/cupertino/dialog/cupertino_action_sheet.0_test.dart diff --git a/examples/api/lib/cupertino/dialog/cupertino_action_sheet.0.dart b/examples/api/lib/cupertino/dialog/cupertino_action_sheet.0.dart new file mode 100644 index 00000000000..507feee8d24 --- /dev/null +++ b/examples/api/lib/cupertino/dialog/cupertino_action_sheet.0.dart @@ -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( + context: context, + builder: (BuildContext context) => CupertinoActionSheet( + title: const Text('Title'), + message: const Text('Message'), + actions: [ + 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'), + ), + ), + ); + } +} diff --git a/examples/api/test/cupertino/dialog/cupertino_action_sheet.0_test.dart b/examples/api/test/cupertino/dialog/cupertino_action_sheet.0_test.dart new file mode 100644 index 00000000000..27c0ec46220 --- /dev/null +++ b/examples/api/test/cupertino/dialog/cupertino_action_sheet.0_test.dart @@ -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); + }); +} diff --git a/packages/flutter/lib/src/cupertino/dialog.dart b/packages/flutter/lib/src/cupertino/dialog.dart index ee72deb8c7f..b0d026d00d8 100644 --- a/packages/flutter/lib/src/cupertino/dialog.dart +++ b/packages/flutter/lib/src/cupertino/dialog.dart @@ -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 createState() => _MyStatefulWidgetState(); -/// } -/// -/// class _MyStatefulWidgetState extends State { -/// @override -/// Widget build(BuildContext context) { -/// return CupertinoPageScaffold( -/// child: Center( -/// child: CupertinoButton( -/// onPressed: () { -/// showCupertinoModalPopup( -/// context: context, -/// builder: (BuildContext context) => CupertinoActionSheet( -/// title: const Text('Title'), -/// message: const Text('Message'), -/// actions: [ -/// 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: