A todo list with containers: merged per field
Each todo is a small tree: a text and a done flag, in a list that peers can
reorder at the same time. One peer can tick a todo while another edits its
text, and both edits survive. This is model B of
Which handler do you need?.
The same list as an aggregate is the aggregate recipe.
A todo list with containers: merged per fieldSource ↗
A Flutter todo list built with container handlers: each todo is a small map of handlers, a collaborative text and a done flag. Two peers can edit the text and check the same todo at the same time, or move it, and both edits are kept.
- todos.dart
- main.dart
- todo_page.dart
- Install
import 'package:crdt_lf/crdt_lf.dart';
/// The kind of the `done` flag. A generic handler needs a tag: write it once.
final doneKind = CRDTRegisterHandler.spec<bool>('todo.done');
/// The todo list of [document]: a movable list with one map per todo.
CRDTMovableListRefHandler todosOf(CRDTDocument document) {
// The kinds this peer must rebuild when the todos of another peer arrive.
document
..register(CRDTMapRefHandler.spec)
..register(CRDTFugueTextHandler.spec)
..register(doneKind);
return document.handler(CRDTMovableListRefHandler.spec, 'todos');
}
/// Adds a todo that reads [text] at the end of [todos].
CRDTMapRefHandler addTodo(CRDTMovableListRefHandler todos, {String text = ''}) {
// One transaction: the other peers get the todo with both its fields.
return todos.doc.runInTransaction(() {
final todo = todos.insertChild(
todos.childRefs().length,
CRDTMapRefHandler.spec,
);
final title = todo.child('text', CRDTFugueTextHandler.spec);
if (text.isNotEmpty) {
title.insert(0, text);
}
todo.child('done', doneKind).set(false);
return todo;
});
}
/// The fields of one todo, each merged on its own.
extension TodoFields on CRDTMapRefHandler {
/// The text of the todo: merged character by character.
CRDTFugueTextHandler get text => child('text', CRDTFugueTextHandler.spec);
/// Whether the todo is done: the last write wins.
CRDTRegisterHandler<bool> get done => child('done', doneKind);
}
import 'package:crdt_lf/crdt_lf.dart';
import 'package:crdt_lf_flutter/crdt_lf_flutter.dart';
import 'package:flutter/material.dart';
import 'todo_page.dart';
import 'todos.dart';
void main() {
runApp(
MaterialApp(
title: 'Todos',
home: CrdtProvider(
// In memory: the provider creates the document and disposes it.
create: (_) {
final document = CRDTDocument();
todosOf(document); // the widgets read the list under 'todos'
return document;
},
child: const TodoPage(),
),
),
);
}
import 'package:crdt_lf/crdt_lf.dart';
import 'package:crdt_lf_flutter/crdt_lf_flutter.dart';
import 'package:flutter/material.dart';
import 'todos.dart';
/// A page that shows the todos of the document above it.
class TodoPage extends StatelessWidget {
/// Creates the page of the todos.
const TodoPage({super.key});
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Todos')),
floatingActionButton: FloatingActionButton(
tooltip: 'New todo',
// An action needs no rebuild: read the handler once.
onPressed:
() => addTodo(
context.crdtHandler<CRDTMovableListRefHandler>('todos'),
),
child: const Icon(Icons.add),
),
// Rebuilds when a todo is added, removed or moved. An edit of one item
// rebuilds only what follows it in its tile.
body: CrdtHandlerBuilder<CRDTMovableListRefHandler>(
id: 'todos',
builder: (context, todos) {
return ReorderableListView(
// `to` is the place after the move, as `move` counts it.
onReorderItem: todos.move,
children: [
// One tile per reference, so a tile index is the list index.
for (final (index, ref) in todos.childRefs().indexed)
switch (todos.doc.resolveHandler(ref)) {
final CRDTMapRefHandler todo => TodoTile(
key: ValueKey(ref.id),
todo: todo,
onDelete: () => todos.removeChildAt(index),
),
// A kind this peer does not know, from a newer peer.
_ => ListTile(
key: ValueKey(ref.id),
title: const Text('Unknown item'),
),
},
],
);
},
),
);
}
}
/// One todo: a check box and a collaborative text field.
class TodoTile extends StatelessWidget {
/// Creates the tile of [todo].
const TodoTile({required this.todo, required this.onDelete, super.key});
/// The todo to show.
final CRDTMapRefHandler todo;
/// Deletes the todo.
final VoidCallback onDelete;
Widget build(BuildContext context) {
return ListTile(
// Follows the flag alone: another peer that checks the todo rebuilds
// this check box, and nothing else.
leading: CrdtHandlerBuilder<CRDTRegisterHandler<bool>>(
id: todo.done.id,
builder:
(context, done) => Checkbox(
value: done.value ?? false,
onChanged: (value) => done.set(value ?? false),
),
),
title: CrdtTextFieldBuilder(
id: todo.text.id,
builder:
(context, controller) => TextField(
controller: controller,
decoration: const InputDecoration(hintText: 'What to do'),
),
),
trailing: IconButton(
tooltip: 'Delete',
icon: const Icon(Icons.delete_outline),
onPressed: onDelete,
),
);
}
}
flutter pub add crdt_lf crdt_lf_flutter
Key points
- Each todo is a
CRDTMapRefHandlerwith a text and a register: a change to one field leaves the other field as it is. - A
CRDTMovableListRefHandlerkeeps the identity of a todo when two peers move it at the same time: it is never duplicated. - Register the kinds of the children (
register), so a peer rebuilds the todos it receives from another peer. - The builder of the list follows the list alone: it rebuilds when a todo is added, removed or moved. Each tile follows its own handlers, so an edit of one todo rebuilds nothing else.
- The page shows one tile per reference of the list, so a tile index is the index
removeChildAtandmoveexpect. ReorderableListView.onReorderItemis recent in Flutter. On an older Flutter, useonReorderand lowertoby one when it is afterfrom.- The document lives in memory. To keep it on the device, open it from a storage backend, as in the flutter-note recipe and on the Persistence page.