diff --git a/objectbox/example/flutter/objectbox_demo/lib/main.dart b/objectbox/example/flutter/objectbox_demo/lib/main.dart index 4dd18dbea..978a49856 100644 --- a/objectbox/example/flutter/objectbox_demo/lib/main.dart +++ b/objectbox/example/flutter/objectbox_demo/lib/main.dart @@ -67,46 +67,72 @@ class _MyHomePageState extends State { } GestureDetector Function(BuildContext, int) _itemBuilder(List notes) => - (BuildContext context, int index) => GestureDetector( - onTap: () => objectbox.noteBox.remove(notes[index].id), - child: Row( - children: [ - Expanded( - child: Container( - decoration: const BoxDecoration( - border: - Border(bottom: BorderSide(color: Colors.black12))), - child: Padding( - padding: const EdgeInsets.symmetric( - vertical: 18.0, horizontal: 10.0), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - notes[index].text, - style: const TextStyle( - fontSize: 15.0, - ), - // Provide a Key for the integration test - key: Key('list_item_$index'), + (BuildContext context, int index) { + final note = notes[index]; + return GestureDetector( + onTap: () => objectbox.noteBox.remove(note.id), + child: Row( + children: [ + Expanded( + child: Container( + decoration: const BoxDecoration( + border: Border( + bottom: BorderSide(color: Colors.black12), + ), + ), + child: Padding( + padding: const EdgeInsets.symmetric( + vertical: 18.0, horizontal: 10.0), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + note.text, + style: const TextStyle( + fontSize: 15.0, ), - Padding( - padding: const EdgeInsets.only(top: 5.0), - child: Text( - 'Added on ${notes[index].dateFormat}', - style: const TextStyle( - fontSize: 12.0, + // Provide a Key for the integration test + key: Key('list_item_$index'), + ), + Wrap( + children: [ + for (final att in note.attachment) + Padding( + padding: const EdgeInsets.only(right: 8.0), + child: Text( + 'A: ${att.id}', + style: TextStyle(fontSize: 10), + ), ), + ], + ), + Padding( + padding: const EdgeInsets.only(top: 5.0), + child: Text( + 'Added on ${notes[index].dateFormat}', + style: const TextStyle( + fontSize: 12.0, ), ), - ], - ), + ), + ], ), ), ), - ], - ), - ); + ), + IconButton( + onPressed: () { + note.attachment.add( + Attachment('Hello'), + ); + objectbox.noteBox.put(note); + }, + icon: const Icon(Icons.attach_file), + ) + ], + ), + ); + }; @override Widget build(BuildContext context) => Scaffold( @@ -125,7 +151,8 @@ class _MyHomePageState extends State { padding: const EdgeInsets.symmetric(horizontal: 10.0), child: TextField( decoration: const InputDecoration( - hintText: 'Enter a new note'), + hintText: 'Enter a new note', + ), controller: _noteInputController, onSubmitted: (value) => _addNote(), // Provide a Key for the integration test @@ -152,13 +179,16 @@ class _MyHomePageState extends State { ), ), Expanded( - child: StreamBuilder>( - stream: _listController.stream, - builder: (context, snapshot) => ListView.builder( - shrinkWrap: true, - padding: const EdgeInsets.symmetric(horizontal: 20.0), - itemCount: snapshot.hasData ? snapshot.data!.length : 0, - itemBuilder: _itemBuilder(snapshot.data ?? [])))) + child: StreamBuilder>( + stream: _listController.stream, + builder: (context, snapshot) => ListView.builder( + shrinkWrap: true, + padding: const EdgeInsets.symmetric(horizontal: 20.0), + itemCount: snapshot.hasData ? snapshot.data!.length : 0, + itemBuilder: _itemBuilder(snapshot.data ?? []), + ), + ), + ) ]), // We need a separate submit button because flutter_driver integration // test doesn't support submitting a TextField using "enter" key. diff --git a/objectbox/example/flutter/objectbox_demo/lib/model.dart b/objectbox/example/flutter/objectbox_demo/lib/model.dart index dbd3061ad..7de7ed28b 100644 --- a/objectbox/example/flutter/objectbox_demo/lib/model.dart +++ b/objectbox/example/flutter/objectbox_demo/lib/model.dart @@ -1,3 +1,4 @@ +import 'package:equatable/equatable.dart'; import 'package:intl/intl.dart'; import 'package:objectbox/objectbox.dart'; @@ -6,7 +7,10 @@ import 'objectbox.g.dart'; // ignore_for_file: public_member_api_docs @Entity() -class Note { +class Note extends Equatable { + Note(this.text, {this.id = 0, this.comment, DateTime? date}) + : date = date ?? DateTime.now(); + int id; String text; @@ -15,8 +19,22 @@ class Note { /// Note: Stored in milliseconds without time zone info. DateTime date; - Note(this.text, {this.id = 0, this.comment, DateTime? date}) - : date = date ?? DateTime.now(); - String get dateFormat => DateFormat('dd.MM.yyyy hh:mm:ss').format(date); + + ToMany attachment = ToMany(); + + @override + List get props => [id, text, comment, date]; +} + +@Entity() +class Attachment extends Equatable { + Attachment(this.content); + + int id = 0; + + final String content; + + @override + List get props => [id, content]; } diff --git a/objectbox/example/flutter/objectbox_demo/lib/objectbox-model.json b/objectbox/example/flutter/objectbox_demo/lib/objectbox-model.json index 518886508..22d9a4431 100644 --- a/objectbox/example/flutter/objectbox_demo/lib/objectbox-model.json +++ b/objectbox/example/flutter/objectbox_demo/lib/objectbox-model.json @@ -30,12 +30,37 @@ "type": 10 } ], + "relations": [ + { + "id": "1:7144087601680027171", + "name": "attachment", + "targetId": "2:332880768092001438" + } + ] + }, + { + "id": "2:332880768092001438", + "lastPropertyId": "2:3461296757897168890", + "name": "Attachment", + "properties": [ + { + "id": "1:5876356419799926351", + "name": "id", + "type": 6, + "flags": 1 + }, + { + "id": "2:3461296757897168890", + "name": "content", + "type": 9 + } + ], "relations": [] } ], - "lastEntityId": "1:2802681814019499133", + "lastEntityId": "2:332880768092001438", "lastIndexId": "0:0", - "lastRelationId": "0:0", + "lastRelationId": "1:7144087601680027171", "lastSequenceId": "0:0", "modelVersion": 5, "modelVersionParserMinimum": 5, diff --git a/objectbox/example/flutter/objectbox_demo/lib/objectbox.dart b/objectbox/example/flutter/objectbox_demo/lib/objectbox.dart index 0eb25a480..540da5d68 100644 --- a/objectbox/example/flutter/objectbox_demo/lib/objectbox.dart +++ b/objectbox/example/flutter/objectbox_demo/lib/objectbox.dart @@ -18,7 +18,9 @@ class ObjectBox { noteBox = Box(store); final qBuilder = noteBox.query() - ..order(Note_.date, flags: Order.descending); + ..order(Note_.date, flags: Order.descending) + // Comment to see it working + ..linkMany(Note_.attachment); queryStream = qBuilder.watch(triggerImmediately: true); // Add some demo data if the box is empty. diff --git a/objectbox/example/flutter/objectbox_demo/pubspec.yaml b/objectbox/example/flutter/objectbox_demo/pubspec.yaml index 273e463d3..bab08f731 100644 --- a/objectbox/example/flutter/objectbox_demo/pubspec.yaml +++ b/objectbox/example/flutter/objectbox_demo/pubspec.yaml @@ -13,6 +13,7 @@ dependencies: intl: any objectbox: ^1.4.1 objectbox_flutter_libs: any + equatable: ^2.0.3 dev_dependencies: objectbox_generator: any