TextFormField 值为空

TextFormField value is null

我试图获取 TextFormField 值。但结果是 null

主页,

children:[
  UrlTextField(),
  UsernameTextField(),
  UrlButton()
]

UrlTextField(), 与 UsernameTextField()

相同
class UrlTextField extends StatelessWidget {
  final myController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return AppTextField(
      decoration:
          InputDecoration(prefixText: "https://", labelText: "Enter your URL"),
      myController: myController,
      textInputType:  TextInputType.url,
    );}}

AppTextField()很普通class,我到处都用这个class

class AppTextField extends StatelessWidget {
  final InputDecoration decoration;
  var myController = TextEditingController();
  final TextInputType textInputType;
  AppTextField({
    this.decoration,
    this.myController,
    this.textInputType
  });

  @override
  Widget build(BuildContext context) {
    return TextFormField(
        controller: myController,
        keyboardType: textInputType,
        textAlign: TextAlign.left,
        decoration: decoration
    );}}

我需要在单击按钮或任何其他区域时获取 Url 和用户名值,

class UrlButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return AppButton(
        onPressed: () {
          String url = UrlTextField().myController.text;
          String username = UsernameTextField().myController.text;
          print('url is $text');
        });}}

AppButton() 这个class也很常见

class AppButton extends StatelessWidget {
  final VoidCallback onPressed;
  AppButton({
    this.buttonTextStyle
  });

  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      child: Text(...),
      onPressed: onPressed);}}

textprint('url is $text'); 中的值是多少不应该是这样吗print('url is $url');

您正在尝试从刚刚在按钮的 onPressed 中实例化的控制器中检索文本,因此目前没有任何文本!要解决此问题,您需要某种状态管理方式来访问和更改现有小部件,在您的情况下为 UrlTextField 小部件。我将举例说明如何快速解决此问题:

主页:

class MainPage extends StatefulWidget {
    ...
    @override
    createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
    UrlTextField _urlTextField = UrlTextField();
    ...

    children:[
      _urlTextField,
      UsernameTextField(),
      UrlButton(_urlTextField)
    ]

现在我们实例化了一个 UrlTextField,它可以被引用并可以传递给另一个小部件,例如您的 UrlButton:

class UrlButton extends StatelessWidget {
    final UrlTextField urlTextField;

    UrlButton(this.urlTextField);

    @override
    Widget build(BuildContext context) {
        return AppButton(
            onPressed: () {
                String url = this.urlTextField.myController.text;
                String username = UsernameTextField().myController.text;
                print('url is $text');
            }
        );
    }
}

通过这种方式,您实例化了一个 UrlTextField 并在您的主页中使用它,用户可以在其中填写一些输入并将其传递给 UrlButton,您可以在其中访问其控制器,从而访问其控制器文本。

我建议您更多地了解状态管理主题,因为有很多方法可以处理这种情况。我可以推荐你看一下 Provider,它非常易于使用并且可以方便地访问某些数据。

我认为这就是你想要做的....但是有很多漏洞兄弟.. 要记住的一件事.. 您需要为每个 TextField 单独的控制器,您不能将一个控制器声明为 myController 并将其分配给所有控制器。它们都将具有相同的值。

class StackHelp extends StatefulWidget {
  @override
  _StackHelp createState() => _StackHelp();
}

class _StackHelp extends State<StackHelp> {
  final TextEditingController myController = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: new Scaffold(
          body: Column(children: <Widget>[
        UrlTextField(myController),
        // UsernameTextField(),
        UrlButton(myController)
      ])),
    );
  }
}

class UrlTextField extends StatelessWidget {
  final TextEditingController myController;
  UrlTextField(this.myController);

  @override
  Widget build(BuildContext context) {
    return AppTextField(
      decoration:
          InputDecoration(prefixText: "https://", labelText: "Enter your URL"),
      myController: myController,
      textInputType: TextInputType.url,
    );
  }
}

class AppTextField extends StatelessWidget {
  final InputDecoration decoration;
  final TextEditingController myController;
  final TextInputType textInputType;
  AppTextField({this.decoration, this.myController, this.textInputType});

  @override
  Widget build(BuildContext context) {
    return TextFormField(
        controller: myController,
        keyboardType: textInputType,
        textAlign: TextAlign.left,
        decoration: decoration);
  }
}

class UrlButton extends StatelessWidget {
  final TextEditingController myController;
  UrlButton(this.myController);

  @override
  Widget build(BuildContext context) {
    void onPressed() {
      String url = this.myController.text;
      // String username = UsernameTextField().myController.text;
      print('url is $url');
    }

    return AppButton(onPressed);
  }
}

class AppButton extends StatelessWidget {
  final VoidCallback onPressed;
  AppButton(this.onPressed);

  @override
  Widget build(BuildContext context) {
    return RaisedButton(child: Text('Test'), onPressed: onPressed);
  }
}