Flutter TextField文本无法打印到变量?

时间:2017-09-05 05:37:57

标签: dart textfield var flutter

onChanged:我的文本字段文本我正在尝试更改我在同一个类中声明的变量,并将其打印在无效函数中。请解释如何打印我的文本域文本。

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;

                  });
                },
              ),

2 个答案:

答案 0 :(得分:1)

这就是我设法用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);
    }
  }

答案 1 :(得分:0)

嗯,我不确定你在这里做什么。但据我所知,你试图在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将在{{1}时更新值正在编辑。

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); },),