mirror of
https://github.com/Iconica-Development/flutter_address_form.git
synced 2025-05-19 10:43:45 +02:00
Added validator to AddressController
This commit is contained in:
parent
affa510a5c
commit
3ba62681c9
2 changed files with 52 additions and 44 deletions
|
@ -87,9 +87,10 @@ class AddressFormExample extends StatelessWidget {
|
|||
onSubmit: (value) => value, controller: _addressController),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
_addressController.validate;
|
||||
_addressController.validate();
|
||||
},
|
||||
child: Text('Test'))
|
||||
child: Text('Test'),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import 'dart:async';
|
||||
import 'dart:ffi';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
|
@ -48,9 +47,11 @@ class AddressForm extends StatefulWidget {
|
|||
class _AddressFormState extends State<AddressForm> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
return Flexible(
|
||||
child: Column(
|
||||
children: [
|
||||
AddressFormTextField(
|
||||
validator: widget._addressController.zipCodeValidator,
|
||||
controller: widget._addressController._zipcodeController,
|
||||
fieldDecoration: widget.zipCodeDecoration,
|
||||
),
|
||||
|
@ -58,10 +59,12 @@ class _AddressFormState extends State<AddressForm> {
|
|||
child: Row(
|
||||
children: [
|
||||
AddressFormTextField(
|
||||
validator: widget._addressController.housenumberValidator,
|
||||
controller: widget._addressController._housenumberController,
|
||||
fieldDecoration: widget.housenumberDecoration,
|
||||
),
|
||||
AddressFormTextField(
|
||||
validator: widget._addressController.suffixValidator,
|
||||
controller: widget._addressController._suffixController,
|
||||
fieldDecoration: widget.suffixDecoration,
|
||||
),
|
||||
|
@ -69,37 +72,43 @@ class _AddressFormState extends State<AddressForm> {
|
|||
),
|
||||
),
|
||||
AddressFormTextField(
|
||||
validator: widget._addressController.streetValidator,
|
||||
controller: widget._addressController._streetController,
|
||||
fieldDecoration: widget.streetDecoration,
|
||||
),
|
||||
AddressFormTextField(
|
||||
validator: widget._addressController.cityValidator,
|
||||
controller: widget._addressController._cityController,
|
||||
fieldDecoration: widget.cityDecoration,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class AddressFormTextField extends StatelessWidget {
|
||||
AddressFormTextField({
|
||||
super.key,
|
||||
AddressFormTextField(
|
||||
{super.key,
|
||||
required this.fieldDecoration,
|
||||
required this.controller,
|
||||
}) {
|
||||
_addressFieldDecoration = fieldDecoration;
|
||||
}
|
||||
required this.validator});
|
||||
|
||||
final TextEditingController controller;
|
||||
final InputDecoration fieldDecoration;
|
||||
final String? Function(String) validator;
|
||||
|
||||
late final InputDecoration _addressFieldDecoration;
|
||||
late InputDecoration _addressFieldDecoration;
|
||||
|
||||
String? get _errorText => validator(controller.value.text);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ValueListenableBuilder<TextEditingValue>(
|
||||
valueListenable: controller,
|
||||
builder: (context, value, _) {
|
||||
_addressFieldDecoration =
|
||||
fieldDecoration.copyWith(errorText: _errorText);
|
||||
return Flexible(
|
||||
child: Container(
|
||||
margin: const EdgeInsets.all(10),
|
||||
|
@ -165,9 +174,7 @@ class AddressController extends ChangeNotifier {
|
|||
|
||||
AddressModel get model => _model;
|
||||
|
||||
bool get validate => _validate();
|
||||
|
||||
bool _validate() {
|
||||
bool validate() {
|
||||
if (zipCodeValidator.call(_zipcodeController.text) == null &&
|
||||
streetValidator.call(_streetController.text) == null &&
|
||||
housenumberValidator.call(_housenumberController.text) == null &&
|
||||
|
|
Loading…
Reference in a new issue