Skip to content

Instantly share code, notes, and snippets.

@kumamotone
Last active March 1, 2024 02:57
Show Gist options
  • Save kumamotone/200c3487d4e91d5a10518b41c79c6bd1 to your computer and use it in GitHub Desktop.
Save kumamotone/200c3487d4e91d5a10518b41c79c6bd1 to your computer and use it in GitHub Desktop.
showOkAdaptiveDialog
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 OKAdaptiveDialogCatalogScreen(),
);
}
}
class OKAdaptiveDialogCatalogScreen extends StatelessWidget {
const OKAdaptiveDialogCatalogScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Dialog Demo'),
),
body: ListView(
padding: const EdgeInsets.all(8),
children: <Widget>[
ElevatedButton(
onPressed: () async {
await showOkAdaptiveDialog(
context: context,
title: 'ダイアログっていうのは',
content: 'こういう部品だよ',
);
},
child: const Text('シンプルなエラーメッセージ'),
),
],
),
);
}
}
Future<void> showOkAdaptiveDialog({
required BuildContext context,
String? title,
String? content,
String okLabel = 'OK',
bool barrierDismissible = true,
}) async {
await showAdaptiveDialog<void>(
context: context,
barrierDismissible: barrierDismissible,
builder: (BuildContext context) => AlertDialog.adaptive(
title: title != null ? Text(title) : null,
content: content != null ? Text(content) : null,
actions: <Widget>[
TextButton(
onPressed: () => Navigator.pop(context),
child: Text(okLabel),
),
],
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment