• Flutter中GetX系列八--GetUtils(正则判断)使用详情


    GetUtils介绍

    GetUtilsgetx为我们提供一些常用的工具类库,包括值是否为空是否是数字是否是视频、图片、音频、PPT、Word、APK邮箱、手机号码、日期、MD5、SHA1等等。

    以判断是否是邮箱、手机号、IPV4地址为例

    1. import 'package:flutter/material.dart';
    2. import 'package:get/get.dart';
    3. class GetXUtilsExample extends StatelessWidget {
    4. var textFieldController = TextEditingController();
    5. @override
    6. Widget build(BuildContext context) {
    7. return Scaffold(
    8. appBar: AppBar(
    9. title: Text("GetX Utils"),
    10. ),
    11. body: Center(
    12. child: Column(
    13. mainAxisAlignment: MainAxisAlignment.center,
    14. crossAxisAlignment: CrossAxisAlignment.center,
    15. children: [
    16. Padding(
    17. padding: EdgeInsets.all(20),
    18. child: TextField(
    19. controller: textFieldController,
    20. ),
    21. ),
    22. SizedBox(height: 10,),
    23. Padding(
    24. padding: EdgeInsets.all(10),
    25. child: ElevatedButton(
    26. child: Text("判断是否是邮箱"),
    27. onPressed: () async {
    28. if (GetUtils.isEmail(textFieldController.text)) {
    29. Get.snackbar("正确", "恭喜你, 完全正确", backgroundColor: Colors.greenAccent);
    30. } else {
    31. Get.snackbar(
    32. "邮箱错误",
    33. "请输入正确的邮箱",
    34. backgroundColor: Colors.pink
    35. );
    36. }
    37. },
    38. ),
    39. ),
    40. Padding(
    41. padding: EdgeInsets.all(10),
    42. child: ElevatedButton(
    43. child: Text("判断是否是手机号"),
    44. onPressed: () async {
    45. if (GetUtils.isPhoneNumber(textFieldController.text)) {
    46. Get.snackbar("正确", "恭喜你, 完全正确", backgroundColor: Colors.greenAccent);
    47. } else {
    48. Get.snackbar(
    49. "手机号错误",
    50. "请输入正确的手机号",
    51. backgroundColor: Colors.pink
    52. );
    53. }
    54. },
    55. ),
    56. ),
    57. Padding(
    58. padding: EdgeInsets.all(10),
    59. child: ElevatedButton(
    60. child: Text("判断是否是IPv4"),
    61. onPressed: () async {
    62. if (GetUtils.isIPv4(textFieldController.text)) {
    63. Get.snackbar("正确", "恭喜你, 完全正确", backgroundColor: Colors.greenAccent);
    64. } else {
    65. Get.snackbar(
    66. "地址错误",
    67. "请输入正确的IPv4地址",
    68. backgroundColor: Colors.pink
    69. );
    70. }
    71. },
    72. ),
    73. ),
    74. ],
    75. ),
    76. ),
    77. );
    78. }
    79. }

    总结

    GetUtils为我们提供了很多的基础的工具类,在我们的项目开发中提供了很多便捷的方法。

  • 相关阅读:
    权限、认证与授权
    qt 判断文件是否存在
    [LeetCode]-打家劫舍/买卖股票系列题
    EXCEL day 02 公式和函数
    代码随想录第46天 | ● 583. 两个字符串的删除操作 ● 72. 编辑距离
    Android焦点控制和键盘弹出
    EDIFACT Integrator Delphi Edition
    1087 有多少不同的值
    【12月海口】2022年第六届船舶,海洋与海事工程国际会议(NAOME 2022)
    Python玩转emoji表情 一行代码的事儿!
  • 原文地址:https://blog.csdn.net/eastWind1101/article/details/128027805