mirror of
https://github.com/kuhyx/todo-app.git
synced 2026-11-23 13:26:24 +01:00
Split note_editor.dart into nine files under the cap
702 -> 220 lines, decomposed rather than just moved, because a single
~600-line State class was the actual problem:
note_editor_document.dart the text model -- template, controllers, and
which of the two sources (raw body vs. section
fields) is authoritative. None of it needed a
BuildContext or a setState, which is what made
it the honest seam. The raw<->guided conversions
and the initial-template detection moved here as
tryAdoptGuided/materialiseToRaw/forInitialText.
note_editor_wizard.dart the two-step priority+template wizard, which now
owns its own step state instead of parking three
fields on the editor.
note_editor_step_page.dart the guided per-step view.
note_editor_chrome.dart the template picker + mode toggle.
note_editor_raw_field.dart the raw-mode text field.
note_editor_mode.dart the mode enum, re-exported so callers are
unaffected.
note_editor_{widget,views} parts, not libraries: createState returns a
private class, so the widget and its State
cannot be split across libraries.
The layout comments about Expanded/expands:true are load-bearing (nesting
broke the constraint chain in release builds before), so they travelled with
the code they describe rather than being summarised away.
Verified: 313 tests pass, flutter analyze clean. Covered lines went UP,
1922 -> 1958, because the wizard's state is now exercised in a widget of its
own; every new file reports 100%.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
9d2aa1c178
commit
63725e726f
@ -3,112 +3,22 @@ import 'package:flutter/material.dart';
|
||||
import 'package:todo/data/note.dart';
|
||||
import 'package:todo/data/note_template.dart';
|
||||
import 'package:todo/ui/markdown_view.dart';
|
||||
import 'package:todo/ui/theme.dart';
|
||||
import 'package:todo/ui/note_editor_chrome.dart';
|
||||
import 'package:todo/ui/note_editor_document.dart';
|
||||
import 'package:todo/ui/note_editor_mode.dart';
|
||||
import 'package:todo/ui/note_editor_raw_field.dart';
|
||||
import 'package:todo/ui/note_editor_step_page.dart';
|
||||
import 'package:todo/ui/note_editor_wizard.dart';
|
||||
|
||||
/// Which view of the note the editor is currently showing.
|
||||
enum NoteEditorMode {
|
||||
/// Read-only rendered Markdown (headings, guidance, bullets).
|
||||
preview,
|
||||
export 'package:todo/ui/note_editor_mode.dart';
|
||||
|
||||
/// Full-screen per-step view, one step per template section.
|
||||
guided,
|
||||
|
||||
/// A single text field showing the assembled Markdown verbatim.
|
||||
raw,
|
||||
}
|
||||
|
||||
/// A guided editor for a note's text, shared by the capture and detail
|
||||
/// screens.
|
||||
///
|
||||
/// It is a *view* over plain text: it parses [initialText] into template
|
||||
/// sections and reports the re-assembled text through [onChanged] on every
|
||||
/// edit. Storage stays plain text, so sync and markdown export are unaffected.
|
||||
///
|
||||
/// Modes (see [NoteEditorMode]):
|
||||
/// * **Preview** — the note rendered as Markdown, read-only.
|
||||
/// * **Guided** — a full-screen per-step view, one step per template
|
||||
/// section, with guidance on what to write and why the LLM needs it.
|
||||
/// * **Raw** — a single text field showing the assembled text verbatim.
|
||||
///
|
||||
/// Non-conforming or freeform text never enters the guided stepper (we never
|
||||
/// force it into the template), so for such text Guided is unavailable and the
|
||||
/// editable source stays the raw body, preserving the user's content.
|
||||
///
|
||||
/// Entering Guided on an empty draft first runs a two-step wizard (priority,
|
||||
/// then template) via [onPriorityChanged], since those choices only make
|
||||
/// sense once, before there's anything to guide. Guided itself — wizard or
|
||||
/// bare step page — hides the template/mode chrome entirely (just a back arrow
|
||||
/// to return to Raw); [onChromeVisibleChanged] tells the parent screen to
|
||||
/// hide its own priority/status row in sync.
|
||||
class NoteEditor extends StatefulWidget {
|
||||
/// Creates a [NoteEditor] with the given callbacks and initial state.
|
||||
const NoteEditor({
|
||||
required this.onChanged,
|
||||
required this.priority,
|
||||
required this.onPriorityChanged,
|
||||
required this.onChromeVisibleChanged,
|
||||
this.initialText = '',
|
||||
this.initialTemplate,
|
||||
this.initialMode = NoteEditorMode.guided,
|
||||
this.autofocus = false,
|
||||
this.advancedMode = true,
|
||||
super.key,
|
||||
});
|
||||
|
||||
/// Called with the freshly assembled note text on every edit.
|
||||
final ValueChanged<String> onChanged;
|
||||
|
||||
/// The note's current priority, shown as the wizard's starting selection.
|
||||
final Priority priority;
|
||||
|
||||
/// Called when the priority wizard step is confirmed (Guided "Start").
|
||||
final ValueChanged<Priority> onPriorityChanged;
|
||||
|
||||
/// Called whenever the editor's own chrome (template dropdown, mode
|
||||
/// selector) is shown/hidden, so the parent screen can hide its
|
||||
/// priority/status row in sync while Guided (wizard or bare stepper) is
|
||||
/// active.
|
||||
final ValueChanged<bool> onChromeVisibleChanged;
|
||||
|
||||
/// Existing note text to load. Empty for a fresh draft.
|
||||
final String initialText;
|
||||
|
||||
/// Template to author with. When null the template is detected from
|
||||
/// [initialText] (used when opening an existing note).
|
||||
final NoteTemplate? initialTemplate;
|
||||
|
||||
/// Preferred mode to open in. Falls back to [NoteEditorMode.raw] when
|
||||
/// [NoteEditorMode.guided] is requested for text that can't be guided
|
||||
/// (freeform template or non-conforming content).
|
||||
final NoteEditorMode initialMode;
|
||||
|
||||
/// Autofocus the first field, so a fresh capture needs zero taps before
|
||||
/// typing — preserving the app's instant-capture invariant.
|
||||
final bool autofocus;
|
||||
|
||||
/// Whether the template picker and View/Guided/Raw mode toggle are shown.
|
||||
/// For a freshly-mounted editor, false pins the mode to
|
||||
/// [NoteEditorMode.raw] regardless of [initialMode], and Guided/Preview
|
||||
/// are unreachable — casual capture needs neither. Toggling this off
|
||||
/// while already mid-Guided does not itself force an immediate mode
|
||||
/// change (the open mode stays until the next mode transition, which
|
||||
/// then resolves to raw) — a transient, self-healing state, not a hole
|
||||
/// in the gating.
|
||||
final bool advancedMode;
|
||||
|
||||
@override
|
||||
State<NoteEditor> createState() => _NoteEditorState();
|
||||
}
|
||||
part 'note_editor_views.dart';
|
||||
part 'note_editor_widget.dart';
|
||||
|
||||
class _NoteEditorState extends State<NoteEditor> {
|
||||
late NoteTemplate _template;
|
||||
late final NoteEditorDocument _doc;
|
||||
late NoteEditorMode _mode;
|
||||
|
||||
/// Whether the editable content currently lives in [_body] (raw source)
|
||||
/// rather than the per-section controllers (guided source). Preview keeps
|
||||
/// whichever source was last active so [_currentText] stays correct.
|
||||
late bool _rawSource;
|
||||
|
||||
int _currentStep = 0;
|
||||
|
||||
/// Whether the priority+template entry wizard is showing in place of the
|
||||
@ -116,70 +26,24 @@ class _NoteEditorState extends State<NoteEditor> {
|
||||
/// either "Start" (which flips to bare Guided) or "Cancel" (back to Raw).
|
||||
bool _enteringGuided = false;
|
||||
|
||||
/// Which wizard step (0: priority, 1: template) is showing.
|
||||
int _wizardStep = 0;
|
||||
|
||||
/// Working copies of the wizard's two choices, committed on "Start".
|
||||
Priority _wizardPriority = Priority.defaultValue;
|
||||
NoteTemplate _wizardTemplate = NoteTemplate.defaultTemplate;
|
||||
|
||||
/// One controller per structured section (keyed by section key).
|
||||
final Map<String, TextEditingController> _section = {};
|
||||
|
||||
/// Set once the user picks a template themselves, which stops
|
||||
/// [_retemplateDraft] from overriding that choice as they keep typing.
|
||||
bool _templatePickedByUser = false;
|
||||
|
||||
/// Single field used for the freeform [NoteTemplate.blank] body and for raw
|
||||
/// mode of a structured template.
|
||||
final TextEditingController _body = TextEditingController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final initial = widget.initialTemplate;
|
||||
if (initial != null) {
|
||||
_template = initial;
|
||||
// Only prefer the guided (sections) source for an empty draft when the
|
||||
// caller actually wants to open in Guided — an empty Raw draft (the new
|
||||
// default) must keep the raw body as its source, or typing into the Raw
|
||||
// field would silently update the (hidden, unused) section controllers
|
||||
// instead of what's emitted via onChanged.
|
||||
_loadSource(
|
||||
initial,
|
||||
widget.initialText,
|
||||
preferGuided: widget.initialMode != NoteEditorMode.raw,
|
||||
);
|
||||
} else {
|
||||
// Detect: does the text cleanly fit the design-spec template?
|
||||
final parsed = parse(NoteTemplate.llmDesignSpec, widget.initialText);
|
||||
if (parsed.conforms) {
|
||||
_template = NoteTemplate.llmDesignSpec;
|
||||
// Same Raw/source consistency rule as above: an explicit Raw request
|
||||
// must keep the raw body as the source even for conforming text.
|
||||
if (widget.initialMode == NoteEditorMode.raw) {
|
||||
_rawSource = true;
|
||||
_body.text = widget.initialText;
|
||||
} else {
|
||||
_rawSource = false;
|
||||
_fillSections(parsed.values);
|
||||
}
|
||||
} else {
|
||||
// Freeform / legacy / hand-mangled — keep it as a raw body, untouched.
|
||||
_template = NoteTemplate.blank;
|
||||
_rawSource = true;
|
||||
_body.text = widget.initialText;
|
||||
}
|
||||
}
|
||||
_doc = NoteEditorDocument.forInitialText(
|
||||
widget.initialText,
|
||||
template: widget.initialTemplate,
|
||||
openingRaw: widget.initialMode == NoteEditorMode.raw,
|
||||
);
|
||||
_mode = _resolveMode(widget.initialMode);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
for (final c in _section.values) {
|
||||
c.dispose();
|
||||
}
|
||||
_body.dispose();
|
||||
_doc.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@ -187,7 +51,7 @@ class _NoteEditorState extends State<NoteEditor> {
|
||||
/// template whose current source still fits the template. Used to decide the
|
||||
/// initial mode; the Guided segment itself is offered for any structured
|
||||
/// template (a switch that no longer conforms is blocked at switch time).
|
||||
bool get _canOpenGuided => !_template.isFreeform && !_rawSource;
|
||||
bool get _canOpenGuided => !_doc.template.isFreeform && !_doc.rawSource;
|
||||
|
||||
/// Picks the mode to actually display: honours [desired] unless Guided was
|
||||
/// asked for when it can't be opened (falls back to Raw), or
|
||||
@ -201,58 +65,13 @@ class _NoteEditorState extends State<NoteEditor> {
|
||||
return desired;
|
||||
}
|
||||
|
||||
/// Ensures a controller exists for every section of [template].
|
||||
void _ensureControllers(NoteTemplate template) {
|
||||
for (final s in template.sections) {
|
||||
_section.putIfAbsent(s.key, TextEditingController.new);
|
||||
}
|
||||
}
|
||||
|
||||
void _goToStep(int index) {
|
||||
setState(() {
|
||||
_currentStep = index.clamp(0, _template.sections.length - 1);
|
||||
_currentStep = index.clamp(0, _doc.template.sections.length - 1);
|
||||
});
|
||||
}
|
||||
|
||||
void _fillSections(Map<String, String> values) {
|
||||
_ensureControllers(_template);
|
||||
for (final s in _template.sections) {
|
||||
_section[s.key]!.text = values[s.key] ?? '';
|
||||
}
|
||||
}
|
||||
|
||||
/// Loads [text] into [template], choosing the guided source when it conforms
|
||||
/// (or when [preferGuided] and the text is empty) and the raw body otherwise.
|
||||
void _loadSource(
|
||||
NoteTemplate template,
|
||||
String text, {
|
||||
required bool preferGuided,
|
||||
}) {
|
||||
if (template.isFreeform) {
|
||||
_rawSource = true;
|
||||
_body.text = text;
|
||||
return;
|
||||
}
|
||||
final parsed = parse(template, text);
|
||||
if (parsed.conforms || (preferGuided && text.trim().isEmpty)) {
|
||||
_rawSource = false;
|
||||
_fillSections(parsed.values);
|
||||
} else {
|
||||
_rawSource = true;
|
||||
_body.text = text;
|
||||
}
|
||||
}
|
||||
|
||||
/// The current note text, assembled from whichever source is active.
|
||||
String _currentText() {
|
||||
if (_rawSource) return _body.text;
|
||||
final values = {
|
||||
for (final s in _template.sections) s.key: _section[s.key]?.text ?? '',
|
||||
};
|
||||
return assemble(_template, values);
|
||||
}
|
||||
|
||||
void _emit() => widget.onChanged(_currentText());
|
||||
void _emit() => widget.onChanged(_doc.currentText());
|
||||
|
||||
/// Re-picks the draft's template from what has been typed so far, unless the
|
||||
/// user has chosen one explicitly.
|
||||
@ -262,23 +81,23 @@ class _NoteEditorState extends State<NoteEditor> {
|
||||
/// link and Guided drops away, write a sentence under it and the spec
|
||||
/// template comes back.
|
||||
void _retemplateDraft() {
|
||||
if (_templatePickedByUser || !_rawSource) return;
|
||||
final next = NoteTemplate.forDraft(_body.text);
|
||||
if (next.id == _template.id) return;
|
||||
if (_templatePickedByUser || !_doc.rawSource) return;
|
||||
final next = NoteTemplate.forDraft(_doc.body.text);
|
||||
if (next.id == _doc.template.id) return;
|
||||
setState(() {
|
||||
_template = next;
|
||||
_doc.template = next;
|
||||
_currentStep = 0;
|
||||
_mode = _resolveMode(_mode);
|
||||
});
|
||||
}
|
||||
|
||||
void _switchTemplate(NoteTemplate next) {
|
||||
if (next.id == _template.id) return;
|
||||
final text = _currentText();
|
||||
if (next.id == _doc.template.id) return;
|
||||
final text = _doc.currentText();
|
||||
setState(() {
|
||||
_template = next;
|
||||
_doc.template = next;
|
||||
_currentStep = 0;
|
||||
_loadSource(next, text, preferGuided: true);
|
||||
_doc.load(next, text, preferGuided: true);
|
||||
_mode = _resolveMode(_mode);
|
||||
});
|
||||
_emit();
|
||||
@ -290,42 +109,31 @@ class _NoteEditorState extends State<NoteEditor> {
|
||||
setState(() {
|
||||
switch (next) {
|
||||
case NoteEditorMode.guided:
|
||||
if (_rawSource) {
|
||||
// raw -> guided: only if the edited text still fits the template.
|
||||
final parsed = parse(_template, _body.text);
|
||||
if (!parsed.conforms && _body.text.trim().isNotEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text(
|
||||
"Text doesn't match the template — staying in raw",
|
||||
),
|
||||
duration: Duration(seconds: 2),
|
||||
// raw -> guided: only if the edited text still fits the template.
|
||||
if (!_doc.tryAdoptGuided()) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text(
|
||||
"Text doesn't match the template — staying in raw",
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
_fillSections(parsed.values);
|
||||
_rawSource = false;
|
||||
duration: Duration(seconds: 2),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
_currentStep = 0;
|
||||
if (_currentText().trim().isEmpty) {
|
||||
if (_doc.currentText().trim().isEmpty) {
|
||||
// Fresh, empty draft: priority and template are meaningful
|
||||
// choices only once, so ask for them before showing the stepper.
|
||||
_enteringGuided = true;
|
||||
_wizardStep = 0;
|
||||
_wizardPriority = widget.priority;
|
||||
_wizardTemplate = NoteTemplate.defaultTemplate;
|
||||
} else {
|
||||
// Existing content: priority/template are already settled, so
|
||||
// skip straight to the bare stepper rather than re-asking.
|
||||
_mode = NoteEditorMode.guided;
|
||||
}
|
||||
case NoteEditorMode.raw:
|
||||
if (!_rawSource) {
|
||||
// guided -> raw: materialise the assembled text into the body.
|
||||
_body.text = _currentText();
|
||||
_rawSource = true;
|
||||
}
|
||||
// guided -> raw: materialise the assembled text into the body.
|
||||
_doc.materialiseToRaw();
|
||||
_mode = NoteEditorMode.raw;
|
||||
case NoteEditorMode.preview:
|
||||
// Read-only render of the current source; nothing to convert.
|
||||
@ -341,13 +149,13 @@ class _NoteEditorState extends State<NoteEditor> {
|
||||
/// Distinct from [_switchTemplate]: that short-circuits when the template
|
||||
/// is unchanged, which would skip flipping out of the wizard here.
|
||||
void _enterGuidedWithTemplate(NoteTemplate template) {
|
||||
final text = _currentText();
|
||||
final text = _doc.currentText();
|
||||
// Choosing a template in the wizard is as explicit as using the dropdown.
|
||||
_templatePickedByUser = true;
|
||||
setState(() {
|
||||
_template = template;
|
||||
_doc.template = template;
|
||||
_currentStep = 0;
|
||||
_loadSource(template, text, preferGuided: true);
|
||||
_doc.load(template, text, preferGuided: true);
|
||||
_mode = NoteEditorMode.guided;
|
||||
_enteringGuided = false;
|
||||
});
|
||||
@ -361,342 +169,53 @@ class _NoteEditorState extends State<NoteEditor> {
|
||||
widget.onChromeVisibleChanged(true);
|
||||
}
|
||||
|
||||
/// Exits the bare stepper back to Raw, restoring the chrome.
|
||||
void _exitGuided() {
|
||||
setState(() {
|
||||
if (!_rawSource) {
|
||||
_body.text = _currentText();
|
||||
_rawSource = true;
|
||||
}
|
||||
_mode = NoteEditorMode.raw;
|
||||
});
|
||||
widget.onChromeVisibleChanged(true);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
if (_enteringGuided) return _buildGuidedEntryWizard(theme);
|
||||
if (_enteringGuided) return _guidedEntryWizard();
|
||||
|
||||
// Guided hides the template/mode chrome entirely. Return the flat step
|
||||
// page directly so the TextField sits in THIS column's Expanded — same
|
||||
// depth as _buildRaw, avoiding the nested-Column/Expanded layout issue
|
||||
// that silently collapses the inner flex space in release builds.
|
||||
if (_mode == NoteEditorMode.guided) return _buildStepPage(theme);
|
||||
if (_mode == NoteEditorMode.guided) return _buildStepPage();
|
||||
|
||||
// Advanced mode off: the template picker and mode toggle are hidden and
|
||||
// _resolveMode() has already pinned _mode to raw, so skip straight to
|
||||
// the body directly (not wrapped in another Expanded — the parent
|
||||
// screen already wraps this whole widget in one, same as the guided
|
||||
// branch above).
|
||||
if (!widget.advancedMode) return _buildBody(theme);
|
||||
if (!widget.advancedMode) return _buildBody();
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
DropdownButtonFormField<String>(
|
||||
initialValue: _template.id,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Template',
|
||||
isDense: true,
|
||||
border: OutlineInputBorder(),
|
||||
contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
),
|
||||
items: [
|
||||
for (final t in NoteTemplate.all)
|
||||
DropdownMenuItem(value: t.id, child: Text(t.label)),
|
||||
],
|
||||
onChanged: (id) {
|
||||
if (id == null) return;
|
||||
NoteEditorChrome(
|
||||
template: _doc.template,
|
||||
mode: _mode,
|
||||
onTemplateSelected: (next) {
|
||||
_templatePickedByUser = true;
|
||||
_switchTemplate(NoteTemplate.all.firstWhere((t) => t.id == id));
|
||||
_switchTemplate(next);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: SegmentedButton<NoteEditorMode>(
|
||||
showSelectedIcon: false,
|
||||
segments: [
|
||||
const ButtonSegment(
|
||||
value: NoteEditorMode.preview,
|
||||
icon: Icon(Icons.visibility_outlined),
|
||||
label: Text('View'),
|
||||
),
|
||||
// Guided is offered for any structured template; tapping it
|
||||
// on an empty draft opens the wizard (see _setMode), and is
|
||||
// blocked at switch time if the raw text no longer conforms.
|
||||
if (!_template.isFreeform)
|
||||
const ButtonSegment(
|
||||
value: NoteEditorMode.guided,
|
||||
icon: Icon(Icons.checklist),
|
||||
label: Text('Guided'),
|
||||
),
|
||||
const ButtonSegment(
|
||||
value: NoteEditorMode.raw,
|
||||
icon: Icon(Icons.notes),
|
||||
label: Text('Raw'),
|
||||
),
|
||||
],
|
||||
selected: {_mode},
|
||||
onSelectionChanged: (s) => _setMode(s.first),
|
||||
),
|
||||
onModeSelected: _setMode,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Expanded(child: _buildBody(theme)),
|
||||
Expanded(child: _buildBody()),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// The two-step priority -> template wizard shown before a fresh draft
|
||||
/// enters Guided. Replaces the normal chrome entirely (see [build]).
|
||||
Widget _buildGuidedEntryWizard(ThemeData theme) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.close),
|
||||
tooltip: 'Cancel',
|
||||
onPressed: _cancelWizard,
|
||||
),
|
||||
Text(
|
||||
'Step ${_wizardStep + 1} of 2',
|
||||
style: theme.textTheme.labelLarge,
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
if (_wizardStep == 0)
|
||||
..._buildWizardPriorityStep()
|
||||
else
|
||||
..._buildWizardTemplateStep(),
|
||||
],
|
||||
);
|
||||
/// Exits the bare stepper back to Raw, restoring the chrome.
|
||||
void _exitGuided() {
|
||||
setState(() {
|
||||
_doc.materialiseToRaw();
|
||||
_mode = NoteEditorMode.raw;
|
||||
});
|
||||
widget.onChromeVisibleChanged(true);
|
||||
}
|
||||
|
||||
List<Widget> _buildWizardPriorityStep() {
|
||||
return [
|
||||
DropdownButtonFormField<Priority>(
|
||||
initialValue: _wizardPriority,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Priority',
|
||||
isDense: true,
|
||||
border: OutlineInputBorder(),
|
||||
contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
),
|
||||
items: [
|
||||
for (final p in Priority.values)
|
||||
DropdownMenuItem(value: p, child: Text(p.label)),
|
||||
],
|
||||
onChanged: (p) {
|
||||
if (p != null) setState(() => _wizardPriority = p);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child: FilledButton(
|
||||
onPressed: () => setState(() => _wizardStep = 1),
|
||||
child: const Text('Next'),
|
||||
),
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
List<Widget> _buildWizardTemplateStep() {
|
||||
final templates = NoteTemplate.all.where((t) => !t.isFreeform).toList();
|
||||
return [
|
||||
DropdownButtonFormField<String>(
|
||||
initialValue: _wizardTemplate.id,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Template',
|
||||
isDense: true,
|
||||
border: OutlineInputBorder(),
|
||||
contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
),
|
||||
items: [
|
||||
for (final t in templates)
|
||||
DropdownMenuItem(value: t.id, child: Text(t.label)),
|
||||
],
|
||||
onChanged: (id) {
|
||||
if (id == null) return;
|
||||
setState(
|
||||
() => _wizardTemplate = templates.firstWhere((t) => t.id == id),
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: () => setState(() => _wizardStep = 0),
|
||||
child: const Text('Back'),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
FilledButton(
|
||||
onPressed: () {
|
||||
widget.onPriorityChanged(_wizardPriority);
|
||||
_enterGuidedWithTemplate(_wizardTemplate);
|
||||
},
|
||||
child: const Text('Start'),
|
||||
),
|
||||
],
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
Widget _buildBody(ThemeData theme) {
|
||||
switch (_mode) {
|
||||
case NoteEditorMode.preview:
|
||||
return MarkdownView(text: _currentText());
|
||||
case NoteEditorMode.raw:
|
||||
return _buildRaw(theme);
|
||||
// coverage:ignore-start
|
||||
// Unreachable: build() short-circuits to _buildStepPage before reaching
|
||||
// here, so this arm only exists to make the switch exhaustive.
|
||||
case NoteEditorMode.guided:
|
||||
return _buildRaw(theme);
|
||||
// coverage:ignore-end
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildRaw(ThemeData theme) {
|
||||
// Caps the editing column at ~70 chars (rule 21) on the desktop's
|
||||
// arbitrarily wide Chrome `--app` window. Center/ConstrainedBox only,
|
||||
// no extra Column — nesting a second Column here previously broke the
|
||||
// expands:true constraint chain in release builds (see _buildStepPage).
|
||||
return Center(
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: kProseMaxWidth),
|
||||
child: TextField(
|
||||
controller: _body,
|
||||
autofocus: widget.autofocus,
|
||||
maxLines: null,
|
||||
expands: true,
|
||||
textAlignVertical: TextAlignVertical.top,
|
||||
keyboardType: TextInputType.multiline,
|
||||
style: theme.textTheme.bodyLarge,
|
||||
decoration: const InputDecoration(
|
||||
border: InputBorder.none,
|
||||
hintText: 'Write your idea…',
|
||||
),
|
||||
onChanged: (_) {
|
||||
_retemplateDraft();
|
||||
_emit();
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Full-screen per-step view returned directly from [build] so the
|
||||
/// [TextField] is a first-level [Expanded] child of THIS column — the same
|
||||
/// depth as [_buildRaw]. Nesting it inside a second Column broke the
|
||||
/// flutter constraint chain in release builds (inner Expanded got 0 height).
|
||||
Widget _buildStepPage(ThemeData theme) {
|
||||
_ensureControllers(_template);
|
||||
final sections = _template.sections;
|
||||
final idx = _currentStep.clamp(0, sections.length - 1);
|
||||
final section = sections[idx];
|
||||
final total = sections.length;
|
||||
final controller = _section[section.key]!;
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
// Back arrow — only exit from guided mode.
|
||||
Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
tooltip: 'Exit guided',
|
||||
onPressed: _exitGuided,
|
||||
),
|
||||
),
|
||||
// Progress bar + step counter.
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 0, 16, 4),
|
||||
child: Row(
|
||||
children: [
|
||||
Text('${idx + 1} / $total', style: theme.textTheme.labelMedium),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: LinearProgressIndicator(value: (idx + 1) / total),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
// Section label + helper text.
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 8, 16, 0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
section.isTitle ? 'title' : section.label,
|
||||
style: theme.textTheme.titleMedium,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
section.helper,
|
||||
style: theme.textTheme.bodySmall?.copyWith(
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
],
|
||||
),
|
||||
),
|
||||
// TextField fills all remaining space — directly in this Column's
|
||||
// Expanded, same as _buildRaw, so expands:true works correctly.
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 0, 16, 0),
|
||||
child: TextField(
|
||||
controller: controller,
|
||||
autofocus: widget.autofocus && idx == 0,
|
||||
expands: true,
|
||||
maxLines: null,
|
||||
keyboardType: TextInputType.multiline,
|
||||
textAlignVertical: TextAlignVertical.top,
|
||||
decoration: InputDecoration(
|
||||
hintText: section.hint,
|
||||
border: const OutlineInputBorder(),
|
||||
isDense: true,
|
||||
),
|
||||
onChanged: (_) {
|
||||
setState(() {});
|
||||
_emit();
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
// Navigation buttons — sibling of Expanded so keyboard pushes them up.
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Row(
|
||||
children: [
|
||||
if (idx > 0)
|
||||
TextButton(
|
||||
onPressed: () => _goToStep(idx - 1),
|
||||
child: const Text('Back'),
|
||||
),
|
||||
const Spacer(),
|
||||
if (idx < total - 1)
|
||||
FilledButton(
|
||||
onPressed: () => _goToStep(idx + 1),
|
||||
child: const Text('Next'),
|
||||
)
|
||||
else
|
||||
FilledButton(onPressed: _exitGuided, child: const Text('Done')),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
/// Rebuilds after a guided step edit, then re-emits the assembled text.
|
||||
void _onStepFieldChanged() {
|
||||
setState(() {});
|
||||
_emit();
|
||||
}
|
||||
}
|
||||
|
||||
95
lib/ui/note_editor_chrome.dart
Normal file
95
lib/ui/note_editor_chrome.dart
Normal file
@ -0,0 +1,95 @@
|
||||
/// The editor's chrome: the template picker and the View/Guided/Raw toggle.
|
||||
///
|
||||
/// Split out of `note_editor.dart` for file size. Stateless and callback-only
|
||||
/// — every choice it offers is applied by `_NoteEditorState`, which owns the
|
||||
/// template and mode. Guided hides this chrome entirely, so it is only ever
|
||||
/// built in Preview and Raw.
|
||||
library;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:todo/data/note_template.dart';
|
||||
import 'package:todo/ui/note_editor_mode.dart';
|
||||
|
||||
/// Template dropdown above a segmented View / Guided / Raw selector.
|
||||
class NoteEditorChrome extends StatelessWidget {
|
||||
/// Creates the chrome for [template] in [mode].
|
||||
const NoteEditorChrome({
|
||||
required this.template,
|
||||
required this.mode,
|
||||
required this.onTemplateSelected,
|
||||
required this.onModeSelected,
|
||||
super.key,
|
||||
});
|
||||
|
||||
/// The template currently selected in the dropdown.
|
||||
final NoteTemplate template;
|
||||
|
||||
/// The mode the segmented button shows as selected.
|
||||
final NoteEditorMode mode;
|
||||
|
||||
/// Called with the template the user picked.
|
||||
final ValueChanged<NoteTemplate> onTemplateSelected;
|
||||
|
||||
/// Called with the mode the user picked.
|
||||
final ValueChanged<NoteEditorMode> onModeSelected;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
DropdownButtonFormField<String>(
|
||||
initialValue: template.id,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Template',
|
||||
isDense: true,
|
||||
border: OutlineInputBorder(),
|
||||
contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
),
|
||||
items: [
|
||||
for (final t in NoteTemplate.all)
|
||||
DropdownMenuItem(value: t.id, child: Text(t.label)),
|
||||
],
|
||||
onChanged: (id) {
|
||||
if (id == null) return;
|
||||
onTemplateSelected(
|
||||
NoteTemplate.all.firstWhere((t) => t.id == id),
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: SegmentedButton<NoteEditorMode>(
|
||||
showSelectedIcon: false,
|
||||
segments: [
|
||||
const ButtonSegment(
|
||||
value: NoteEditorMode.preview,
|
||||
icon: Icon(Icons.visibility_outlined),
|
||||
label: Text('View'),
|
||||
),
|
||||
// Guided is offered for any structured template; tapping it
|
||||
// on an empty draft opens the wizard, and is blocked at
|
||||
// switch time if the raw text no longer conforms.
|
||||
if (!template.isFreeform)
|
||||
const ButtonSegment(
|
||||
value: NoteEditorMode.guided,
|
||||
icon: Icon(Icons.checklist),
|
||||
label: Text('Guided'),
|
||||
),
|
||||
const ButtonSegment(
|
||||
value: NoteEditorMode.raw,
|
||||
icon: Icon(Icons.notes),
|
||||
label: Text('Raw'),
|
||||
),
|
||||
],
|
||||
selected: {mode},
|
||||
onSelectionChanged: (s) => onModeSelected(s.first),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
163
lib/ui/note_editor_document.dart
Normal file
163
lib/ui/note_editor_document.dart
Normal file
@ -0,0 +1,163 @@
|
||||
/// The editor's text state: the template, the controllers, and which of the
|
||||
/// two sources (raw body vs. per-section fields) is currently authoritative.
|
||||
///
|
||||
/// Split out of `note_editor.dart` for file size, and it is the honest seam:
|
||||
/// none of this needs a `BuildContext` or a `setState` — it is the model the
|
||||
/// widget renders. `_NoteEditorState` still drives every transition and still
|
||||
/// owns when to rebuild; this class just holds the text and converts between
|
||||
/// the two sources.
|
||||
///
|
||||
/// The two-source design is what keeps non-conforming text safe: text that
|
||||
/// does not fit the template is never forced through it, it stays verbatim in
|
||||
/// [body] and the editor shows the raw field instead.
|
||||
library;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:todo/data/note_template.dart';
|
||||
|
||||
/// Holds a note's editable text for one [NoteTemplate].
|
||||
class NoteEditorDocument {
|
||||
/// Creates a document for [template]; call [load] to put text in it.
|
||||
NoteEditorDocument(this.template);
|
||||
|
||||
/// Builds the document a freshly-mounted editor should start from.
|
||||
///
|
||||
/// With an explicit [template] the text is simply loaded into it. Without
|
||||
/// one the template is *detected*: text that cleanly fits the design-spec
|
||||
/// template adopts it, and anything else (freeform, legacy, hand-mangled)
|
||||
/// stays a raw body under [NoteTemplate.blank], untouched.
|
||||
///
|
||||
/// [openingRaw] keeps the raw body authoritative even for conforming text.
|
||||
/// Without it, typing into a Raw field would silently update the hidden,
|
||||
/// unused section controllers instead of what `onChanged` emits.
|
||||
factory NoteEditorDocument.forInitialText(
|
||||
String text, {
|
||||
required NoteTemplate? template,
|
||||
required bool openingRaw,
|
||||
}) {
|
||||
if (template != null) {
|
||||
return NoteEditorDocument(template)
|
||||
..load(template, text, preferGuided: !openingRaw);
|
||||
}
|
||||
final parsed = parse(NoteTemplate.llmDesignSpec, text);
|
||||
if (!parsed.conforms) {
|
||||
return NoteEditorDocument(NoteTemplate.blank)
|
||||
..rawSource = true
|
||||
..body.text = text;
|
||||
}
|
||||
final doc = NoteEditorDocument(NoteTemplate.llmDesignSpec);
|
||||
if (openingRaw) {
|
||||
doc
|
||||
..rawSource = true
|
||||
..body.text = text;
|
||||
} else {
|
||||
doc.adoptSections(parsed.values);
|
||||
}
|
||||
return doc;
|
||||
}
|
||||
|
||||
/// The template the text is authored against.
|
||||
NoteTemplate template;
|
||||
|
||||
/// Whether the editable content currently lives in [body] (raw source)
|
||||
/// rather than the per-section controllers (guided source). Preview keeps
|
||||
/// whichever source was last active so [currentText] stays correct.
|
||||
bool rawSource = true;
|
||||
|
||||
/// Single field used for the freeform [NoteTemplate.blank] body and for raw
|
||||
/// mode of a structured template.
|
||||
final TextEditingController body = TextEditingController();
|
||||
|
||||
/// One controller per structured section (keyed by section key).
|
||||
final Map<String, TextEditingController> section = {};
|
||||
|
||||
/// Ensures a controller exists for every section of [forTemplate].
|
||||
void ensureControllers(NoteTemplate forTemplate) {
|
||||
for (final s in forTemplate.sections) {
|
||||
section.putIfAbsent(s.key, TextEditingController.new);
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes [values] into the per-section controllers.
|
||||
void fillSections(Map<String, String> values) {
|
||||
ensureControllers(template);
|
||||
for (final s in template.sections) {
|
||||
section[s.key]!.text = values[s.key] ?? '';
|
||||
}
|
||||
}
|
||||
|
||||
/// Loads [text] into [forTemplate], choosing the guided source when it
|
||||
/// conforms (or when [preferGuided] and the text is empty) and the raw body
|
||||
/// otherwise.
|
||||
void load(
|
||||
NoteTemplate forTemplate,
|
||||
String text, {
|
||||
required bool preferGuided,
|
||||
}) {
|
||||
template = forTemplate;
|
||||
if (forTemplate.isFreeform) {
|
||||
rawSource = true;
|
||||
body.text = text;
|
||||
return;
|
||||
}
|
||||
final parsed = parse(forTemplate, text);
|
||||
if (parsed.conforms || (preferGuided && text.trim().isEmpty)) {
|
||||
rawSource = false;
|
||||
fillSections(parsed.values);
|
||||
} else {
|
||||
rawSource = true;
|
||||
body.text = text;
|
||||
}
|
||||
}
|
||||
|
||||
/// Adopts [values] as the guided source, making the section fields
|
||||
/// authoritative.
|
||||
///
|
||||
/// The raw -> guided direction, once the caller has confirmed the text
|
||||
/// still conforms to the template.
|
||||
void adoptSections(Map<String, String> values) {
|
||||
fillSections(values);
|
||||
rawSource = false;
|
||||
}
|
||||
|
||||
/// Tries to make the section fields authoritative, for raw -> guided.
|
||||
///
|
||||
/// Returns false and changes nothing when the edited raw text no longer
|
||||
/// fits the template — the caller keeps the editor in raw and says so,
|
||||
/// rather than forcing text through a template it does not match.
|
||||
/// Empty text always succeeds: there is nothing to lose.
|
||||
bool tryAdoptGuided() {
|
||||
if (!rawSource) return true;
|
||||
final parsed = parse(template, body.text);
|
||||
if (!parsed.conforms && body.text.trim().isNotEmpty) return false;
|
||||
adoptSections(parsed.values);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Moves the editable content into the raw body, leaving it authoritative.
|
||||
///
|
||||
/// Used on guided -> raw so the assembled text is what the user then edits.
|
||||
void materialiseToRaw() {
|
||||
if (rawSource) return;
|
||||
body.text = currentText();
|
||||
rawSource = true;
|
||||
}
|
||||
|
||||
/// The current note text, assembled from whichever source is active.
|
||||
String currentText() {
|
||||
if (rawSource) return body.text;
|
||||
final values = {
|
||||
for (final s in template.sections) s.key: section[s.key]?.text ?? '',
|
||||
};
|
||||
return assemble(template, values);
|
||||
}
|
||||
|
||||
/// Disposes every controller this document owns.
|
||||
void dispose() {
|
||||
for (final c in section.values) {
|
||||
c.dispose();
|
||||
}
|
||||
body.dispose();
|
||||
}
|
||||
}
|
||||
18
lib/ui/note_editor_mode.dart
Normal file
18
lib/ui/note_editor_mode.dart
Normal file
@ -0,0 +1,18 @@
|
||||
/// Which view of the note the editor is currently showing.
|
||||
///
|
||||
/// Its own file so the chrome and the editor can both name it without either
|
||||
/// importing the other. `note_editor.dart` re-exports it, so existing callers
|
||||
/// (the detail screen, the tests) are unaffected.
|
||||
library;
|
||||
|
||||
/// Which view of the note the editor is currently showing.
|
||||
enum NoteEditorMode {
|
||||
/// Read-only rendered Markdown (headings, guidance, bullets).
|
||||
preview,
|
||||
|
||||
/// Full-screen per-step view, one step per template section.
|
||||
guided,
|
||||
|
||||
/// A single text field showing the assembled Markdown verbatim.
|
||||
raw,
|
||||
}
|
||||
57
lib/ui/note_editor_raw_field.dart
Normal file
57
lib/ui/note_editor_raw_field.dart
Normal file
@ -0,0 +1,57 @@
|
||||
/// The raw-mode editing field: the assembled note text, verbatim.
|
||||
///
|
||||
/// Split out of `note_editor.dart` for file size. Stateless; the controller
|
||||
/// stays owned by `_NoteEditorState`.
|
||||
library;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:todo/ui/theme.dart';
|
||||
|
||||
/// A full-height plain-text field for the note's assembled Markdown.
|
||||
class NoteEditorRawField extends StatelessWidget {
|
||||
/// Creates the raw field over [controller].
|
||||
const NoteEditorRawField({
|
||||
required this.controller,
|
||||
required this.autofocus,
|
||||
required this.onChanged,
|
||||
super.key,
|
||||
});
|
||||
|
||||
/// Controller holding the raw body text.
|
||||
final TextEditingController controller;
|
||||
|
||||
/// Whether the field takes focus on first build.
|
||||
final bool autofocus;
|
||||
|
||||
/// Called on every keystroke.
|
||||
final VoidCallback onChanged;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Caps the editing column at ~70 chars (rule 21) on the desktop's
|
||||
// arbitrarily wide Chrome `--app` window. Center/ConstrainedBox only,
|
||||
// no extra Column — nesting a second Column here previously broke the
|
||||
// expands:true constraint chain in release builds (see
|
||||
// NoteEditorStepPage, which documents the same hazard).
|
||||
return Center(
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: kProseMaxWidth),
|
||||
child: TextField(
|
||||
controller: controller,
|
||||
autofocus: autofocus,
|
||||
maxLines: null,
|
||||
expands: true,
|
||||
textAlignVertical: TextAlignVertical.top,
|
||||
keyboardType: TextInputType.multiline,
|
||||
style: Theme.of(context).textTheme.bodyLarge,
|
||||
decoration: const InputDecoration(
|
||||
border: InputBorder.none,
|
||||
hintText: 'Write your idea…',
|
||||
),
|
||||
onChanged: (_) => onChanged(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
151
lib/ui/note_editor_step_page.dart
Normal file
151
lib/ui/note_editor_step_page.dart
Normal file
@ -0,0 +1,151 @@
|
||||
/// The guided stepper's full-screen per-step view.
|
||||
///
|
||||
/// Split out of `note_editor.dart` for file size. Stateless on purpose: the
|
||||
/// controllers, the current step and the template all stay owned by
|
||||
/// `_NoteEditorState`, and are passed in — this widget only lays them out.
|
||||
///
|
||||
/// Returned directly from the editor's `build` so the [TextField] is a
|
||||
/// first-level [Expanded] child of THIS column, the same depth as the raw
|
||||
/// editor's. Nesting it inside a second Column broke the flutter constraint
|
||||
/// chain in release builds (the inner Expanded got 0 height), so the flat
|
||||
/// structure here is load-bearing rather than incidental.
|
||||
library;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:todo/data/note_template.dart';
|
||||
|
||||
/// One step of the guided editor: label, guidance, input, and navigation.
|
||||
class NoteEditorStepPage extends StatelessWidget {
|
||||
/// Creates the step view for [section], step [index] of [total].
|
||||
const NoteEditorStepPage({
|
||||
required this.section,
|
||||
required this.controller,
|
||||
required this.index,
|
||||
required this.total,
|
||||
required this.autofocus,
|
||||
required this.onChanged,
|
||||
required this.onGoToStep,
|
||||
required this.onExit,
|
||||
super.key,
|
||||
});
|
||||
|
||||
/// The template section this step edits.
|
||||
final TemplateSection section;
|
||||
|
||||
/// Controller for this section's text, owned by the editor.
|
||||
final TextEditingController controller;
|
||||
|
||||
/// Zero-based index of this step, already clamped to the section range.
|
||||
final int index;
|
||||
|
||||
/// How many steps the template has in total.
|
||||
final int total;
|
||||
|
||||
/// Whether the input should take focus on first build.
|
||||
final bool autofocus;
|
||||
|
||||
/// Called on every keystroke so the editor can re-emit the assembled text.
|
||||
final VoidCallback onChanged;
|
||||
|
||||
/// Called with the step index to move to.
|
||||
final ValueChanged<int> onGoToStep;
|
||||
|
||||
/// Called by the back arrow and by "Done" on the last step.
|
||||
final VoidCallback onExit;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
// Back arrow — only exit from guided mode.
|
||||
Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
tooltip: 'Exit guided',
|
||||
onPressed: onExit,
|
||||
),
|
||||
),
|
||||
// Progress bar + step counter.
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 0, 16, 4),
|
||||
child: Row(
|
||||
children: [
|
||||
Text('${index + 1} / $total', style: theme.textTheme.labelMedium),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: LinearProgressIndicator(value: (index + 1) / total),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
// Section label + helper text.
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 8, 16, 0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
section.isTitle ? 'title' : section.label,
|
||||
style: theme.textTheme.titleMedium,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
section.helper,
|
||||
style: theme.textTheme.bodySmall?.copyWith(
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
],
|
||||
),
|
||||
),
|
||||
// TextField fills all remaining space — directly in this Column's
|
||||
// Expanded, same as the raw editor, so expands:true works correctly.
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 0, 16, 0),
|
||||
child: TextField(
|
||||
controller: controller,
|
||||
autofocus: autofocus && index == 0,
|
||||
expands: true,
|
||||
maxLines: null,
|
||||
keyboardType: TextInputType.multiline,
|
||||
textAlignVertical: TextAlignVertical.top,
|
||||
decoration: InputDecoration(
|
||||
hintText: section.hint,
|
||||
border: const OutlineInputBorder(),
|
||||
isDense: true,
|
||||
),
|
||||
onChanged: (_) => onChanged(),
|
||||
),
|
||||
),
|
||||
),
|
||||
// Navigation buttons — sibling of Expanded so keyboard pushes them up.
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Row(
|
||||
children: [
|
||||
if (index > 0)
|
||||
TextButton(
|
||||
onPressed: () => onGoToStep(index - 1),
|
||||
child: const Text('Back'),
|
||||
),
|
||||
const Spacer(),
|
||||
if (index < total - 1)
|
||||
FilledButton(
|
||||
onPressed: () => onGoToStep(index + 1),
|
||||
child: const Text('Next'),
|
||||
)
|
||||
else
|
||||
FilledButton(onPressed: onExit, child: const Text('Done')),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
65
lib/ui/note_editor_views.dart
Normal file
65
lib/ui/note_editor_views.dart
Normal file
@ -0,0 +1,65 @@
|
||||
/// The [NoteEditor]'s view wiring: the small methods that hand this editor's
|
||||
/// state to the extracted presentation widgets.
|
||||
///
|
||||
/// A `part` of `note_editor.dart` because every method here reads private
|
||||
/// state (`_doc`, `_mode`, `_currentStep`) that Dart scopes to the library.
|
||||
part of 'note_editor.dart';
|
||||
|
||||
extension _NoteEditorViews on _NoteEditorState {
|
||||
/// The pre-guided wizard. Its step state lives in the widget itself; only
|
||||
/// the two committed choices come back here.
|
||||
Widget _guidedEntryWizard() => NoteEditorWizard(
|
||||
initialPriority: widget.priority,
|
||||
initialTemplate: NoteTemplate.defaultTemplate,
|
||||
onCancel: _cancelWizard,
|
||||
onStart: (priority, template) {
|
||||
widget.onPriorityChanged(priority);
|
||||
_enterGuidedWithTemplate(template);
|
||||
},
|
||||
);
|
||||
|
||||
/// The raw field, wired to this editor's body controller.
|
||||
Widget _rawField() => NoteEditorRawField(
|
||||
controller: _doc.body,
|
||||
autofocus: widget.autofocus,
|
||||
onChanged: () {
|
||||
_retemplateDraft();
|
||||
_emit();
|
||||
},
|
||||
);
|
||||
|
||||
Widget _buildBody() {
|
||||
switch (_mode) {
|
||||
case NoteEditorMode.preview:
|
||||
return MarkdownView(text: _doc.currentText());
|
||||
case NoteEditorMode.raw:
|
||||
return _rawField();
|
||||
// coverage:ignore-start
|
||||
// Unreachable: build() short-circuits to _buildStepPage before reaching
|
||||
// here, so this arm only exists to make the switch exhaustive.
|
||||
case NoteEditorMode.guided:
|
||||
return _rawField();
|
||||
// coverage:ignore-end
|
||||
}
|
||||
}
|
||||
|
||||
/// Full-screen per-step view returned directly from [build] so the
|
||||
/// TextField is a first-level Expanded child of that column -- see
|
||||
/// [NoteEditorStepPage], which documents why the flat structure matters.
|
||||
Widget _buildStepPage() {
|
||||
_doc.ensureControllers(_doc.template);
|
||||
final sections = _doc.template.sections;
|
||||
final idx = _currentStep.clamp(0, sections.length - 1);
|
||||
final section = sections[idx];
|
||||
return NoteEditorStepPage(
|
||||
section: section,
|
||||
controller: _doc.section[section.key]!,
|
||||
index: idx,
|
||||
total: sections.length,
|
||||
autofocus: widget.autofocus,
|
||||
onGoToStep: _goToStep,
|
||||
onExit: _exitGuided,
|
||||
onChanged: _onStepFieldChanged,
|
||||
);
|
||||
}
|
||||
}
|
||||
90
lib/ui/note_editor_widget.dart
Normal file
90
lib/ui/note_editor_widget.dart
Normal file
@ -0,0 +1,90 @@
|
||||
/// The [NoteEditor] widget's public surface: its parameters and their
|
||||
/// contracts. The state that drives them lives in `note_editor.dart`.
|
||||
///
|
||||
/// A `part` rather than a separate library because the widget and its State
|
||||
/// are genuinely one unit -- `createState` returns a private class, so they
|
||||
/// cannot be split across libraries without making that class public.
|
||||
part of 'note_editor.dart';
|
||||
|
||||
/// A guided editor for a note's text, shared by the capture and detail
|
||||
/// screens.
|
||||
///
|
||||
/// It is a *view* over plain text: it parses [initialText] into template
|
||||
/// sections and reports the re-assembled text through [onChanged] on every
|
||||
/// edit. Storage stays plain text, so sync and markdown export are unaffected.
|
||||
///
|
||||
/// Modes (see [NoteEditorMode]):
|
||||
/// * **Preview** — the note rendered as Markdown, read-only.
|
||||
/// * **Guided** — a full-screen per-step view, one step per template
|
||||
/// section, with guidance on what to write and why the LLM needs it.
|
||||
/// * **Raw** — a single text field showing the assembled text verbatim.
|
||||
///
|
||||
/// Non-conforming or freeform text never enters the guided stepper (we never
|
||||
/// force it into the template), so for such text Guided is unavailable and the
|
||||
/// editable source stays the raw body, preserving the user's content.
|
||||
///
|
||||
/// Entering Guided on an empty draft first runs a two-step wizard (priority,
|
||||
/// then template) via [onPriorityChanged], since those choices only make
|
||||
/// sense once, before there's anything to guide. Guided itself — wizard or
|
||||
/// bare step page — hides the template/mode chrome entirely (just a back arrow
|
||||
/// to return to Raw); [onChromeVisibleChanged] tells the parent screen to
|
||||
/// hide its own priority/status row in sync.
|
||||
class NoteEditor extends StatefulWidget {
|
||||
/// Creates a [NoteEditor] with the given callbacks and initial state.
|
||||
const NoteEditor({
|
||||
required this.onChanged,
|
||||
required this.priority,
|
||||
required this.onPriorityChanged,
|
||||
required this.onChromeVisibleChanged,
|
||||
this.initialText = '',
|
||||
this.initialTemplate,
|
||||
this.initialMode = NoteEditorMode.guided,
|
||||
this.autofocus = false,
|
||||
this.advancedMode = true,
|
||||
super.key,
|
||||
});
|
||||
|
||||
/// Called with the freshly assembled note text on every edit.
|
||||
final ValueChanged<String> onChanged;
|
||||
|
||||
/// The note's current priority, shown as the wizard's starting selection.
|
||||
final Priority priority;
|
||||
|
||||
/// Called when the priority wizard step is confirmed (Guided "Start").
|
||||
final ValueChanged<Priority> onPriorityChanged;
|
||||
|
||||
/// Called whenever the editor's own chrome (template dropdown, mode
|
||||
/// selector) is shown/hidden, so the parent screen can hide its
|
||||
/// priority/status row in sync while Guided (wizard or bare stepper) is
|
||||
/// active.
|
||||
final ValueChanged<bool> onChromeVisibleChanged;
|
||||
|
||||
/// Existing note text to load. Empty for a fresh draft.
|
||||
final String initialText;
|
||||
|
||||
/// Template to author with. When null the template is detected from
|
||||
/// [initialText] (used when opening an existing note).
|
||||
final NoteTemplate? initialTemplate;
|
||||
|
||||
/// Preferred mode to open in. Falls back to [NoteEditorMode.raw] when
|
||||
/// [NoteEditorMode.guided] is requested for text that can't be guided
|
||||
/// (freeform template or non-conforming content).
|
||||
final NoteEditorMode initialMode;
|
||||
|
||||
/// Autofocus the first field, so a fresh capture needs zero taps before
|
||||
/// typing — preserving the app's instant-capture invariant.
|
||||
final bool autofocus;
|
||||
|
||||
/// Whether the template picker and View/Guided/Raw mode toggle are shown.
|
||||
/// For a freshly-mounted editor, false pins the mode to
|
||||
/// [NoteEditorMode.raw] regardless of [initialMode], and Guided/Preview
|
||||
/// are unreachable — casual capture needs neither. Toggling this off
|
||||
/// while already mid-Guided does not itself force an immediate mode
|
||||
/// change (the open mode stays until the next mode transition, which
|
||||
/// then resolves to raw) — a transient, self-healing state, not a hole
|
||||
/// in the gating.
|
||||
final bool advancedMode;
|
||||
|
||||
@override
|
||||
State<NoteEditor> createState() => _NoteEditorState();
|
||||
}
|
||||
138
lib/ui/note_editor_wizard.dart
Normal file
138
lib/ui/note_editor_wizard.dart
Normal file
@ -0,0 +1,138 @@
|
||||
/// The two-step wizard shown before the guided stepper on an empty draft.
|
||||
///
|
||||
/// Split out of `note_editor.dart` for file size. It owns its own step index
|
||||
/// and pending choices — the editor only cares about the two values that come
|
||||
/// back out, so keeping that state here rather than in `_NoteEditorState` is
|
||||
/// both smaller and better scoped.
|
||||
///
|
||||
/// Priority and template are asked once, up front, because those choices only
|
||||
/// make sense before there is anything to guide.
|
||||
library;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:todo/data/note.dart';
|
||||
import 'package:todo/data/note_template.dart';
|
||||
|
||||
/// Asks for a priority, then a template, then hands both back via [onStart].
|
||||
class NoteEditorWizard extends StatefulWidget {
|
||||
/// Creates the wizard seeded with [initialPriority] and [initialTemplate].
|
||||
const NoteEditorWizard({
|
||||
required this.initialPriority,
|
||||
required this.initialTemplate,
|
||||
required this.onStart,
|
||||
required this.onCancel,
|
||||
super.key,
|
||||
});
|
||||
|
||||
/// Priority the first step opens on.
|
||||
final Priority initialPriority;
|
||||
|
||||
/// Template the second step opens on.
|
||||
final NoteTemplate initialTemplate;
|
||||
|
||||
/// Called with both choices when the user presses "Start".
|
||||
final void Function(Priority priority, NoteTemplate template) onStart;
|
||||
|
||||
/// Called when the user dismisses the wizard.
|
||||
final VoidCallback onCancel;
|
||||
|
||||
@override
|
||||
State<NoteEditorWizard> createState() => _NoteEditorWizardState();
|
||||
}
|
||||
|
||||
class _NoteEditorWizardState extends State<NoteEditorWizard> {
|
||||
late int _step = 0;
|
||||
late Priority _priority = widget.initialPriority;
|
||||
late NoteTemplate _template = widget.initialTemplate;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.close),
|
||||
tooltip: 'Cancel',
|
||||
onPressed: widget.onCancel,
|
||||
),
|
||||
Text('Step ${_step + 1} of 2', style: theme.textTheme.labelLarge),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
if (_step == 0) ..._priorityStep() else ..._templateStep(),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
List<Widget> _priorityStep() {
|
||||
return [
|
||||
DropdownButtonFormField<Priority>(
|
||||
initialValue: _priority,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Priority',
|
||||
isDense: true,
|
||||
border: OutlineInputBorder(),
|
||||
contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
),
|
||||
items: [
|
||||
for (final p in Priority.values)
|
||||
DropdownMenuItem(value: p, child: Text(p.label)),
|
||||
],
|
||||
onChanged: (p) {
|
||||
if (p != null) setState(() => _priority = p);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child: FilledButton(
|
||||
onPressed: () => setState(() => _step = 1),
|
||||
child: const Text('Next'),
|
||||
),
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
List<Widget> _templateStep() {
|
||||
final templates = NoteTemplate.all.where((t) => !t.isFreeform).toList();
|
||||
return [
|
||||
DropdownButtonFormField<String>(
|
||||
initialValue: _template.id,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Template',
|
||||
isDense: true,
|
||||
border: OutlineInputBorder(),
|
||||
contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
),
|
||||
items: [
|
||||
for (final t in templates)
|
||||
DropdownMenuItem(value: t.id, child: Text(t.label)),
|
||||
],
|
||||
onChanged: (id) {
|
||||
if (id == null) return;
|
||||
setState(() => _template = templates.firstWhere((t) => t.id == id));
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: () => setState(() => _step = 0),
|
||||
child: const Text('Back'),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
FilledButton(
|
||||
onPressed: () => widget.onStart(_priority, _template),
|
||||
child: const Text('Start'),
|
||||
),
|
||||
],
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
|
||||
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
|
||||
# In Windows, build-name is used as the major, minor, and patch parts
|
||||
# of the product and file versions while build-number is used as the build suffix.
|
||||
version: 1.0.11+1
|
||||
version: 1.0.12+1
|
||||
|
||||
environment:
|
||||
sdk: ^3.12.2
|
||||
|
||||
Loading…
Reference in New Issue
Block a user