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),
|
onSubmit: (value) => value, controller: _addressController),
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
_addressController.validate;
|
_addressController.validate();
|
||||||
},
|
},
|
||||||
child: Text('Test'))
|
child: Text('Test'),
|
||||||
|
)
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:ffi';
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
@ -48,9 +47,11 @@ class AddressForm extends StatefulWidget {
|
||||||
class _AddressFormState extends State<AddressForm> {
|
class _AddressFormState extends State<AddressForm> {
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Column(
|
return Flexible(
|
||||||
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
AddressFormTextField(
|
AddressFormTextField(
|
||||||
|
validator: widget._addressController.zipCodeValidator,
|
||||||
controller: widget._addressController._zipcodeController,
|
controller: widget._addressController._zipcodeController,
|
||||||
fieldDecoration: widget.zipCodeDecoration,
|
fieldDecoration: widget.zipCodeDecoration,
|
||||||
),
|
),
|
||||||
|
@ -58,10 +59,12 @@ class _AddressFormState extends State<AddressForm> {
|
||||||
child: Row(
|
child: Row(
|
||||||
children: [
|
children: [
|
||||||
AddressFormTextField(
|
AddressFormTextField(
|
||||||
|
validator: widget._addressController.housenumberValidator,
|
||||||
controller: widget._addressController._housenumberController,
|
controller: widget._addressController._housenumberController,
|
||||||
fieldDecoration: widget.housenumberDecoration,
|
fieldDecoration: widget.housenumberDecoration,
|
||||||
),
|
),
|
||||||
AddressFormTextField(
|
AddressFormTextField(
|
||||||
|
validator: widget._addressController.suffixValidator,
|
||||||
controller: widget._addressController._suffixController,
|
controller: widget._addressController._suffixController,
|
||||||
fieldDecoration: widget.suffixDecoration,
|
fieldDecoration: widget.suffixDecoration,
|
||||||
),
|
),
|
||||||
|
@ -69,37 +72,43 @@ class _AddressFormState extends State<AddressForm> {
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
AddressFormTextField(
|
AddressFormTextField(
|
||||||
|
validator: widget._addressController.streetValidator,
|
||||||
controller: widget._addressController._streetController,
|
controller: widget._addressController._streetController,
|
||||||
fieldDecoration: widget.streetDecoration,
|
fieldDecoration: widget.streetDecoration,
|
||||||
),
|
),
|
||||||
AddressFormTextField(
|
AddressFormTextField(
|
||||||
|
validator: widget._addressController.cityValidator,
|
||||||
controller: widget._addressController._cityController,
|
controller: widget._addressController._cityController,
|
||||||
fieldDecoration: widget.cityDecoration,
|
fieldDecoration: widget.cityDecoration,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class AddressFormTextField extends StatelessWidget {
|
class AddressFormTextField extends StatelessWidget {
|
||||||
AddressFormTextField({
|
AddressFormTextField(
|
||||||
super.key,
|
{super.key,
|
||||||
required this.fieldDecoration,
|
required this.fieldDecoration,
|
||||||
required this.controller,
|
required this.controller,
|
||||||
}) {
|
required this.validator});
|
||||||
_addressFieldDecoration = fieldDecoration;
|
|
||||||
}
|
|
||||||
|
|
||||||
final TextEditingController controller;
|
final TextEditingController controller;
|
||||||
final InputDecoration fieldDecoration;
|
final InputDecoration fieldDecoration;
|
||||||
|
final String? Function(String) validator;
|
||||||
|
|
||||||
late final InputDecoration _addressFieldDecoration;
|
late InputDecoration _addressFieldDecoration;
|
||||||
|
|
||||||
|
String? get _errorText => validator(controller.value.text);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return ValueListenableBuilder<TextEditingValue>(
|
return ValueListenableBuilder<TextEditingValue>(
|
||||||
valueListenable: controller,
|
valueListenable: controller,
|
||||||
builder: (context, value, _) {
|
builder: (context, value, _) {
|
||||||
|
_addressFieldDecoration =
|
||||||
|
fieldDecoration.copyWith(errorText: _errorText);
|
||||||
return Flexible(
|
return Flexible(
|
||||||
child: Container(
|
child: Container(
|
||||||
margin: const EdgeInsets.all(10),
|
margin: const EdgeInsets.all(10),
|
||||||
|
@ -165,9 +174,7 @@ class AddressController extends ChangeNotifier {
|
||||||
|
|
||||||
AddressModel get model => _model;
|
AddressModel get model => _model;
|
||||||
|
|
||||||
bool get validate => _validate();
|
bool validate() {
|
||||||
|
|
||||||
bool _validate() {
|
|
||||||
if (zipCodeValidator.call(_zipcodeController.text) == null &&
|
if (zipCodeValidator.call(_zipcodeController.text) == null &&
|
||||||
streetValidator.call(_streetController.text) == null &&
|
streetValidator.call(_streetController.text) == null &&
|
||||||
housenumberValidator.call(_housenumberController.text) == null &&
|
housenumberValidator.call(_housenumberController.text) == null &&
|
||||||
|
|
Loading…
Reference in a new issue