我正在尝试用Flutter登录。我正在咨询Web服务。我想在Post请求的正文中发送来自不同TextFormField的用户名和密码。我怎样才能做到这一点?这是我一直在努力的代码。
import 'package:flutter/material.dart'; import 'package:device_id/device_id.dart'; import 'package:http/http.dart' as http; import 'dart:async'; import 'dart:convert'; class SignIn extends StatefulWidget { @override _SignInState createState() => _SignInState(); } class _SignInState extends State{ Future getData() async { final response = await http.post( Uri.encodeFull("The route i'm consulting), body: { "username": user, "password": password },
我想从此处的用户名和密码TextFormField检索输入文本
headers: { "Accept": "application/json", }); print(response.statusCode); print(response.body); } String _deviceid = 'Unknown'; String user = ''; String password = ''; TextEditingController controller = new TextEditingController(); TextEditingController controller2 = new TextEditingController(); @override void dispose() { controller.dispose(); controller2.dispose(); super.dispose(); } @override Widget build(BuildContext context) { username = TextFormField(
这是我要检索的第一个TextFormField,以在请求正文中发送
controller: controller, keyboardType: TextInputType.text, autofocus: false, decoration: InputDecoration( hintText: "Username", hintStyle: TextStyle(fontSize: 16.0), contentPadding: EdgeInsets.fromLTRB(20.0, 0.0, 20.0, 10.0), border: UnderlineInputBorder(borderRadius: BorderRadius.circular(32.0))), ); final password = TextFormField(
这是我要检索的第二个TextFormField,以将其发送到请求的正文中
controller: controller2, autofocus: false, obscureText: true, decoration: InputDecoration( hintText: "Password", hintStyle: TextStyle(fontSize: 16.0), contentPadding: EdgeInsets.fromLTRB(20.0, 25.0, 20.0, 10.0), border: UnderlineInputBorder(borderRadius: BorderRadius.circular(32.0))), ); final loginButton = Padding( padding: EdgeInsets.symmetric(vertical: 25.0), child: Material( borderRadius: BorderRadius.circular(30.0), shadowColor: Colors.blueAccent.shade100, elevation: 10.0, child: MaterialButton( minWidth: 200.0, height: 42.0, color: Colors.blueAccent, onPressed: (){ }, child: Text( "Login", style: TextStyle(color: Colors.white), ), ), ), ); return Form( child: new Center( child: ListView( padding: EdgeInsets.only(left: 24.0, right: 24.0, top: 10.0), children:[ username, SizedBox(height: 8.0), password, SizedBox(height: 24.0), loginButton ]), ), ); } }
boformer.. 16
请参阅检索文本字段的值。
StatefulWidget
在表格周围包裹一个
在中添加两个TextEditingController
字段,State
每个字段一个TextFormField
将控制器传递到表单字段(controller
构造函数参数)
检索值,例如在按钮单击侦听器中使用 myController.text
我不确定您是否也在询问如何发送HTTP发布请求。
这是一个非常小的示例:
class LoginScreen extends StatefulWidget { @override StatecreateState() => _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'); } }
Barakat Turk.. 5
这是登录屏幕的完整示例,您可以在其中验证输入并在通过验证后提交数据。
import 'package:flutter/material.dart'; import '../mixins/validate_mixin.dart'; class LoginScreen extends StatefulWidget{ final GlobalKeyscaffoldKey; LoginScreen(this.scaffoldKey); @override State createState() { return LoginScreenState(scaffoldKey); } } class LoginScreenState extends State with ValidateMixin{ final formKey = GlobalKey (); final GlobalKey scaffoldKey; LoginScreenState(this.scaffoldKey); String _email; String _password; @override Widget build(BuildContext context) { return Container( margin: EdgeInsets.all(40.0), child: Form( key: formKey, child: Column( children: [ emailField(), passwordField(), Container(margin: EdgeInsets.only(bottom: 25.0),), submitButton(), ], ), ), ); } Widget emailField() { return TextFormField( decoration: InputDecoration(hintText: 'ali@gmail.co', labelText: 'Email'), keyboardType: TextInputType.emailAddress, validator: validateEmail, onSaved: (String value) { _email = value; }, ); } Widget passwordField() { return TextFormField( obscureText: true, decoration: InputDecoration(hintText: '*****', labelText: 'Password'), onSaved: (String value) { _password = value; }, ); } Widget submitButton() { return RaisedButton.icon( color: Colors.cyan[900], textColor: Colors.white, label: Text('Submit'), icon: Icon(Icons.save), onPressed: () { final bool v = formKey.currentState.validate(); if (v) { formKey.currentState.save(); _performLogin(); print('object'); } },); } void _performLogin () { var snackbar = new SnackBar( content: Text('Email: $_email and Password $_password'), ); scaffoldKey.currentState.showSnackBar(snackbar); } }
您可以返回完整的示例。 https://github.com/barakat-turki/flutter_login_screen
请参阅检索文本字段的值。
StatefulWidget
在表格周围包裹一个
在中添加两个TextEditingController
字段,State
每个字段一个TextFormField
将控制器传递到表单字段(controller
构造函数参数)
检索值,例如在按钮单击侦听器中使用 myController.text
我不确定您是否也在询问如何发送HTTP发布请求。
这是一个非常小的示例:
class LoginScreen extends StatefulWidget { @override StatecreateState() => _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'); } }
这是登录屏幕的完整示例,您可以在其中验证输入并在通过验证后提交数据。
import 'package:flutter/material.dart'; import '../mixins/validate_mixin.dart'; class LoginScreen extends StatefulWidget{ final GlobalKeyscaffoldKey; LoginScreen(this.scaffoldKey); @override State createState() { return LoginScreenState(scaffoldKey); } } class LoginScreenState extends State with ValidateMixin{ final formKey = GlobalKey (); final GlobalKey scaffoldKey; LoginScreenState(this.scaffoldKey); String _email; String _password; @override Widget build(BuildContext context) { return Container( margin: EdgeInsets.all(40.0), child: Form( key: formKey, child: Column( children: [ emailField(), passwordField(), Container(margin: EdgeInsets.only(bottom: 25.0),), submitButton(), ], ), ), ); } Widget emailField() { return TextFormField( decoration: InputDecoration(hintText: 'ali@gmail.co', labelText: 'Email'), keyboardType: TextInputType.emailAddress, validator: validateEmail, onSaved: (String value) { _email = value; }, ); } Widget passwordField() { return TextFormField( obscureText: true, decoration: InputDecoration(hintText: '*****', labelText: 'Password'), onSaved: (String value) { _password = value; }, ); } Widget submitButton() { return RaisedButton.icon( color: Colors.cyan[900], textColor: Colors.white, label: Text('Submit'), icon: Icon(Icons.save), onPressed: () { final bool v = formKey.currentState.validate(); if (v) { formKey.currentState.save(); _performLogin(); print('object'); } },); } void _performLogin () { var snackbar = new SnackBar( content: Text('Email: $_email and Password $_password'), ); scaffoldKey.currentState.showSnackBar(snackbar); } }
您可以返回完整的示例。 https://github.com/barakat-turki/flutter_login_screen