【问题标题】:How to continuously get whether the TextField's text is empty in Flutter?如何在Flutter中连续获取TextField的文本是否为空?
【发布时间】:2022-12-31 19:12:02
【问题描述】:

我有一个TextField。我希望它的文本不为空。 (所以我想知道文本是否为空)

我尝试使用以下代码,但它不起作用:

controller.text.trim().isEmpty()

我的代码:

TextFormField(
  controller: controller,
),

controller.text.trim().isEmpty()

如果您需要更多信息,请随时发表评论。

如何在Flutter中连续获取TextField的文本是否为空?我将不胜感激任何帮助。先感谢您!

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您可以将侦听器添加到您的TextEditingController 并调用 setState 来更新 UI。

    TextEditingController controller  = TextEditingController()..addListener(() {
       
       setState((){}); // to update the ui
      });
    

    您将使用controller.text.trim().isEmpty() 的地方将显示更新后的状态。

    【讨论】:

    • 您好感谢您的回答! controller 是在build 的内部还是外部?
    • 在构建方法之外。在构建中声明变量将在每次构建时重置。
    • 我收到以下错误:The instance member 'setState' can't be accessed in an initializer.
    【解决方案2】:

    完整示例:

    代码:

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: const MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      const MyHomePage({Key? key}) : super(key: key);
    
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      TextEditingController _controller = TextEditingController();
      String _text = '';
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Flutter Demo Home Page'),
          ),
          body: Container(
            padding: const EdgeInsets.all(16),
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text(_text),
                  const SizedBox(height: 20),
                  TextField(
                    controller: _controller,
                    decoration: const InputDecoration(
                      hintText: 'Enter text',
                    ),
                  ),
                ],
              ),
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: () {
              if (_controller.text.isNotEmpty) {
                setState(() {
                  _text = _controller.text;
                });
              } else {
                setState(() {
                  _text = 'Please enter text';
                });
              }
            },
            tooltip: 'Check',
            child: const Icon(Icons.check),
          ),
        );
      }
    }
    

    【讨论】:

    • 您好感谢您的回答!我想我的问题误解了你。我想要的是一个文本为空时禁用的按钮
    猜你喜欢
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    相关资源
    最近更新 更多