mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

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">
77 lines
2.3 KiB
Dart
77 lines
2.3 KiB
Dart
// 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'),
|
|
);
|
|
}
|
|
}
|