Skip to content

Instantly share code, notes, and snippets.

@kmvignesh
Created September 13, 2020 14:51
Show Gist options
  • Select an option

  • Save kmvignesh/0e535e29c5ba9f556d54036d98aa7b6c to your computer and use it in GitHub Desktop.

Select an option

Save kmvignesh/0e535e29c5ba9f556d54036d98aa7b6c to your computer and use it in GitHub Desktop.
TextField and TextFormField
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _secureText = true;
TextEditingController _passwordController = TextEditingController();
String _passwordError;
var _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("TextField Demo"),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: SingleChildScrollView(
child: Column(
children: [
TextField(
decoration: InputDecoration(
hintText: "Your Name",
labelText: "Name",
labelStyle: TextStyle(fontSize: 24, color: Colors.black),
border: InputBorder.none,
fillColor: Colors.black12,
filled: true),
obscureText: false,
maxLength: 20,
),
SizedBox(
height: 16,
),
TextField(
decoration: InputDecoration(
hintText: "Detailed Description",
labelText: "Description",
labelStyle: TextStyle(fontSize: 24, color: Colors.black),
border: UnderlineInputBorder()),
obscureText: false,
maxLines: 3,
),
SizedBox(
height: 16,
),
TextField(
controller: _passwordController,
decoration: InputDecoration(
hintText: "Password",
labelText: "Password",
errorText: _passwordError,
labelStyle: TextStyle(fontSize: 24, color: Colors.black),
border: OutlineInputBorder(),
suffixIcon: IconButton(
icon: Icon(
_secureText ? Icons.remove_red_eye : Icons.security),
onPressed: () {
setState(() {
_secureText = !_secureText;
});
},
)),
obscureText: _secureText,
),
SizedBox(
height: 16,
),
Form(
key: _formKey,
child: Column(
children: [
TextFormField(
validator: (String value){
if(value.length < 10)
return "Enter at least 10 char";
else
return null;
},
decoration: InputDecoration(
hintText: "Name",
labelText: "Name",
labelStyle: TextStyle(fontSize: 24, color: Colors.black),
border: OutlineInputBorder()),
),
SizedBox(
height: 16,
),
TextFormField(
validator: (String value){
if(value.length < 3)
return "Enter at least 3 char";
else
return null;
},
decoration: InputDecoration(
hintText: "Password",
labelText: "Password",
labelStyle: TextStyle(fontSize: 24, color: Colors.black),
border: OutlineInputBorder()),
obscureText: true,
),
],
)),
SizedBox(
height: 16,
),
RaisedButton(onPressed: () {
print("Password : " + _passwordController.text);
setState(() {
print("Form Validation : " + _formKey.currentState.validate().toString());
if(_passwordController.text.length < 3)
_passwordError = "Enter at least 3 char";
else
_passwordError = null;
});
})
],
),
),
),
);
}
}
@Hassanainrazaa
Copy link

Nice work

@UberLT
Copy link

UberLT commented Nov 13, 2022

Your video was great, it will help me a lot in my college work! Very good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment