81 lines
2.3 KiB
Dart
81 lines
2.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
/// Result of [showEntryDialog]: an entry id + content.
|
|
class EntryInput {
|
|
final String id;
|
|
final String content;
|
|
EntryInput(this.id, this.content);
|
|
}
|
|
|
|
/// Dialog to add or edit an entry.
|
|
/// When [lockedId] is true, the id field is disabled (editing existing entry).
|
|
Future<EntryInput?> showEntryDialog(
|
|
BuildContext context, {
|
|
required String title,
|
|
required String id,
|
|
required String initialContent,
|
|
required bool lockedId,
|
|
}) {
|
|
final idCtrl = TextEditingController(text: id);
|
|
final contentCtrl = TextEditingController(text: initialContent);
|
|
return showDialog<EntryInput>(
|
|
context: context,
|
|
builder: (ctx) => AlertDialog(
|
|
title: Text(title),
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
TextField(
|
|
controller: idCtrl,
|
|
enabled: !lockedId,
|
|
decoration: const InputDecoration(
|
|
labelText: 'ID (unik)', border: OutlineInputBorder()),
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextField(
|
|
controller: contentCtrl,
|
|
maxLines: 5,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Konten', border: OutlineInputBorder()),
|
|
),
|
|
],
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(ctx),
|
|
child: const Text('Batal')),
|
|
ElevatedButton(
|
|
onPressed: () => Navigator.pop(
|
|
ctx, EntryInput(idCtrl.text.trim(), contentCtrl.text)),
|
|
child: const Text('Simpan'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Confirmation dialog; returns true if user confirms.
|
|
Future<bool?> showConfirmDialog(
|
|
BuildContext context, {
|
|
required String title,
|
|
required String body,
|
|
}) {
|
|
return showDialog<bool>(
|
|
context: context,
|
|
builder: (ctx) => AlertDialog(
|
|
title: Text(title),
|
|
content: Text(body),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(ctx, false),
|
|
child: const Text('Batal')),
|
|
ElevatedButton(
|
|
style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
|
|
onPressed: () => Navigator.pop(ctx, true),
|
|
child: const Text('Hapus', style: TextStyle(color: Colors.white)),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|