From b58b877bb0c3706c2613449f1617e9a39d331f9c Mon Sep 17 00:00:00 2001 From: Brandon Presley Date: Thu, 13 Apr 2023 17:23:26 +1200 Subject: [PATCH] Add search to settings page --- lib/home_page.dart | 2 +- lib/settings_page.dart | 160 ++++++++++++++++++++++------------------- 2 files changed, 86 insertions(+), 76 deletions(-) diff --git a/lib/home_page.dart b/lib/home_page.dart index b2c5422..5344e56 100644 --- a/lib/home_page.dart +++ b/lib/home_page.dart @@ -42,7 +42,7 @@ class RootPage extends State { Widget getBody() { switch (routes[selected]['title']) { case 'Settings': - return const SettingsPage(); + return SettingsPage(search: search); case 'Timer': return const TimerPage(); case 'Workouts': diff --git a/lib/settings_page.dart b/lib/settings_page.dart index d17d26d..fb8d3e3 100644 --- a/lib/settings_page.dart +++ b/lib/settings_page.dart @@ -6,20 +6,24 @@ import 'package:fmassive/sound_picker.dart'; import 'package:moor/moor.dart'; class SettingsPage extends StatelessWidget { - const SettingsPage({super.key}); + const SettingsPage({super.key, required this.search}); + + final String search; @override Widget build(BuildContext context) { - return const Scaffold( + return Scaffold( body: Center( - child: _SettingsPage(), + child: _SettingsPage(search: search), ), ); } } class _SettingsPage extends StatefulWidget { - const _SettingsPage({Key? key}) : super(key: key); + const _SettingsPage({Key? key, required this.search}) : super(key: key); + + final String search; @override createState() => _SettingsPageState(); @@ -28,8 +32,6 @@ class _SettingsPage extends StatefulWidget { class _SettingsPageState extends State<_SettingsPage> { late Stream stream; - final TextEditingController searchController = TextEditingController(); - @override void initState() { super.initState(); @@ -40,83 +42,91 @@ class _SettingsPageState extends State<_SettingsPage> { Widget build(BuildContext context) { return Scaffold( body: StreamBuilder( - stream: stream, - builder: (context, snapshot) { - final settings = snapshot.data; + stream: stream, + builder: (context, snapshot) { + final settings = snapshot.data; - if (settings == null) - return const Center(child: CircularProgressIndicator()); + if (settings == null) + return const Center(child: CircularProgressIndicator()); - return SingleChildScrollView( - child: material.Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - SwitchListTile( - title: const Text('Alarm'), - value: settings.alarm, - onChanged: (value) { - db - .update(db.settings) - .write(SettingsCompanion(alarm: Value(value))); - }, - ), - SwitchListTile( - title: const Text('Vibrate'), - value: settings.vibrate, - onChanged: (value) { - db - .update(db.settings) - .write(SettingsCompanion(vibrate: Value(value))); - }, - ), - SwitchListTile( - title: const Text('Notify'), - value: settings.notify, - onChanged: (value) { - db - .update(db.settings) - .write(SettingsCompanion(notify: Value(value))); - }, - ), - SwitchListTile( - title: const Text('Images'), - value: settings.images, - onChanged: (value) { - db - .update(db.settings) - .write(SettingsCompanion(images: Value(value))); - }, - ), - SwitchListTile( - title: const Text('Show Unit'), - value: settings.showUnit, - onChanged: (value) { - db - .update(db.settings) - .write(SettingsCompanion(showUnit: Value(value))); - }, - ), - SwitchListTile( - title: const Text('Steps'), - value: settings.steps, - onChanged: (value) { - db - .update(db.settings) - .write(SettingsCompanion(steps: Value(value))); - }, - ), - Center( - child: SoundPicker( + final filteredItems = [ + {'title': 'Alarm', 'value': settings.alarm}, + {'title': 'Vibrate', 'value': settings.vibrate}, + {'title': 'Notify', 'value': settings.notify}, + {'title': 'Images', 'value': settings.images}, + {'title': 'Show Unit', 'value': settings.showUnit}, + {'title': 'Steps', 'value': settings.steps}, + {'title': 'Sound', 'value': settings.sound}, + ] + .where((item) => (item['title'] as String) + .toLowerCase() + .contains(widget.search.toLowerCase())) + .toList(); + + return material.Column( + children: [ + Expanded( + child: ListView.builder( + itemCount: filteredItems.length, + itemBuilder: (context, index) { + final item = filteredItems[index]; + + if (item['title'] == 'Sound') { + return Center( + child: SoundPicker( path: settings.sound, onSelect: (path) { db .update(db.settings) .write(SettingsCompanion(sound: Value(path))); - })), - ], + }, + ), + ); + } + + return SwitchListTile( + title: Text(item['title'].toString()), + value: item['value'] as bool, + onChanged: (value) { + switch (item['title']) { + case 'Alarm': + db + .update(db.settings) + .write(SettingsCompanion(alarm: Value(value))); + break; + case 'Vibrate': + db.update(db.settings).write( + SettingsCompanion(vibrate: Value(value))); + break; + case 'Notify': + db + .update(db.settings) + .write(SettingsCompanion(notify: Value(value))); + break; + case 'Images': + db + .update(db.settings) + .write(SettingsCompanion(images: Value(value))); + break; + case 'Show Unit': + db.update(db.settings).write( + SettingsCompanion(showUnit: Value(value))); + break; + case 'Steps': + db + .update(db.settings) + .write(SettingsCompanion(steps: Value(value))); + break; + } + }, + ); + }, + ), ), - ); - }), + ], + ); + }, + ), ); } }