The history of a note: see it after each change
A document keeps every change it applied, so it can show itself as it was after any of them. The history page reuses a widget of the note page, and adds a slider that moves through the changes.
The history of a note: see it after each changeSource โ
A Flutter note with a history page: a slider moves through the changes, the page shows the note as it was after each one, and says who made the change and when. The history is read-only.
- history_page.dart
- note_page.dart
- main.dart
- Install
import 'package:crdt_lf/crdt_lf.dart';
import 'package:crdt_lf_flutter/crdt_lf_flutter.dart';
import 'package:flutter/material.dart';
/// A page that shows the note of the document above it after each change.
class HistoryPage extends StatelessWidget {
/// Creates the history of the note.
const HistoryPage({super.key});
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('History')),
// Takes the history as it is now, and shows it to the widgets below.
body: CrdtHistoryScope(
// Built once: it reads the view, and rebuilds on every move.
child: const NotePreview(),
builder:
(context, session, preview) => Column(
children: [
Expanded(child: preview!),
HistoryControls(session: session),
],
),
),
);
}
}
/// The text of the note. Inside a history scope it reads the past.
class NotePreview extends StatelessWidget {
/// Creates the preview of the note.
const NotePreview({super.key});
Widget build(BuildContext context) {
// The same widget shows the live note outside a history scope.
return CrdtHandlerBuilder<CRDTFugueTextHandler>(
id: 'note',
builder:
(context, note) => SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Align(alignment: Alignment.topLeft, child: Text(note.value)),
),
);
}
}
/// Moves the cursor of [session], and says who made the change it stands on.
class HistoryControls extends StatelessWidget {
/// Creates the controls of [session].
const HistoryControls({required this.session, super.key});
/// The history to move through.
final HistorySession session;
Widget build(BuildContext context) {
return Material(
elevation: 4,
child: Padding(
padding: const EdgeInsets.all(8),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Change ${session.cursor} of ${session.length}'),
Text(describeChange(session)),
Row(
children: [
IconButton(
tooltip: 'Previous',
icon: const Icon(Icons.chevron_left),
onPressed: session.canPrevious ? session.previous : null,
),
Expanded(
child: Slider(
value: session.cursor.toDouble(),
// A slider needs a range: an empty history has none.
max: session.length == 0 ? 1 : session.length.toDouble(),
onChanged:
session.length == 0
? null
: (value) => session.jump(value.round()),
),
),
IconButton(
tooltip: 'Next',
icon: const Icon(Icons.chevron_right),
onPressed: session.canNext ? session.next : null,
),
],
),
],
),
),
);
}
}
/// Who made the change the cursor of [session] stands on, and when.
String describeChange(HistorySession session) {
// The view holds the changes up to the cursor.
final changes = session.document.exportChanges();
if (changes.isEmpty) {
return 'Before the first change';
}
final last = changes.last;
final author =
last.author == session.document.peerId
? 'You'
: last.author.id.substring(0, 8);
final time = last.id.hlc.asDateTime.toLocal();
final minute = time.minute.toString().padLeft(2, '0');
return '$author ยท ${time.hour}:$minute';
}
import 'dart:async';
import 'package:crdt_lf_flutter/crdt_lf_flutter.dart';
import 'package:flutter/material.dart';
import 'history_page.dart';
/// A page that edits the note of the document above it.
class NotePage extends StatelessWidget {
/// Creates the page of the note.
const NotePage({super.key});
void _openHistory(BuildContext context) {
// A new route is outside the provider: hand the document to it.
final document = context.crdtDocument;
unawaited(
Navigator.of(context).push(
MaterialPageRoute<void>(
builder:
(context) => CrdtProvider.value(
value: document,
child: const HistoryPage(),
),
),
),
);
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Note'),
actions: [
IconButton(
tooltip: 'History',
icon: const Icon(Icons.history),
onPressed: () => _openHistory(context),
),
],
),
body: CrdtTextFieldBuilder(
id: 'note',
builder:
(context, controller) => TextField(
controller: controller,
maxLines: null,
decoration: const InputDecoration(
hintText: 'Write here',
contentPadding: EdgeInsets.all(16),
),
),
),
);
}
}
import 'package:crdt_lf/crdt_lf.dart';
import 'package:crdt_lf_flutter/crdt_lf_flutter.dart';
import 'package:flutter/material.dart';
import 'note_page.dart';
void main() {
runApp(
MaterialApp(
title: 'Note',
home: CrdtProvider(
// In memory: the provider creates the document and disposes it.
create: (_) {
final document = CRDTDocument();
CRDTFugueTextHandler(document, 'note');
return document;
},
child: const NotePage(),
),
),
);
}
flutter pub add crdt_lf crdt_lf_flutter
Key points
CrdtHistoryScopetakes the history once, when it is built: an edit made later does not reach it. Open the page again to see it.- Below the scope, a widget that follows one handler reads the past with no change: the preview is a plain
CrdtHandlerBuilder. - The view is read-only. A text field below the scope throws: show the text with a widget that only reads.
- A new route is outside the provider of the page that pushes it: pass the document with
CrdtProvider.value. - Every change carries its author and its clock: the view exports the changes up to the cursor, and the last one is the change the cursor stands on.
- The history starts at the last snapshot. After a compaction, cursor
0is that snapshot, not an empty note.