简体   繁体   English

如何在颤振上使用 switch case 语法

[英]How to use switch case syntax on flutter

I'm making login and sign up page in my mobile application with flutter.我正在用颤振在我的移动应用程序中制作登录和注册页面。 I'm wondering how can I put the right data which is matched with the props.我想知道如何放置与道具匹配的正确数据。

For example, if I got the 'password' as a textValue, then I want to return the warningText which is 'Password must be at least 7 characters long.', the icon would be Icons.lock, and the hintText would be 'password'.例如,如果我将“密码”作为 textValue,那么我想返回警告文本,即“密码必须至少为 7 个字符长。”,图标为 Icons.lock,提示文本为“密码” '。

The textValue can be three options which are 'username', 'password' and 'email'. textValue 可以是“用户名”、“密码”和“电子邮件”三个选项。

  • textValue : username textValue : 用户名

    • warningText : 'Please enter at least 4 characters' warningText : '请输入至少 4 个字符'
    • icon : Icons.account_circle,图标:Icons.account_circle,
    • hintText : username提示文本:用户名
  • textValue : email文本值:电子邮件

    • warningText : 'Please enter a valid email address.' warningText : '请输入有效的电子邮件地址。'
    • icon : Icons.email,图标 : Icons.email,
    • hintText : email提示文本:电子邮件

Also, the reason why I want to use switch case syntax is because I want to make my codes shorter.另外,我想使用 switch case 语法的原因是因为我想让我的代码更短。 I could just use multiple of textformfield, but I want to make my codes shorter and make them re-useable.我可以只使用多个 textformfield,但我想让我的代码更短并使其可重用。

Because I'm using Provider for state management, I want to put the switch case codes into the Provider.因为我使用Provider进行状态管理,所以我想将switch case代码放入Provider中。

This is the textformfield widget code.这是 textformfield 小部件代码。

class LoginSignupTextFormField extends StatelessWidget {
  const LoginSignupTextFormField({Key? key,
    required this.valueKeyNumb,
    required this.textValue})
  : super(key: key);

  final int valueKeyNumb;
  final String textValue;
  @override
  Widget build(BuildContext context) {
    return TextFormField(
      obscureText: textValue == 'password' ? true : false,
      key: const ValueKey(3),
      validator: (value) {
        if (value!.isEmpty || value.length < 6) {
          return 'Password must be at least 7 characters long.';
        }
        return null;
      },
      onSaved: (value) {
        context.watch<LoginSignupData>().userPassword = value!;
      },
      onChanged: (value) {
        context.watch<LoginSignupData>().userPassword = value;
      },
      decoration: const InputDecoration(
          prefixIcon: Icon(
            Icons.lock,
            color: Colors.red,
          ),
          enabledBorder: OutlineInputBorder(
            borderSide: BorderSide(
                color: Colors.blueGrey),
            borderRadius: BorderRadius.all(
              Radius.circular(35.0),
            ),
          ),
          focusedBorder: OutlineInputBorder(
            borderSide: BorderSide(
                color: Colors.black),
            borderRadius: BorderRadius.all(
              Radius.circular(35.0),
            ),
          ),
          hintText: 'password',
          hintStyle: TextStyle(
              fontSize: 14,
              color: Colors.red),
          contentPadding: EdgeInsets.all(10)),
    );
  }
}

This is my Provider code for the textformfield and I want to put the switch case codes in it.这是我的 textformfield 提供程序代码,我想将 switch case 代码放入其中。

class LoginSignupData extends ChangeNotifier{
  final authentication = FirebaseAuth.instance;

  bool isSignup = true;
  final formKey = GlobalKey<FormState>();
  String userName = '';
  String userEmail = '';
  String userPassword = '';

  changeBool(status){
    isSignup = status;
    notifyListeners();
  }

  tryValidation() {
    final isValid = formKey.currentState!.validate();
    if (isValid) {
      formKey.currentState!.save();
      notifyListeners();
    }
  }
}

You may have to take three separate textfield for that and assign values to each.您可能需要为此使用三个单独的文本字段并为每个字段分配值。

If you want to make common textfield for all then you can take enum with three values and you have to pass that in constructor.如果您想为所有人创建通用文本字段,那么您可以使用三个值的枚举,并且您必须在构造函数中传递它。

enum TextFieldType {EMAIL, PASSWORD, USERNAME};

Now you can check the value inside the validator and according to that you can show the error.现在您可以检查验证器中的值,并据此显示错误。

switch (enumValue) { // Your Enum Value which you have passed
  case TextFieldType.PASSWORD:
    //Your validations for password
    break;
  case TextFieldType.EMAIL:
    //Your validations for email
    break;
  case TextFieldType.USERNAME:
    //Your validations for username
    break;
}

Hope it will help you.希望它会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM