Introduction to GetUtils
GetUtils
It is getx
to provide us with some commonly used tool libraries, including whether the value is empty , whether it is a number , whether it is a video, picture, audio, PPT, Word, APK, email , mobile phone number, date, MD5, SHA1, etc.
Take judging whether it is an email address, mobile phone number, or IPV4 address as an example
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class GetXUtilsExample extends StatelessWidget {
var textFieldController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("GetX Utils"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: EdgeInsets.all(20),
child: TextField(
controller: textFieldController,
),
),
SizedBox(height: 10,),
Padding(
padding: EdgeInsets.all(10),
child: ElevatedButton(
child: Text("判断是否是邮箱"),
onPressed: () async {
if (GetUtils.isEmail(textFieldController.text)) {
Get.snackbar("正确", "恭喜你, 完全正确", backgroundColor: Colors.greenAccent);
} else {
Get.snackbar(
"邮箱错误",
"请输入正确的邮箱",
backgroundColor: Colors.pink
);
}
},
),
),
Padding(
padding: EdgeInsets.all(10),
child: ElevatedButton(
child: Text("判断是否是手机号"),
onPressed: () async {
if (GetUtils.isPhoneNumber(textFieldController.text)) {
Get.snackbar("正确", "恭喜你, 完全正确", backgroundColor: Colors.greenAccent);
} else {
Get.snackbar(
"手机号错误",
"请输入正确的手机号",
backgroundColor: Colors.pink
);
}
},
),
),
Padding(
padding: EdgeInsets.all(10),
child: ElevatedButton(
child: Text("判断是否是IPv4"),
onPressed: () async {
if (GetUtils.isIPv4(textFieldController.text)) {
Get.snackbar("正确", "恭喜你, 完全正确", backgroundColor: Colors.greenAccent);
} else {
Get.snackbar(
"地址错误",
"请输入正确的IPv4地址",
backgroundColor: Colors.pink
);
}
},
),
),
],
),
),
);
}
}
Summarize
GetUtils
It provides us with a lot of basic tool classes, and provides many convenient methods in our project development.