Dialog mit Ok und Cancel - wurzelsand/flutter-memos GitHub Wiki
Dialog Windows-Desktop:
Dialog Linux-Desktop, MacOS etc.:
import 'package:flutter/material.dart';
import 'dart:io' show Platform;
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: ExampleWidget(),
),
);
}
}
class ExampleWidget extends StatelessWidget {
ExampleWidget({super.key});
@override
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
onPressed: () {
showSimpleDialog(context)
.then((value) => print('Dialog closed with $value'));
},
child: const Text('Open')));
}
Future<bool> showSimpleDialog(BuildContext context) {
final Future<dynamic> future = showDialog(
context: context,
builder: (ctx) => SimpleDialog(
contentPadding: const EdgeInsets.all(12),
children: [
const Text('Do you really want to exit?'),
const SizedBox(height: 24),
createActionRow(ctx),
],
),
);
return Future(() => future.then((value) => value as bool));
}
final buttonDirection =
Platform.isWindows ? TextDirection.rtl : TextDirection.ltr;
Widget createActionRow(BuildContext context) {
return Row(
children: [
const Spacer(),
Row(
textDirection: buttonDirection,
children: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.pop(context, true),
child: const Text('Ok'),
),
],
),
],
);
}
}