Last active
March 1, 2024 03:00
-
-
Save kumamotone/3644b77d8909770acb54e1dab3b6fe1c to your computer and use it in GitHub Desktop.
showOkCancelAdaptiveDialog
This file contains hidden or 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
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Dialog Demo', | |
theme: ThemeData( | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
home: const OKCancelAdaptiveDialogCatalogScreen(), | |
); | |
} | |
} | |
class OKCancelAdaptiveDialogCatalogScreen extends StatelessWidget { | |
const OKCancelAdaptiveDialogCatalogScreen({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('ShowOKCancelAdaptiveDialog Demo'), | |
), | |
body: ListView( | |
padding: const EdgeInsets.all(8), | |
children: <Widget>[ | |
ElevatedButton( | |
onPressed: () async { | |
await showOkCancelAdaptiveDialog( | |
context: context, | |
title: 'アーカイブしますか?', | |
message: 'あとから元に戻すことができます', | |
); | |
}, | |
child: const Text('シンプルな確認'), | |
), | |
], | |
), | |
); | |
} | |
} | |
Future<OkCancelResult> showOkCancelAdaptiveDialog({ | |
required BuildContext context, | |
String? title, | |
String? message, | |
String okLabel = 'OK', | |
String cancelLabel = 'キャンセル', | |
OkCancelAlertDefaultType? defaultType, | |
bool isDestructiveAction = false, | |
bool barrierDismissible = true, | |
}) async { | |
const okResult = OkCancelResult.ok; | |
const cancelResult = OkCancelResult.cancel; | |
final result = await showAdaptiveDialog<OkCancelResult>( | |
context: context, | |
barrierDismissible: barrierDismissible, | |
builder: (BuildContext context) => AlertDialog.adaptive( | |
title: title != null ? Text(title) : null, | |
content: message != null ? Text(message) : null, | |
actions: <Widget>[ | |
_AdaptiveAction( | |
context: context, | |
onPressed: () => Navigator.pop(context, cancelResult), | |
isDefaultAction: defaultType == OkCancelAlertDefaultType.cancel, | |
labelString: cancelLabel, | |
), | |
_AdaptiveAction( | |
context: context, | |
onPressed: () => Navigator.pop(context, okResult), | |
isDefaultAction: | |
defaultType == null || defaultType == OkCancelAlertDefaultType.ok, | |
labelString: okLabel, | |
isDestructiveAction: isDestructiveAction, | |
), | |
], | |
), | |
); | |
return result ?? OkCancelResult.cancel; | |
} | |
class _AdaptiveAction extends StatelessWidget { | |
const _AdaptiveAction({ | |
required this.context, | |
required this.onPressed, | |
required this.isDefaultAction, | |
required this.labelString, | |
this.isDestructiveAction = false, | |
}); | |
final BuildContext context; | |
final VoidCallback onPressed; | |
final bool isDefaultAction; | |
final String labelString; | |
final bool isDestructiveAction; | |
@override | |
Widget build(BuildContext context) { | |
final theme = Theme.of(context); | |
switch (theme.platform) { | |
case TargetPlatform.android: | |
case TargetPlatform.fuchsia: | |
case TargetPlatform.linux: | |
case TargetPlatform.windows: | |
return TextButton( | |
onPressed: onPressed, | |
child: Text( | |
labelString, | |
style: isDestructiveAction | |
? TextStyle(color: Theme.of(context).colorScheme.error) | |
: null, | |
), | |
); | |
case TargetPlatform.iOS: | |
case TargetPlatform.macOS: | |
return CupertinoDialogAction( | |
onPressed: onPressed, | |
isDefaultAction: isDefaultAction, | |
isDestructiveAction: isDestructiveAction, | |
child: Text(labelString), | |
); | |
} | |
} | |
} | |
enum OkCancelResult { | |
ok, | |
cancel, | |
} | |
enum OkCancelAlertDefaultType { | |
ok, | |
cancel, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment