Skip to content

Instantly share code, notes, and snippets.

@vijayinyoutube
Created April 10, 2020 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vijayinyoutube/74a2a4af13473f117895a2583f3f41c7 to your computer and use it in GitHub Desktop.
Save vijayinyoutube/74a2a4af13473f117895a2583f3f41c7 to your computer and use it in GitHub Desktop.
Input form fields in flutter
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() => runApp(MyApp());
String choice;
String _radioValue;
String _selectedCity;
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: MyPage(),
);
}
}
class MyPage extends StatefulWidget {
@override
_MyPageState createState() => _MyPageState();
}
class _MyPageState extends State<MyPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.green,
leading: Icon(Icons.contact_mail),
title: Text("Personal Details"),
centerTitle: true,
),
body: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Column(
children: <Widget>[
SizedBox(
height: 50,
),
TextFormField(
decoration: InputDecoration(
errorStyle: TextStyle(color: Colors.red, fontSize: 15.0),
labelText: "Enter your Name",
hintStyle: TextStyle(
color: Colors.grey,
fontSize: 16.0,
),
prefixIcon: Icon(
Icons.person,
),
),
),
SizedBox(height: 25),
TextFormField(
inputFormatters: [
WhitelistingTextInputFormatter.digitsOnly,
new LengthLimitingTextInputFormatter(10),
],
keyboardType: TextInputType.number,
decoration: InputDecoration(
errorStyle: TextStyle(color: Colors.red, fontSize: 15.0),
labelText: "Enter your Mobile Number",
hintStyle: TextStyle(
color: Colors.grey,
fontSize: 16.0,
),
prefixIcon: Icon(
Icons.phone,
),
),
),
SizedBox(height: 25),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text("Are you a programmer?"),
SizedBox(width: 25),
Radio(
value: 'one',
groupValue: _radioValue,
onChanged: radioButtonChanges,
),
Text(
"Yes",
),
Radio(
value: 'two',
groupValue: _radioValue,
onChanged: radioButtonChanges,
),
Text(
"No",
),
],
),
SizedBox(
height: 25,
),
TextFormField(
decoration: InputDecoration(
errorStyle: TextStyle(color: Colors.red, fontSize: 15.0),
labelText: "Enter your Email ID",
hintStyle: TextStyle(
color: Colors.grey,
fontSize: 16.0,
),
prefixIcon: Icon(
Icons.mail,
),
),
),
SizedBox(
height: 25,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text("Enter your city"),
SizedBox(width: 25),
new DropdownButton<String>(
value: _selectedCity,
items: <String>['City-A', 'City-B', 'City-C', 'City-D']
.map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (newValue) {
setState(() {
_selectedCity = newValue;
print(_selectedCity);
});
},
),
],
),
SizedBox(height: 25),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new RaisedButton(
textColor: Colors.white,
color: Colors.red,
onPressed: () {},
child: new Text("Reset"),
),
new RaisedButton(
textColor: Colors.white,
color: Colors.green,
onPressed: () {},
child: new Text("Submit"),
),
],
),
],
),
),
),
);
}
void radioButtonChanges(String value) {
setState(() {
_radioValue = value;
switch (value) {
case 'one':
choice = "He is a programmer !";
break;
case 'two':
choice = "No, He is not a Programmer !";
break;
default:
choice = null;
}
debugPrint(choice);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment