Skip to main content

AsTextField

A versatile text input component with platform-adaptive styling, validation support, and customizable appearance.

Import

import 'package:alphasow_ui/alphasow_ui.dart';

Basic Usage

AsTextField(
labelText: 'Enter your name',
onChanged: (value) {
print('Text changed: $value');
},
)

With Hint and Helper Text

AsTextField(
labelText: 'Email',
hintText: 'example@domain.com',
helperText: 'We will never share your email',
onChanged: (value) {},
)

Password Field

AsTextField(
labelText: 'Password',
obscureText: true,
suffixIcon: Icons.visibility,
onChanged: (value) {},
)

With Validation

class MyForm extends StatefulWidget {
@override
_MyFormState createState() => _MyFormState();
}

class _MyFormState extends State<MyForm> {
final _formKey = GlobalKey<FormState>();

@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Column(
children: [
AsTextField(
labelText: 'Email',
keyboardType: TextInputType.emailAddress,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter an email';
}
if (!value.contains('@')) {
return 'Please enter a valid email';
}
return null;
},
onChanged: (value) {},
),
AsButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
// Form is valid
}
},
child: Text('Submit'),
),
],
),
);
}
}

Multiline Text Field

AsTextField(
labelText: 'Description',
maxLines: 4,
minLines: 2,
onChanged: (value) {},
)

With Prefix and Suffix

AsTextField(
labelText: 'Phone',
prefixIcon: Icons.phone,
suffixText: 'Required',
keyboardType: TextInputType.phone,
onChanged: (value) {},
)

API Reference

Properties

PropertyTypeDefaultDescription
controllerTextEditingController?nullControls the text being edited
labelTextString?nullLabel text displayed above the field
hintTextString?nullHint text displayed when field is empty
helperTextString?nullHelper text displayed below the field
errorTextString?nullError text displayed when validation fails
obscureTextboolfalseWhether to hide the text being edited
enabledbooltrueWhether the text field is enabled
readOnlyboolfalseWhether the text field is read-only
maxLinesint?1Maximum number of lines
minLinesint?nullMinimum number of lines
maxLengthint?nullMaximum number of characters
keyboardTypeTextInputType?nullType of keyboard to show
textInputActionTextInputAction?nullAction button on the keyboard
validatorString? Function(String?)?nullForm validation function
onChangedValueChanged<String>?nullCalled when text changes
onSubmittedValueChanged<String>?nullCalled when user submits
prefixIconIconData?nullIcon displayed at the start
suffixIconIconData?nullIcon displayed at the end
prefixTextString?nullText displayed at the start
suffixTextString?nullText displayed at the end

Examples

Search Field

AsTextField(
labelText: 'Search',
prefixIcon: Icons.search,
hintText: 'Search for items...',
onChanged: (value) {
// Perform search
},
)

Number Input

AsTextField(
labelText: 'Age',
keyboardType: TextInputType.number,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your age';
}
final age = int.tryParse(value);
if (age == null || age < 0 || age > 150) {
return 'Please enter a valid age';
}
return null;
},
onChanged: (value) {},
)

Currency Input

AsTextField(
labelText: 'Price',
prefixText: '\$',
keyboardType: TextInputType.numberWithOptions(decimal: true),
onChanged: (value) {},
)

Controlled Input

class ControlledTextField extends StatefulWidget {
@override
_ControlledTextFieldState createState() => _ControlledTextFieldState();
}

class _ControlledTextFieldState extends State<ControlledTextField> {
final _controller = TextEditingController();

@override
void dispose() {
_controller.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Column(
children: [
AsTextField(
controller: _controller,
labelText: 'Controlled Input',
onChanged: (value) {},
),
AsButton(
onPressed: () {
_controller.clear();
},
child: Text('Clear'),
),
],
);
}
}

Platform Differences

Material (Android/Web)

  • Uses TextFormField with Material styling
  • Material Design input decoration
  • Material focus and error states

Cupertino (iOS/macOS)

  • Uses CupertinoTextFormFieldRow
  • iOS-style input styling
  • Native iOS keyboard behavior

See Also