Issue
I am building a flutter app, and I have a Register
form there. I am using TextFormFields
and below is my code.
import 'package:flutter/material.dart';
class RegisterPage extends StatelessWidget {
final borderColorGreen = const Color(0xff339966);
TextEditingController nameTxtController =TextEditingController();
TextEditingController emailTxtController =TextEditingController();
TextEditingController mobileTxtController =TextEditingController();
TextEditingController passwordTxtController =TextEditingController();
TextEditingController confirmPasswordTxtController =TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/background_login_5.jpg"),
fit: BoxFit.cover,
),
),
child: ListView(
children: <Widget>[
_buildTitleSection(),
_buildInputFields("Name",nameTxtController),
_buildInputFields("Email",emailTxtController),
_buildInputFields("Mobile",mobileTxtController),
_buildInputFields("Password",passwordTxtController),
_buildInputFields("Confirm Password",confirmPasswordTxtController),
_buildRegisterButton(),
],
),
),
);
}
Widget _buildTitleSection() {
return Container(
margin: EdgeInsets.only(top: 40),
padding: EdgeInsets.all(32),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"something.xyz",
style: TextStyle(
color: borderColorGreen,
fontWeight: FontWeight.w900,
fontSize: 25,
),
),
],
));
}
Widget _buildInputFields(String label, TextEditingController textController) {
return Container(
margin: EdgeInsets.only(left: 20, bottom: 20),
child: Column(
children: <Widget>[
Container(
padding: EdgeInsets.only(right: 20),
child: Row(
children: <Widget>[
Flexible(
child: TextFormField(
controller: textController,
style: new TextStyle(color: Colors.white),
decoration: InputDecoration(
labelText: label,
fillColor: Colors.white,
labelStyle: TextStyle(
color: Colors.white, fontWeight: FontWeight.w600),
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: Colors.white30, width: 2.0),
borderRadius: BorderRadius.circular(25.0),
),
),
),
),
],
),
),
],
));
}
Widget _buildRegisterButton() {
return Container(
margin: EdgeInsets.only(top: 10, right: 20, left: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Flexible(
child: SizedBox(
width: double.infinity, // match_parent
child: RaisedButton(
child: Text(
"Create Account",
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.normal),
),
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0)),
color: borderColorGreen,
onPressed: () {},
)))
],
),
);
}
}
When I click on a TextField
the keyboard opens and I can type text. However if I press the back button of my phone to close the keyboard, then the text I entered from all fields.
I am referring to the hardware back button available in Android phones.
The form is scrllable and after entering text to a TextField
if I clicked on the other TextField
, the text stay there as it should. If I press the back button to close the keyboard, all text in the form is gone.
How can I fix this issue?
Solution
Please change your widget to a StatefulWidget
, that will not cause the controllers to reset.
Hope that helps!
Answered By - Hemanth Raj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.