Files
rasadyar_application/packages/auth/lib/presentation/widget/captcha/view.dart

120 lines
3.8 KiB
Dart
Raw Normal View History

2025-06-03 16:55:49 +03:30
import 'dart:math';
2025-05-17 09:39:09 +03:30
2025-05-17 09:08:46 +03:30
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
2025-06-03 22:52:21 +03:30
import 'package:rasadyar_auth/presentation/pages/auth/logic.dart';
2025-05-17 09:08:46 +03:30
import 'package:rasadyar_auth/presentation/widget/clear_button.dart';
import 'package:rasadyar_core/core.dart';
import 'logic.dart';
class CaptchaWidget extends GetView<CaptchaWidgetLogic> {
2025-05-17 15:24:06 +03:30
const CaptchaWidget({super.key});
2025-05-17 09:08:46 +03:30
@override
Widget build(BuildContext context) {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2025-05-17 15:24:06 +03:30
GestureDetector(
2025-06-03 16:55:49 +03:30
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),
],
2025-05-18 10:21:50 +03:30
),
),
2025-06-03 16:55:49 +03:30
onLoading: const Center(child: CupertinoActivityIndicator(color: AppColor.blueNormal)),
onError: (error) {
return const Center(child: Text('خطا در بارگذاری کد امنیتی', style: AppFonts.yekan13));
},
),
),
),
2025-05-18 10:21:50 +03:30
2025-05-17 15:24:06 +03:30
const SizedBox(width: 8),
2025-05-17 09:08:46 +03:30
Expanded(
child: Form(
key: controller.formKey,
2025-05-17 15:24:06 +03:30
autovalidateMode: AutovalidateMode.disabled,
child: ObxValue((data) {
return RTextField(
label: 'کد امنیتی',
controller: data.value,
2025-06-03 16:55:49 +03:30
keyboardType: TextInputType.numberWithOptions(decimal: false, signed: false),
2025-05-17 15:24:06 +03:30
maxLines: 1,
maxLength: 6,
2025-06-03 16:55:49 +03:30
suffixIcon: (data.value.text.trim().isNotEmpty ?? false)
? clearButton(() => controller.textController.value.clear())
2025-05-18 10:21:50 +03:30
: null,
2025-05-17 15:24:06 +03:30
onSubmitted: (data) {},
validator: (value) {
if (value == null || value.isEmpty) {
return 'کد امنیتی را وارد کنید';
2025-06-03 22:52:21 +03:30
} else if (controller.captchaKey.value != null && controller.captchaKey.value != value) {
return 'کد امنیتی اشتباه است';
2025-05-17 15:24:06 +03:30
}
return null;
},
2025-06-03 22:52:21 +03:30
onChanged: (pass) {
if(pass.length== 6) {
if(controller.formKey.currentState?.validate()??false) {
Get.find<AuthLogic>().isDisabled.value = false;
}
}
},
2025-05-17 15:24:06 +03:30
style: AppFonts.yekan13,
);
}, controller.textController),
2025-05-17 09:08:46 +03:30
),
),
],
);
}
}
2025-06-03 16:55:49 +03:30
class _CaptchaLinePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final random = Random();
final paint1 = Paint()
2025-06-03 22:52:21 +03:30
..color = Colors.deepOrange
2025-06-03 16:55:49 +03:30
..strokeWidth = 2;
final paint2 = Paint()
2025-06-03 22:52:21 +03:30
..color =Colors.blue
2025-06-03 16:55:49 +03:30
..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;
}