ここで何をしようとしているのかよく分かりません。しかし、あなたが理解しているところでは、ユーザの入力を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
プロパティを使用してユーザー入力を保存し、TextField
を編集中はonChanged
が値を更新します。
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
を持っているので、おそらくそれが良いだろう。
です'関数。しかし、私は本当にあなたの質問を理解しているかどうかわからない... –