import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
final TextEditingController controller = TextEditingController();
final _form = GlobalKey<FormState>();
void _incrementCounter() {
setState(() {
_counter++;
});
}
bool isValid = true;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
margin: const EdgeInsets.all(10),
child: Column(
children: [
Form(
key: _form,
child: FormField(
validator: (val) =>
val == null || val == '' ? 'Type Something!' : null,
builder: (formfielstate) {
return Column(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisSize: MainAxisSize.min,
children: [
TextField(
controller: controller,
decoration: const InputDecoration(labelText: 'Enter'),
),
if (formfielstate.hasError)
Padding(
padding: const EdgeInsets.only(top: 10),
child: Text(
formfielstate.errorText!,
style: const TextStyle(color: Colors.red),
),
),
],
);
},
),
),
Align(
alignment: Alignment.center,
child: ElevatedButton(
onPressed: () {
if (_form.currentState!.validate()) {}
},
child: const Text('Click'),
),
)
],
),
),
);
}
}
// TextFormField(
// controller: controller,
// decoration: const InputDecoration(
// errorStyle: TextStyle(
// textBaseline: TextBaseline.alphabetic
// ),
// hintText: 'Enter',
// border: OutlineInputBorder(
// borderSide: BorderSide(
// color: Colors.black38,
// )),
// enabledBorder: OutlineInputBorder(
// borderSide: BorderSide(
// color: Colors.black38,
// ))),
// validator: (val) {
// if (val == null || val.isEmpty) {
// return 'Enter your Enter';
// }
//
// return null;
// },
// ),
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter