showCreatePlanDialog function
Future<void>
showCreatePlanDialog(
- BuildContext context,
- NmPlanController controller,
- List<
NmCommandSet> commandSets
Show create plan dialog.
Implementation
Future<void> showCreatePlanDialog(BuildContext context, NmPlanController controller, List<NmCommandSet> commandSets) async {
final l10n = NmTelecom.l10n(context);
final id = TextEditingController();
bool cloneMode = true;
String? sourceId = controller.selected?.id ?? controller.records.firstOrNull?.id;
String? commandSetId = commandSets.firstOrNull?.id;
String? error;
await showDialog<void>(
context: context,
builder: (dialogContext) => StatefulBuilder(
builder: (context, setState) => AlertDialog(
title: Row(children: [const NmIcon(NmFugue.applicationBadgePlus), const SizedBox(width: 8), Text(l10n.planNewDialogTitle)]),
content: SizedBox(
width: 420,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SegmentedButton<bool>(
segments: [
ButtonSegment(value: true, label: Text(l10n.planClone), icon: const NmIcon(NmFugue.applicationsStack)),
ButtonSegment(value: false, label: Text(l10n.planBlankSegment), icon: const NmIcon(NmFugue.applicationForm)),
],
selected: {cloneMode},
onSelectionChanged: (value) => setState(() => cloneMode = value.first),
),
if (cloneMode) ...[
const SizedBox(height: 12),
DropdownButtonFormField<String>(
isExpanded: true,
initialValue: sourceId,
decoration: InputDecoration(labelText: l10n.planSourceLabel, isDense: true),
items: [for (final plan in controller.records) DropdownMenuItem(value: plan.id, child: Text(plan.id))],
onChanged: (v) => setState(() => sourceId = v),
),
],
const SizedBox(height: 12),
TextField(
controller: id,
style: context.nm.text.body,
decoration: InputDecoration(
labelText: l10n.planIdFieldLabel,
isDense: true,
border: OutlineInputBorder(borderRadius: BorderRadius.circular(context.nm.sizes.radiusControl)),
),
),
const SizedBox(height: 10),
DropdownButtonFormField<String>(
isExpanded: true,
initialValue: commandSetId,
decoration: InputDecoration(labelText: l10n.planCommandSetRequiredLabel, isDense: true),
items: [for (final set in commandSets) DropdownMenuItem(value: set.id, child: Text(set.name))],
onChanged: (v) => setState(() => commandSetId = v),
),
if (error != null) ...[
const SizedBox(height: 10),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const NmIcon(NmFugue.exclamation),
const SizedBox(width: 8),
Expanded(
child: Text(
error!,
style: context.nm.text.body.copyWith(color: context.nm.colors.dangerText, fontWeight: FontWeight.w600),
),
),
],
),
],
],
),
),
actions: [
TextButton(onPressed: () => Navigator.pop(dialogContext), child: Text(l10n.planCancel)),
FilledButton(
onPressed: () {
final idValue = id.text.trim();
if (idValue.isEmpty || commandSetId == null) {
setState(() => error = l10n.planValIdAndSetRequired);
return;
}
if (cloneMode && sourceId == null) {
setState(() => error = l10n.planValSourceRequired);
return;
}
try {
controller.createPlan(idValue, commandSetId!, l10n, cloneFromId: cloneMode ? sourceId : null);
Navigator.pop(dialogContext);
} on NmPlanRepositoryException catch (e) {
setState(() => error = e.message);
}
},
child: Text(l10n.planCreateButton),
),
],
),
),
);
id.dispose();
}