From 7abb060306b567a20c746b292ccfe71fa9b01c86 Mon Sep 17 00:00:00 2001 From: FahadFahim71 <45163265+FahadFahim71@users.noreply.github.com> Date: Tue, 6 Feb 2024 16:28:51 +0100 Subject: [PATCH] feat: add auth drop down field --- lib/flutter_registration.dart | 1 + lib/src/auth_screen.dart | 113 ++++++++++++++++-------------- lib/src/model/auth_drop_down.dart | 59 ++++++++++++++++ 3 files changed, 120 insertions(+), 53 deletions(-) create mode 100644 lib/src/model/auth_drop_down.dart diff --git a/lib/flutter_registration.dart b/lib/flutter_registration.dart index 8719e4f..9e7f910 100644 --- a/lib/flutter_registration.dart +++ b/lib/flutter_registration.dart @@ -11,6 +11,7 @@ export 'src/model/auth_field.dart'; export 'src/model/auth_step.dart'; export 'src/model/auth_text_field.dart'; export 'src/model/auth_bool_field.dart'; +export 'src/model/auth_drop_down.dart'; export 'src/registration_screen.dart'; export 'src/service/registration_repository.dart'; export 'package:flutter_input_library/flutter_input_library.dart' diff --git a/lib/src/auth_screen.dart b/lib/src/auth_screen.dart index 7b93d54..bc04cf9 100644 --- a/lib/src/auth_screen.dart +++ b/lib/src/auth_screen.dart @@ -149,71 +149,78 @@ class _AuthScreenState extends State { padding: const EdgeInsets.only( top: 15.0, bottom: 30.0, - left: 30.0, - right: 30.0, ), - child: Row( - mainAxisAlignment: - (widget.previousButtonBuilder != null && - previousButton == null) - ? MainAxisAlignment.spaceAround - : widget.steps.first != step - ? MainAxisAlignment.spaceBetween - : MainAxisAlignment.end, + child: Column( children: [ - if (widget.steps.first != step) - if (widget.previousButtonBuilder == null) ...[ - ElevatedButton( - onPressed: onPrevious, - child: Row( - children: [ - const Icon( - Icons.arrow_back, - size: 18, + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + if (widget.steps.first != step) + if (widget.previousButtonBuilder == null) ...[ + ElevatedButton( + onPressed: onPrevious, + child: Row( + children: [ + const Icon( + Icons.arrow_back, + size: 18, + ), + Padding( + padding: + const EdgeInsets.only(left: 4.0), + child: Text(widget.previousBtnTitle), + ), + ], ), - Padding( - padding: const EdgeInsets.only(left: 4.0), - child: Text(widget.previousBtnTitle), - ), - ], - ), - ), - ] else if (previousButton != null) ...[ - previousButton - ], - widget.nextButtonBuilder?.call( - () async { - await onNext(step); - }, - widget.steps.last == step - ? widget.submitBtnTitle - : widget.nextBtnTitle, - ) ?? - ElevatedButton( - onPressed: () async { - await onNext(step); - }, - child: Row( - children: [ - Text( + ), + ] else if (previousButton != null) ...[ + Padding( + padding: const EdgeInsets.all(8.0), + child: previousButton, + ) + ], + Padding( + padding: const EdgeInsets.all(8.0), + child: widget.nextButtonBuilder?.call( + () async { + await onNext(step); + }, widget.steps.last == step ? widget.submitBtnTitle : widget.nextBtnTitle, - ), - const Padding( - padding: EdgeInsets.only(left: 4.0), - child: Icon( - Icons.arrow_forward, - size: 18, + ) ?? + ElevatedButton( + onPressed: () async { + await onNext(step); + }, + child: Row( + children: [ + Text( + widget.steps.last == step + ? widget.submitBtnTitle + : widget.nextBtnTitle, + ), + const Padding( + padding: EdgeInsets.only(left: 4.0), + child: Icon( + Icons.arrow_forward, + size: 18, + ), + ), + ], ), ), - ], - ), ), + ], + ), + if (widget.loginButton != null) + Padding( + padding: const EdgeInsets.only(top: 20.0), + child: widget.loginButton!, + ), ], ), ), - if (widget.loginButton != null) widget.loginButton!, ], ), ], diff --git a/lib/src/model/auth_drop_down.dart b/lib/src/model/auth_drop_down.dart new file mode 100644 index 0000000..80c87da --- /dev/null +++ b/lib/src/model/auth_drop_down.dart @@ -0,0 +1,59 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_registration/flutter_registration.dart'; + +class AuthDropdownField extends AuthField { + AuthDropdownField({ + required super.name, + required this.items, + required this.onChanged, + this.dropdownDecoration, + this.padding = const EdgeInsets.all(8.0), + this.textStyle, + this.icon = const Icon(Icons.keyboard_arrow_down), + required super.value, + }) { + selectedValue = value ?? items.first; + } + + final List items; + final Function(String?) onChanged; + String? selectedValue; + final InputDecoration? dropdownDecoration; + final EdgeInsets padding; + final TextStyle? textStyle; + final Icon icon; + + @override + Widget build() { + return Padding( + padding: padding, + child: DropdownButtonFormField( + icon: icon, + style: textStyle, + value: selectedValue, + decoration: dropdownDecoration, + items: items.map((String value) { + return DropdownMenuItem( + value: value, + child: Text(value), + ); + }).toList(), + onChanged: (newValue) { + selectedValue = newValue; + onChanged(newValue); + }, + validator: (value) { + if (validators.isNotEmpty) { + for (var validator in validators) { + var output = validator(value); + if (output != null) { + return output; + } + } + } + return null; + }, + ), + ); + } +}