Capture data from TextFormField on flutter for http POST request

前端 未结 2 784
心在旅途
心在旅途 2021-02-14 04:53

i am trying to make a login with flutter. I am consulting a web service. I want to send in the body of the Post request the username and the password from different TextFormFiel

2条回答
  •  没有蜡笔的小新
    2021-02-14 05:31

    See Retrieve the value of a text field.

    1. Wrap a StatefulWidget around your form
    2. Add two TextEditingController fields in your State, one for each TextFormField
    3. Pass the controllers to your form fields (controller constructor parameter)
    4. Retrieve the values, for example in a button click listener using myController.text

    I'm not sure if you are also asking how to send a HTTP post request.

    Here is a very minimal example:

    class LoginScreen extends StatefulWidget {
      @override
      State createState() => _LoginScreenState();
    }
    
    class _LoginScreenState extends State {
    
      final _usernameController = TextEditingController();
      final _passwordController = TextEditingController();
    
      @override
      Widget build(BuildContext context) {
        return Column(
          children: [
            TextFormField(controller: _usernameController,),
            TextFormField(controller: _passwordController, obscureText: true,),
            RaisedButton(
              onPressed: _performLogin,
              child: Text('Login'),
            )
          ],
        );
      }
    
      void _performLogin() {
        String username = _usernameController.text;
        String password = _passwordController.text;
    
        print('login attempt: $username with $password');
      }
    }
    

提交回复
热议问题