forked from flutter/flutter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adaptive alert dialog (flutter#124336)
Fixes flutter#102811. Adds an adaptive constructor to AlertDialog, along with the adaptive function showAdaptiveDialog. <img width="357" alt="Screenshot 2023-04-06 at 10 40 18 AM" src="https://user-images.githubusercontent.com/58190796/230455412-31100922-cfc5-4252-b8c6-6f076353f29e.png"> <img width="350" alt="Screenshot 2023-04-06 at 10 42 50 AM" src="https://user-images.githubusercontent.com/58190796/230455454-363dd37e-c44e-4aca-b6a0-cfa1d959f606.png">
- Loading branch information
1 parent
c05bc40
commit bd2617e
Showing
4 changed files
with
391 additions
and
1 deletion.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
examples/api/lib/material/dialog/adaptive_alert_dialog.0.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// 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/material.dart'; | ||
|
||
/// Flutter code sample for [AlertDialog]. | ||
void main() => runApp(const AdaptiveAlertDialogApp()); | ||
|
||
class AdaptiveAlertDialogApp extends StatelessWidget { | ||
const AdaptiveAlertDialogApp({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
// Try this: set the platform to TargetPlatform.android and see the difference | ||
theme: ThemeData(platform: TargetPlatform.iOS, useMaterial3: true), | ||
home: Scaffold( | ||
appBar: AppBar(title: const Text('AlertDialog Sample')), | ||
body: const Center( | ||
child: AdaptiveDialogExample(), | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
class AdaptiveDialogExample extends StatelessWidget { | ||
const AdaptiveDialogExample({super.key}); | ||
|
||
Widget adaptiveAction({ | ||
required BuildContext context, | ||
required VoidCallback onPressed, | ||
required Widget child | ||
}) { | ||
final ThemeData theme = Theme.of(context); | ||
switch (theme.platform) { | ||
case TargetPlatform.android: | ||
case TargetPlatform.fuchsia: | ||
case TargetPlatform.linux: | ||
case TargetPlatform.windows: | ||
return TextButton(onPressed: onPressed, child: child); | ||
case TargetPlatform.iOS: | ||
case TargetPlatform.macOS: | ||
return CupertinoDialogAction(onPressed: onPressed, child: child); | ||
} | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return TextButton( | ||
onPressed: () => showAdaptiveDialog<String>( | ||
context: context, | ||
builder: (BuildContext context) => AlertDialog.adaptive( | ||
title: const Text('AlertDialog Title'), | ||
content: const Text('AlertDialog description'), | ||
actions: <Widget>[ | ||
adaptiveAction( | ||
context: context, | ||
onPressed: () => Navigator.pop(context, 'Cancel'), | ||
child: const Text('Cancel'), | ||
), | ||
adaptiveAction( | ||
context: context, | ||
onPressed: () => Navigator.pop(context, 'OK'), | ||
child: const Text('OK'), | ||
), | ||
], | ||
), | ||
), | ||
child: const Text('Show Dialog'), | ||
); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
examples/api/test/material/dialog/adaptive_alert_dialog.0_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// 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/dialog/adaptive_alert_dialog.0.dart' as example; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
testWidgets('Show Adaptive Alert dialog', (WidgetTester tester) async { | ||
const String dialogTitle = 'AlertDialog Title'; | ||
await tester.pumpWidget( | ||
const MaterialApp( | ||
home: Scaffold( | ||
body: example.AdaptiveAlertDialogApp(), | ||
), | ||
), | ||
); | ||
|
||
expect(find.text(dialogTitle), findsNothing); | ||
|
||
await tester.tap(find.widgetWithText(TextButton, 'Show Dialog')); | ||
await tester.pumpAndSettle(); | ||
expect(find.text(dialogTitle), findsOneWidget); | ||
|
||
await tester.tap(find.text('OK')); | ||
await tester.pumpAndSettle(); | ||
expect(find.text(dialogTitle), findsNothing); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.