Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the TextField value in flutter

I have created sign up screen in the flutter. I want to get the email and password in the onPressed of the FlatButton.

Does anyone know how to do this in flutter ?

  Widget SignupPage() {     return new Scaffold(body: Container(       height: MediaQuery.of(context).size.height,       decoration: BoxDecoration(         color: Colors.white,         image: DecorationImage(           colorFilter: new ColorFilter.mode(               Colors.black.withOpacity(0.05), BlendMode.dstATop),           image: AssetImage('assets/images/mountains.jpg'),           fit: BoxFit.cover,         ),       ),       child: new Column(         children: <Widget>[           Container(             padding: EdgeInsets.all(100.0),             child: Center(               child: Icon(                 Icons.headset_mic,                 color: Colors.redAccent,                 size: 50.0,               ),             ),           ),           new Row(             children: <Widget>[               new Expanded(                 child: new Padding(                   padding: const EdgeInsets.only(left: 40.0),                   child: new Text(                     "EMAIL",                     style: TextStyle(                       fontWeight: FontWeight.bold,                       color: Colors.redAccent,                       fontSize: 15.0,                     ),                   ),                 ),               ),             ],           ),           new Container(             width: MediaQuery.of(context).size.width,             margin: const EdgeInsets.only(left: 40.0, right: 40.0, top: 10.0),             alignment: Alignment.center,             decoration: BoxDecoration(               border: Border(                 bottom: BorderSide(                     color: Colors.redAccent,                     width: 0.5,                     style: BorderStyle.solid),               ),             ),             padding: const EdgeInsets.only(left: 0.0, right: 10.0),             child: new Row(               crossAxisAlignment: CrossAxisAlignment.center,               mainAxisAlignment: MainAxisAlignment.start,               children: <Widget>[                 new Expanded(                   child: TextField(                     obscureText: true,                     textAlign: TextAlign.left,                     decoration: InputDecoration(                       border: InputBorder.none,                       hintText: 'PLEASE ENTER YOUR EMAIL',                       hintStyle: TextStyle(color: Colors.grey),                     ),                   ),                 ),               ],             ),           ),           Divider(             height: 24.0,           ),           new Row(             children: <Widget>[               new Expanded(                 child: new Padding(                   padding: const EdgeInsets.only(left: 40.0),                   child: new Text(                     "PASSWORD",                     style: TextStyle(                       fontWeight: FontWeight.bold,                       color: Colors.redAccent,                       fontSize: 15.0,                     ),                   ),                 ),               ),             ],           ),           new Container(             width: MediaQuery.of(context).size.width,             margin: const EdgeInsets.only(left: 40.0, right: 40.0, top: 10.0),             alignment: Alignment.center,             decoration: BoxDecoration(               border: Border(                 bottom: BorderSide(                     color: Colors.redAccent,                     width: 0.5,                     style: BorderStyle.solid),               ),             ),             padding: const EdgeInsets.only(left: 0.0, right: 10.0),             child: new Row(               crossAxisAlignment: CrossAxisAlignment.center,               mainAxisAlignment: MainAxisAlignment.start,               children: <Widget>[                 new Expanded(                   child: TextField(                     obscureText: true,                     textAlign: TextAlign.left,                     decoration: InputDecoration(                       border: InputBorder.none,                       hintText: '*********',                       hintStyle: TextStyle(color: Colors.grey),                     ),                   ),                 ),               ],             ),           ),           Divider(             height: 24.0,           ),           new Row(             children: <Widget>[               new Expanded(                 child: new Padding(                   padding: const EdgeInsets.only(left: 40.0),                   child: new Text(                     "CONFIRM PASSWORD",                     style: TextStyle(                       fontWeight: FontWeight.bold,                       color: Colors.redAccent,                       fontSize: 15.0,                     ),                   ),                 ),               ),             ],           ),           new Container(             width: MediaQuery.of(context).size.width,             margin: const EdgeInsets.only(left: 40.0, right: 40.0, top: 10.0),             alignment: Alignment.center,             decoration: BoxDecoration(               border: Border(                 bottom: BorderSide(                     color: Colors.redAccent,                     width: 0.5,                     style: BorderStyle.solid),               ),             ),             padding: const EdgeInsets.only(left: 0.0, right: 10.0),             child: new Row(               crossAxisAlignment: CrossAxisAlignment.center,               mainAxisAlignment: MainAxisAlignment.start,               children: <Widget>[                 new Expanded(                   child: TextField(                     obscureText: true,                     textAlign: TextAlign.left,                     decoration: InputDecoration(                       border: InputBorder.none,                       hintText: '*********',                       hintStyle: TextStyle(color: Colors.grey),                     ),                   ),                 ),               ],             ),           ),           Divider(             height: 24.0,           ),           new Row(             mainAxisAlignment: MainAxisAlignment.end,             children: <Widget>[               Padding(                 padding: const EdgeInsets.only(right: 20.0),                 child: new FlatButton(                   child: new Text(                     "Already have an account?",                     style: TextStyle(                       fontWeight: FontWeight.bold,                       color: Colors.redAccent,                       fontSize: 15.0,                     ),                     textAlign: TextAlign.end,                   ),                   onPressed: () => {},                 ),               ),             ],           ),           new Container(             width: MediaQuery.of(context).size.width,             margin: const EdgeInsets.only(left: 30.0, right: 30.0, top: 50.0),             alignment: Alignment.center,             child: new Row(               children: <Widget>[                 new Expanded(                   child: new FlatButton(                     shape: new RoundedRectangleBorder(                       borderRadius: new BorderRadius.circular(30.0),                     ),                     color: Colors.redAccent,                     onPressed: () => authHandler.handleSignInEmail(emailController.text, passwordController.text)                         .then((FirebaseUser user) {                       Navigator.push(context, new MaterialPageRoute(builder: (context) => HomePage()));                     }).catchError((e) => print(e)),                     child: new Container(                       padding: const EdgeInsets.symmetric(                         vertical: 20.0,                         horizontal: 20.0,                       ),                       child: new Row(                         mainAxisAlignment: MainAxisAlignment.center,                         children: <Widget>[                           new Expanded(                             child: Text(                               "SIGN UP",                               textAlign: TextAlign.center,                               style: TextStyle(                                   color: Colors.white,                                   fontWeight: FontWeight.bold),                             ),                           ),                         ],                       ),                     ),                   ),                 ),               ],             ),           ),         ],       ),     ));   } 
like image 427
N Sharma Avatar asked Aug 11 '18 06:08

N Sharma


People also ask

How do you get a form value in Flutter?

You can use something like this in the following code: _formKey. currentState. save(); calls the onSaved() on each textFormField items, which assigns the value to all the fields and you can use them as required.


1 Answers

Create your TextEditingController as a global variable in your State class and set it into your TextField widget.

TextEditingController emailController = new TextEditingController();  ....   TextField(   controller: emailController,   obscureText: true,   textAlign: TextAlign.left,   decoration: InputDecoration(     border: InputBorder.none,     hintText: 'PLEASE ENTER YOUR EMAIL',     hintStyle: TextStyle(color: Colors.grey),   ), ) 

Now you can get the value using :

emailController.text 

You can get more information here: https://flutter.dev/docs/cookbook/forms/retrieve-input

like image 155
diegoveloper Avatar answered Oct 08 '22 17:10

diegoveloper