forked from firka/flutter
Adaptive alert dialog (#124336)
Fixes #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">
This commit is contained in:
@@ -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'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user