TextFormField 中的空值

Empty value in TextFormField

这是一个AlertDialog,当用户输入姓名时,姓名不能为null或为空。

import 'package:flutter/material.dart';

abstract class ComponentDialog {
  static final TextEditingController _controller = TextEditingController();

  static Future<dynamic> BuidDialog(
      BuildContext context,
      String titleLabel,
      String inputLabel,
      String cancelLabel,
      String confirmLabel,
      ) =>

      showDialog(
        context: context,
        builder: (BuildContext context) => AlertDialog(
          title: Text(titleLabel),
          content: TextFormField(
            controller: _controller,
            decoration: InputDecoration(
              labelText: inputLabel,
              border: const OutlineInputBorder(
                borderSide: BorderSide(
                  color: Colors.pinkAccent, // TODO: charge color
                ),
              ),
            ),
          ),
          actions: <Widget>[
            TextButton(
              onPressed: () => Navigator.pop(context),
              child: Text(cancelLabel),
            ),
            TextButton(
              onPressed: () {
                if (stringTitleLabel.IsNullOrWhiteSpace(objeto)) {

                  return;
                }
                Navigator.pop(context, _controller.text);
              },
              child: Text(confirmLabel),
            ),
          ],
        ),
      );
}

我们可以使用 StatefulBuildershowDialog 上更新 UI,在 TextFormFiled 上使用 errorText 显示错误消息,onTap 将重置错误信息。

abstract class ComponentDialog {
  static final TextEditingController _controller = TextEditingController();
  String? errorText;
  static Future<dynamic> BuidDialog(
    BuildContext context,
    String titleLabel,
    String inputLabel,
    String cancelLabel,
    String confirmLabel,
  ) async {
    String? errorText;
    return await showDialog(
      context: context,
      builder: (BuildContext context) => StatefulBuilder(
        builder: (context, setStateSB) {
          return AlertDialog(
            title: Text(titleLabel),
            content: TextFormField(
              controller: _controller,
              onTap: () {
                setStateSB(() {
                  errorText = null;
                });
              },
              decoration: InputDecoration(
                errorText: errorText,
                labelText: inputLabel,
                border: const OutlineInputBorder(
                  borderSide: BorderSide(
                    color: Colors.pinkAccent,
                  ),
                ),
              ),
            ),
            actions: <Widget>[
              TextButton(
                onPressed: () {
                  if (_controller.text.isEmpty) {
                    setStateSB(() {
                      errorText = "Enter Name";
                    });

                    return;
                  }
                  // Navigator.pop(context, _controller.text);
                },
                child: Text(confirmLabel),
              ),
            ],
          );
        },
      ),
    );
  }
}
 Padding(
       padding: EdgeInsets.only(left: 2.0, top: 1.0, right: 120.0, bottom: 4.0),
       child: Text(
                'name', //username
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16,),
                ),
       ),