mirror of
				https://github.com/ImranR98/Obtainium.git
				synced 2025-10-31 13:33:28 +01:00 
			
		
		
		
	Added dropdown support to generated form
This commit is contained in:
		| @@ -1,6 +1,5 @@ | ||||
| import 'dart:convert'; | ||||
|  | ||||
| import 'package:html/parser.dart'; | ||||
| import 'package:http/http.dart'; | ||||
| import 'package:obtainium/custom_errors.dart'; | ||||
| import 'package:obtainium/providers/source_provider.dart'; | ||||
|   | ||||
| @@ -1,4 +1,3 @@ | ||||
| import 'package:html/parser.dart'; | ||||
| import 'package:http/http.dart'; | ||||
| import 'package:obtainium/app_sources/fdroid.dart'; | ||||
| import 'package:obtainium/custom_errors.dart'; | ||||
|   | ||||
| @@ -2,7 +2,8 @@ import 'package:flutter/material.dart'; | ||||
|  | ||||
| enum FormItemType { string, bool } | ||||
|  | ||||
| typedef OnValueChanges = void Function(List<String> values, bool valid); | ||||
| typedef OnValueChanges = void Function( | ||||
|     List<String> values, bool valid, bool isBuilding); | ||||
|  | ||||
| class GeneratedFormItem { | ||||
|   late String label; | ||||
| @@ -13,6 +14,7 @@ class GeneratedFormItem { | ||||
|   late String id; | ||||
|   late List<Widget> belowWidgets; | ||||
|   late String? hint; | ||||
|   late List<String>? opts; | ||||
|  | ||||
|   GeneratedFormItem( | ||||
|       {this.label = 'Input', | ||||
| @@ -22,7 +24,8 @@ class GeneratedFormItem { | ||||
|       this.additionalValidators = const [], | ||||
|       this.id = 'input', | ||||
|       this.belowWidgets = const [], | ||||
|       this.hint}); | ||||
|       this.hint, | ||||
|       this.opts}); | ||||
| } | ||||
|  | ||||
| class GeneratedForm extends StatefulWidget { | ||||
| @@ -47,7 +50,7 @@ class _GeneratedFormState extends State<GeneratedForm> { | ||||
|   List<List<Widget>> rows = []; | ||||
|  | ||||
|   // If any value changes, call this to update the parent with value and validity | ||||
|   void someValueChanged() { | ||||
|   void someValueChanged({bool isBuilding = false}) { | ||||
|     List<String> returnValues = []; | ||||
|     var valid = true; | ||||
|     for (int r = 0; r < values.length; r++) { | ||||
| @@ -62,7 +65,7 @@ class _GeneratedFormState extends State<GeneratedForm> { | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|     widget.onValueChanges(returnValues, valid); | ||||
|     widget.onValueChanges(returnValues, valid, isBuilding); | ||||
|   } | ||||
|  | ||||
|   @override | ||||
| @@ -75,14 +78,16 @@ class _GeneratedFormState extends State<GeneratedForm> { | ||||
|         .map((row) => row.map((e) { | ||||
|               return j < widget.defaultValues.length | ||||
|                   ? widget.defaultValues[j++] | ||||
|                   : ''; | ||||
|                   : e.opts != null | ||||
|                       ? e.opts!.first | ||||
|                       : ''; | ||||
|             }).toList()) | ||||
|         .toList(); | ||||
|  | ||||
|     // Dynamically create form inputs | ||||
|     formInputs = widget.items.asMap().entries.map((row) { | ||||
|       return row.value.asMap().entries.map((e) { | ||||
|         if (e.value.type == FormItemType.string) { | ||||
|         if (e.value.type == FormItemType.string && e.value.opts == null) { | ||||
|           final formFieldKey = GlobalKey<FormFieldState>(); | ||||
|           return TextFormField( | ||||
|             key: formFieldKey, | ||||
| @@ -112,11 +117,29 @@ class _GeneratedFormState extends State<GeneratedForm> { | ||||
|               return null; | ||||
|             }, | ||||
|           ); | ||||
|         } else if (e.value.type == FormItemType.string && | ||||
|             e.value.opts != null) { | ||||
|           if (e.value.opts!.isEmpty) { | ||||
|             return const Text('ERROR: DROPDOWN MUST HAVE AT LEAST ONE OPT.'); | ||||
|           } | ||||
|           return DropdownButtonFormField( | ||||
|               decoration: const InputDecoration(labelText: 'Colour'), | ||||
|               value: values[row.key][e.key], | ||||
|               items: e.value.opts! | ||||
|                   .map((e) => DropdownMenuItem(value: e, child: Text(e))) | ||||
|                   .toList(), | ||||
|               onChanged: (value) { | ||||
|                 setState(() { | ||||
|                   values[row.key][e.key] = value ?? e.value.opts!.first; | ||||
|                   someValueChanged(); | ||||
|                 }); | ||||
|               }); | ||||
|         } else { | ||||
|           return Container(); // Some input types added in build | ||||
|         } | ||||
|       }).toList(); | ||||
|     }).toList(); | ||||
|     someValueChanged(isBuilding: true); | ||||
|   } | ||||
|  | ||||
|   @override | ||||
|   | ||||
| @@ -46,11 +46,16 @@ class _GeneratedFormModalState extends State<GeneratedFormModal> { | ||||
|           ), | ||||
|         GeneratedForm( | ||||
|             items: widget.items, | ||||
|             onValueChanges: (values, valid) { | ||||
|               setState(() { | ||||
|             onValueChanges: (values, valid, isBuilding) { | ||||
|               if (isBuilding) { | ||||
|                 this.values = values; | ||||
|                 this.valid = valid; | ||||
|               }); | ||||
|               } else { | ||||
|                 setState(() { | ||||
|                   this.values = values; | ||||
|                   this.valid = valid; | ||||
|                 }); | ||||
|               } | ||||
|             }, | ||||
|             defaultValues: widget.defaultValues) | ||||
|       ]), | ||||
|   | ||||
| @@ -66,7 +66,7 @@ class _AddAppPageState extends State<AddAppPage> { | ||||
|                                           ]) | ||||
|                                     ] | ||||
|                                   ], | ||||
|                                   onValueChanges: (values, valid) { | ||||
|                                   onValueChanges: (values, valid, isBuilding) { | ||||
|                                     setState(() { | ||||
|                                       userInput = values[0]; | ||||
|                                       var source = valid | ||||
| @@ -179,7 +179,7 @@ class _AddAppPageState extends State<AddAppPage> { | ||||
|                                 .additionalDataFormItems.isNotEmpty) | ||||
|                               GeneratedForm( | ||||
|                                   items: pickedSource!.additionalDataFormItems, | ||||
|                                   onValueChanges: (values, valid) { | ||||
|                                   onValueChanges: (values, valid, isBuilding) { | ||||
|                                     setState(() { | ||||
|                                       additionalData = values; | ||||
|                                       validAdditionalData = valid; | ||||
|   | ||||
| @@ -142,7 +142,7 @@ class _SettingsPageState extends State<SettingsPage> { | ||||
|       if (e.moreSourceSettingsFormItems.isNotEmpty) { | ||||
|         return GeneratedForm( | ||||
|             items: e.moreSourceSettingsFormItems.map((e) => [e]).toList(), | ||||
|             onValueChanges: (values, valid) { | ||||
|             onValueChanges: (values, valid, isBuilding) { | ||||
|               if (valid) { | ||||
|                 for (var i = 0; i < values.length; i++) { | ||||
|                   settingsProvider.setSettingString( | ||||
|   | ||||
		Reference in New Issue
	
	Block a user