From 4778501be8959f53444ed64561b9d7325759d230 Mon Sep 17 00:00:00 2001 From: Codel1417 Date: Sat, 1 Jun 2024 16:22:53 -0400 Subject: [PATCH] Re-work trigger select to use bottom sheet --- lib/Frontend/Widgets/trigger_select.dart | 77 ------------------------ lib/Frontend/pages/triggers.dart | 56 ++++++++++++----- pubspec.lock | 13 ++-- pubspec.yaml | 4 +- 4 files changed, 52 insertions(+), 98 deletions(-) delete mode 100644 lib/Frontend/Widgets/trigger_select.dart diff --git a/lib/Frontend/Widgets/trigger_select.dart b/lib/Frontend/Widgets/trigger_select.dart deleted file mode 100644 index 19c0d1e2..00000000 --- a/lib/Frontend/Widgets/trigger_select.dart +++ /dev/null @@ -1,77 +0,0 @@ -import 'package:back_button_interceptor/back_button_interceptor.dart'; -import 'package:flutter/material.dart'; -import 'package:flutter_riverpod/flutter_riverpod.dart'; - -import '../../Backend/sensors.dart'; -import '../translation_string_definitions.dart'; - -class TriggerSelect extends ConsumerStatefulWidget { - const TriggerSelect({super.key}); - - @override - ConsumerState createState() => _TriggerSelectState(); -} - -class _TriggerSelectState extends ConsumerState { - TriggerDefinition? triggerDefinition; - - @override - void initState() { - super.initState(); - BackButtonInterceptor.add(myInterceptor); - } - - @override - void dispose() { - BackButtonInterceptor.remove(myInterceptor); - super.dispose(); - } - - bool myInterceptor(bool stopDefaultButtonEvent, RouteInfo info) { - Navigator.pop(context); - return true; - } - - @override - Widget build(BuildContext context) { - return AlertDialog( - title: Text(triggersSelectLabel()), - content: SingleChildScrollView( - child: Column( - mainAxisSize: MainAxisSize.min, - children: ref - .watch(triggerDefinitionListProvider) - .map((TriggerDefinition e) => ListTile( - title: Text(e.name), - leading: Radio( - value: e, - groupValue: triggerDefinition, - onChanged: (TriggerDefinition? value) { - setState( - () { - triggerDefinition = value; - }, - ); - }, - ), - trailing: e.icon, - subtitle: Text(e.description), - )) - .toList(), - ), - ), - actions: [ - TextButton( - onPressed: () => Navigator.pop(context, null), - child: Text(cancel()), - ), - TextButton( - onPressed: () { - Navigator.pop(context, triggerDefinition); - }, - child: Text(ok()), - ), - ], - ); - } -} diff --git a/lib/Frontend/pages/triggers.dart b/lib/Frontend/pages/triggers.dart index fbacd312..b8b9cca0 100644 --- a/lib/Frontend/pages/triggers.dart +++ b/lib/Frontend/pages/triggers.dart @@ -1,11 +1,11 @@ import 'package:back_button_interceptor/back_button_interceptor.dart'; +import 'package:choice/choice.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:multi_value_listenable_builder/multi_value_listenable_builder.dart'; import 'package:tail_app/Backend/Definitions/Action/base_action.dart'; import 'package:tail_app/Backend/Definitions/Device/device_definition.dart'; import 'package:tail_app/Backend/sensors.dart'; -import 'package:tail_app/Frontend/Widgets/trigger_select.dart'; import 'package:uuid/uuid.dart'; import '../../constants.dart'; @@ -27,20 +27,42 @@ class _TriggersState extends ConsumerState { Widget build(BuildContext context) { final List triggersList = ref.watch(triggerListProvider); return Scaffold( - floatingActionButton: FloatingActionButton.extended( - icon: const Icon(Icons.add), - onPressed: () { - showDialog( - context: context, - useRootNavigator: true, - builder: (BuildContext context) { - plausible.event(page: "/Triggers/AddTrigger"); - return const TriggerSelect(); + floatingActionButton: Builder( + builder: (context) { + List triggerDefinitions = ref.watch(triggerDefinitionListProvider.notifier).get(); + return PromptedChoice.single( + itemCount: triggerDefinitions.length, + itemBuilder: (ChoiceController state, int index) { + TriggerDefinition triggerDefinition = triggerDefinitions[index]; + return RadioListTile( + value: triggerDefinition, + groupValue: state.single, + onChanged: (value) { + state.select(triggerDefinition); + }, + secondary: triggerDefinition.icon, + subtitle: ChoiceText( + triggerDefinition.description, + highlight: state.search?.value, + ), + title: ChoiceText( + triggerDefinition.name, + highlight: state.search?.value, + ), + ); }, - ).then( - (TriggerDefinition? value) { + promptDelegate: ChoicePrompt.delegateBottomSheet(useRootNavigator: true, enableDrag: true, maxHeightFactor: 0.8), + modalHeaderBuilder: ChoiceModal.createHeader( + automaticallyImplyLeading: true, + actionsBuilder: [ + ChoiceModal.createConfirmButton(), + ChoiceModal.createSpacer(width: 10), + ], + ), + title: triggersSelectLabel(), + confirmation: true, + onChanged: (value) { if (value != null) { - // The user selected a Trigger Definition setState( () { Trigger trigger = Trigger.trigDef(value, const Uuid().v4()); @@ -50,9 +72,15 @@ class _TriggersState extends ConsumerState { ); } }, + anchorBuilder: (state, openModal) { + return FloatingActionButton.extended( + icon: const Icon(Icons.add), + label: Text(triggersAdd()), + onPressed: openModal, + ); + }, ); }, - label: Text(triggersAdd()), ), body: SingleChildScrollView( physics: const AlwaysScrollableScrollPhysics(), diff --git a/pubspec.lock b/pubspec.lock index 2b989dc2..42cec940 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -58,10 +58,10 @@ packages: dependency: transitive description: name: archive - sha256: "6bd38d335f0954f5fad9c79e614604fbf03a0e5b975923dd001b6ea965ef5b4b" + sha256: cb6a278ef2dbb298455e1a713bda08524a175630ec643a242c399c932a0a1f7d url: "https://pub.dev" source: hosted - version: "3.6.0" + version: "3.6.1" args: dependency: transitive description: @@ -217,10 +217,11 @@ packages: choice: dependency: "direct main" description: - name: choice - sha256: "52d07065e8056beba5b26cff7786134cbfa24927b1f5bf60a05d50058597b2d9" - url: "https://pub.dev" - source: hosted + path: "." + ref: HEAD + resolved-ref: ba4dc8b3579620e3fac0944dcab97f9b22e7c3eb + url: "https://github.com/Codel1417/flutter_choice" + source: git version: "2.3.2" circular_buffer: dependency: "direct main" diff --git a/pubspec.yaml b/pubspec.yaml index 897a8de2..ba15a467 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -78,7 +78,9 @@ dependencies: flutter_native_splash: ^2.4.0 lottie_native: ^0.1.3 duration: ^3.0.13 - choice: ^2.3.2 # used for device choice ui + choice: + git: + url: https://github.com/Codel1417/flutter_choice # Dio HTTP dio: ^5.4.3+1