Created with <3 with dartpad.dev.
Last active
March 20, 2023 10:05
-
-
Save Chralu/cd87f00e2433eda0486363acab67efda to your computer and use it in GitHub Desktop.
Form- custom textfield
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
const Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
body: Center( | |
child: Form( | |
child: CustomFormField( | |
autovalidateMode: AutovalidateMode.onUserInteraction, | |
initialValue: "Salut la compagnie", | |
inputDecoration: const InputDecoration( | |
hintText: "Toto only", | |
suffixIcon: Icon(Icons.catching_pokemon), | |
), | |
validator: (value) { | |
if (value?.toLowerCase() != "toto") return "No no !"; | |
return null; | |
}, | |
), | |
), | |
), | |
), | |
); | |
} | |
} | |
class CustomFormField extends FormField<String> { | |
CustomFormField({ | |
super.key, | |
this.inputDecoration, | |
FormFieldValidator<String>? validator, | |
AutovalidateMode? autovalidateMode, | |
String? initialValue, | |
FocusNode? focusNode, | |
}) : super( | |
validator: validator, | |
initialValue: initialValue, | |
autovalidateMode: autovalidateMode, | |
builder: (FormFieldState<String> state) { | |
return Builder( | |
builder: (context) => TextField( | |
focusNode: focusNode, | |
decoration: inputDecoration?.copyWith( | |
errorText: state.errorText, | |
suffixIcon: state.hasError | |
? Icon( | |
Icons.error, | |
color: Theme.of(context).colorScheme.error, | |
) | |
: inputDecoration.suffixIcon, | |
), | |
onChanged: state.didChange, | |
), | |
); | |
}, | |
); | |
final InputDecoration? inputDecoration; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment