flutter_registration/lib/src/model/auth_text_field.dart

79 lines
2.3 KiB
Dart
Raw Normal View History

2022-11-01 09:19:20 +01:00
// SPDX-FileCopyrightText: 2022 Iconica
//
// SPDX-License-Identifier: BSD-3-Clause
2022-09-22 10:09:45 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_registration/flutter_registration.dart';
2024-02-19 13:31:12 +01:00
/// A field for capturing text inputs in a Flutter form.
///
/// Extends [AuthField].
2022-09-22 10:09:45 +02:00
class AuthTextField extends AuthField {
2024-02-19 13:31:12 +01:00
/// Constructs an [AuthTextField] object.
///
/// [name] specifies the name of the field.
///
/// [textEditingController] controller for the text input (optional).
///
/// [title] specifies the title widget of the field (optional).
///
/// [validators] defines a list of validation functions for the field (optional).
///
/// [value] specifies the initial value of the field (default is an empty string).
///
/// [textStyle] defines the text style for the text input.
///
/// [onChange] is a callback function triggered when the value of the field changes.
///
/// [textFieldDecoration] defines the decoration for the text input field (optional).
///
/// [padding] defines the padding around the text input field (default is EdgeInsets.all(8.0)).
2022-09-22 10:09:45 +02:00
AuthTextField({
required super.name,
TextEditingController? textEditingController,
super.title,
2022-09-22 10:09:45 +02:00
super.validators = const [],
super.value = '',
this.textStyle,
this.onChange,
this.textFieldDecoration,
this.padding = const EdgeInsets.all(8.0),
2022-09-22 10:09:45 +02:00
}) {
textController =
textEditingController ?? TextEditingController(text: value);
2022-09-22 10:09:45 +02:00
}
late TextEditingController textController;
final TextStyle? textStyle;
final Function(String value)? onChange;
final InputDecoration? textFieldDecoration;
final EdgeInsets padding;
2022-09-22 10:09:45 +02:00
@override
Widget build(BuildContext context, Function onValueChanged) {
return Padding(
padding: padding,
child: TextFormField(
style: textStyle,
decoration: textFieldDecoration,
controller: textController,
onChanged: (v) {
value = v;
onChange?.call(value);
onValueChanged();
},
validator: (value) {
for (var validator in validators) {
var output = validator(value);
if (output != null) {
return output;
}
2022-09-22 10:09:45 +02:00
}
return null;
},
),
);
}
2022-09-22 10:09:45 +02:00
}