Flutter TextField文本不打印到变量?
问题描述:
我的文本框文本onChanged:
我试图改变我在同一个类中声明的变量,并让它在无效函数中打印。请解释如何打印我的文本字段文本。Flutter TextField文本不打印到变量?
class LoginPageState extends State<LoginPage> {
@override
var _email;
var _password;
var _username;
final TextEditingController controller = new TextEditingController();
void _loginButton() {
print("Login from Page");
print(_password);
print(_username);
print(_email);
}
Widget build(BuildContext context) {
return new Scaffold(
backgroundColor: Colors.white70,
appBar: new AppBar(
backgroundColor: Colors.amber,
title: new Text("Login/Signup"),
),
body: new Container(
child: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new TextField(
controller: controller,
decoration:
new InputDecoration(labelText: "E M A I L A D D R E S S"),
onChanged: (String newString){
setState((){
_email = controller.text;
});
},
),
答
嗯,我不知道你在这里做什么。但根据我的理解,您试图在TextField
中保存用户的输入。
我已经而不需要setState()
如果您需要获得用户的输入,如电子邮件,您应该获取用户提交后的数据来实现这一点,所以我不知道为什么你想在你的情况下使用onChanged
。
基本上你需要做的是向用户提供一个按钮,在完成数据填充后应该按下按钮,然后使用你的TextEditingController
来获得用户输入的内容,并且你可以做你想做的事那里。
这是展示如何做到这一点的代码,我想你正在尝试做类似的东西,以一个新会员注册功能,你的应用程序中:
import 'package:flutter/material.dart';
void main() {
runApp(new MaterialApp(
home: new MyApp(),
));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
var _email;
var _password;
var _username;
final TextEditingController _nameController = new TextEditingController();
final TextEditingController _emailController = new TextEditingController();
final TextEditingController _passController = new TextEditingController();
void _loginButton({String name, String pass, String email}) {
print("Login from Page");
this._username = name;
this._email = email;
this._password = pass;
print(_username);
print(_email);
print(_password);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Column (
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new TextField(controller: this._nameController, decoration: new InputDecoration(hintText: "Type your user name"),),
new TextField(controller: this._emailController,decoration: new InputDecoration(hintText: "Type your email")),
new TextField(controller: this._passController,decoration: new InputDecoration(hintText: "Type your password")),
new RaisedButton(child: new Text ("Sign In"),
onPressed:() {
_loginButton(name: this._nameController.text,
email: this._emailController.text,
pass: this._passController.text
);
})
],
));
}
}
如果你不想使用一个按钮,然后使用onSubmitted
属性在键盘上按下返回按钮时保存用户输入,而onChanged
将在编辑TextField
时更新该值。
new TextField(controller: this._nameController,
decoration: new InputDecoration(hintText: "Type your user name"),
onSubmitted: (String s) { //when the user press the return button do this
_loginButton(name: this._nameController.text);
},
onChanged: (String s) { //when the user is editing the text do this
_loginButton(name: this._nameController.text);
},),
,不过也许这将是更好地为您,因为您有多个TextField
需要立刻被保存使用的按钮。
答
这是我如何管理与onChanged:
来完成这个...
new TextField(
controller: _emailController,
decoration:
new InputDecoration(labelText: "E M A I L A D D R E S S"),
onChanged: (str) {
setState(() {
str = _emailController.text;
});
},
),
Future<Null> _loginButton() async {
try {
FirebaseAuth.instance.signOut();
print("Login from Page");
_email = _emailController.text.toString();
_password = _passController.text.toString();
_username = _nameController.text.toString();
print(_email);
print(_username);
print(_password);
await FirebaseAuth.instance
.signInWithEmailAndPassword(email: _email, password: _password);
var userid = FirebaseAuth.instance.currentUser.uid;
if (FirebaseAuth.instance.currentUser.photoUrl == null) {
fb.child("users/${userid}/image").onValue.listen((Event event) {
print('Child added: ${event.snapshot.value}');
});
}
print(FirebaseAuth.instance.currentUser);
} catch (error) {
print(error);
}
}
你可以称之为'_loginButton()'要么在'build'或过去的事情了'return'前'的setState '功能。但我不知道我是否真的明白你的问题...... –