feat : captcha widget
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@@ -16,35 +16,33 @@ class CaptchaWidget extends GetView<CaptchaWidgetLogic> {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
GestureDetector(
|
||||
onTap: controller.getCaptcha,
|
||||
child: Container(
|
||||
width: 135,
|
||||
height: 50,
|
||||
clipBehavior: Clip.antiAliasWithSaveLayer,
|
||||
decoration: BoxDecoration(
|
||||
color: AppColor.whiteNormalHover,
|
||||
border: Border.all(color: Colors.grey.shade300),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: controller.obx(
|
||||
(state) =>
|
||||
Image.memory(
|
||||
base64Decode(state?.captchaImage ?? ''),
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
onLoading: const Center(
|
||||
child: CupertinoActivityIndicator(color: AppColor.blueNormal),
|
||||
onTap: controller.getCaptcha,
|
||||
child: Container(
|
||||
width: 135,
|
||||
height: 50,
|
||||
clipBehavior: Clip.antiAliasWithSaveLayer,
|
||||
decoration: BoxDecoration(
|
||||
color: AppColor.whiteNormalHover,
|
||||
border: Border.all(color: Colors.grey.shade300),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: controller.obx(
|
||||
(state) => Center(
|
||||
child: Stack(
|
||||
alignment: Alignment.center,
|
||||
children: [
|
||||
CustomPaint(size: const Size(135, 50), painter: _CaptchaLinePainter()),
|
||||
Text(controller.captchaKey.value ?? 'دوباره سعی کنید', style: AppFonts.yekan24Bold),
|
||||
],
|
||||
),
|
||||
onError: (error) {
|
||||
return const Center(
|
||||
child: Text(
|
||||
'خطا در بارگذاری کد امنیتی',
|
||||
style: AppFonts.yekan13,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
)),
|
||||
onLoading: const Center(child: CupertinoActivityIndicator(color: AppColor.blueNormal)),
|
||||
onError: (error) {
|
||||
return const Center(child: Text('خطا در بارگذاری کد امنیتی', style: AppFonts.yekan13));
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
@@ -55,19 +53,11 @@ class CaptchaWidget extends GetView<CaptchaWidgetLogic> {
|
||||
return RTextField(
|
||||
label: 'کد امنیتی',
|
||||
controller: data.value,
|
||||
keyboardType: TextInputType.numberWithOptions(
|
||||
decimal: false,
|
||||
signed: false,
|
||||
),
|
||||
keyboardType: TextInputType.numberWithOptions(decimal: false, signed: false),
|
||||
maxLines: 1,
|
||||
maxLength: 6,
|
||||
suffixIcon:
|
||||
(data.value.text
|
||||
.trim()
|
||||
.isNotEmpty ?? false)
|
||||
? clearButton(
|
||||
() => controller.textController.value.clear(),
|
||||
)
|
||||
suffixIcon: (data.value.text.trim().isNotEmpty ?? false)
|
||||
? clearButton(() => controller.textController.value.clear())
|
||||
: null,
|
||||
|
||||
onSubmitted: (data) {},
|
||||
@@ -86,3 +76,33 @@ class CaptchaWidget extends GetView<CaptchaWidgetLogic> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _CaptchaLinePainter extends CustomPainter {
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
final random = Random();
|
||||
final paint1 = Paint()
|
||||
..color = Color.fromRGBO(random.nextInt(255), random.nextInt(255), random.nextInt(255), 1)
|
||||
..strokeWidth = 2;
|
||||
final paint2 = Paint()
|
||||
..color = Color.fromRGBO(random.nextInt(255), random.nextInt(255), random.nextInt(255), 1)
|
||||
..strokeWidth = 2;
|
||||
|
||||
// First line: top-left to bottom-right
|
||||
canvas.drawLine(
|
||||
Offset(0, 0),
|
||||
Offset(size.width, size.height),
|
||||
paint1,
|
||||
);
|
||||
|
||||
// Second line: bottom-left to top-right
|
||||
canvas.drawLine(
|
||||
Offset(0, size.height),
|
||||
Offset(size.width, 0),
|
||||
paint2,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user