CRDT LF Flutter
Flutter reactivity for crdt_lf.
Rebuild your widgets when the CRDT state changes, for the whole document or for
one handler, and bind text fields and editors to collaborative text. Built on
provider.
π The full Flutter guide β widgets, delta widgets, text fields, editors, presence β is on mattiapispisa.it/crdt.
Features
- π³
CrdtProvider: provides aCRDTDocumentto the subtree - π
CrdtBuilder/CrdtSelector: rebuild on every document update, or on a slice - π―
CrdtHandlerBuilder/Selector/Listener: rebuild only when one handler changes, nested ones included - π
CrdtHandlerDeltaBuilder/Listener: follow what each change did, for long lists andAnimatedList - βοΈ
CrdtTextFieldBuilder: aTextEditingControllerbound to a text handler, IME included, zero rebuilds - π
±οΈ
CrdtRichTextFieldBuilder: inline formats in aTextField, with undo - π§©
CrdtEditorBinder: the sync loop for any editor (used bycrdt_lf_quill) - π₯
CrdtTextCursorsOverlay/CrdtAwarenessCursorsOverlay: the other peers' carets and pointers - β β¦and more: everything else is in the documentation
See it live
Greyhound Markdown is built with crdt_lf and crdt_lf_flutter. Open it on
two devices, join the same room and edit together.
The example app
shows every widget on one screen, with a β³ rebuilt ΓN badge on each region:
Quick start
flutter pub add crdt_lf crdt_lf_flutter
Provide the document to the tree:
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(),
),
),
);
}
Rebuild on one handler, and read it once for an action:
@override
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'),
),
},
],
);
},
),
);
}
onReorderItem is recent in Flutter. On an older Flutter, use onReorder
and lower to by one when it is after from.
The whole app (todosOf, addTodo, TodoTile), tested in CI, is the
todo list recipe. For a text field, see the
note recipe.
Documentation
- Flutter overview: the provider and the rebuild widgets
- Delta widgets
- Text fields: remote cursors, rich text
- Editors and presence
- Benchmarks
Roadmap
πΊοΈ The project board shows what is done, what is in progress and what comes next, for this package and the rest of the workspace. Missing something? Open an issue: requests go on the board too.
Apps
- greyhound_markdown β Real-time collaborative markdown editor built on crdt_lf
Packages
Other bricks of the crdt "system" are:
Libraries
- crdt_lf_flutter
- Flutter reactivity for crdt_lf package.