Houshan-Basa/lib/ui/widgets/components/text/mobile_number_text_field.dart

130 lines
4.9 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hoshan/core/utils/strings.dart';
import 'package:hoshan/ui/theme/colors.dart';
import 'package:hoshan/ui/theme/text.dart';
class MobileNumberTextField extends StatefulWidget {
final TextEditingController? controller;
final Function(String)? onChange;
final String? hintText;
final int? maxLength;
final String? label;
final Widget? suffix;
final Widget? error;
final Widget? success;
final bool isPassword;
final bool enabled;
final bool justEnglish;
final int? minLines;
final int maxLines;
final TextInputAction textInputAction;
const MobileNumberTextField(
{super.key,
this.hintText,
this.maxLength,
this.label,
this.suffix,
this.error,
this.isPassword = false,
this.controller,
this.onChange,
this.minLines,
this.textInputAction = TextInputAction.done,
this.justEnglish = false,
this.maxLines = 1,
this.success,
this.enabled = true});
@override
State<MobileNumberTextField> createState() => _MobileNumberTextFieldState();
}
class _MobileNumberTextFieldState extends State<MobileNumberTextField> {
ValueNotifier<bool> isPassword = ValueNotifier(true);
String text = '';
@override
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: isPassword,
builder: (context, value, _) {
return Directionality(
textDirection: TextDirection.rtl,
child: TextField(
maxLength: widget.maxLength,
obscureText: widget.isPassword && value,
style: AppTextStyles.body5.copyWith(
color: widget.enabled
? Theme.of(context).colorScheme.onSurface
: AppColors.gray[700]),
controller: widget.controller,
textInputAction: widget.minLines != null && widget.minLines! > 1
? TextInputAction.newline
: widget.textInputAction,
onChanged: (value) {
setState(() {
text = value;
});
widget.onChange?.call(value);
},
keyboardType: TextInputType.phone,
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
minLines: widget.minLines,
maxLines: widget.maxLines,
textDirection: text.startsWithEnglish()
? TextDirection.ltr
: TextDirection.ltr,
decoration: InputDecoration(
alignLabelWithHint: true,
hintText: widget.hintText,
hintStyle: AppTextStyles.body5,
contentPadding: const EdgeInsets.all(18),
error: widget.error ?? widget.success,
enabled: widget.enabled,
labelStyle: AppTextStyles.body5
.copyWith(color: Theme.of(context).colorScheme.onSurface),
floatingLabelStyle: AppTextStyles.body5
.copyWith(color: Theme.of(context).colorScheme.primary),
labelText: widget.label,
suffixIcon: Icon(
CupertinoIcons.phone,
color: Theme.of(context).colorScheme.primary,
),
fillColor: Theme.of(context).colorScheme.primary,
focusColor: Theme.of(context).colorScheme.primary,
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: BorderSide(
color: Theme.of(context).colorScheme.primary)),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: BorderSide(
color: Theme.of(context).colorScheme.primary)),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: BorderSide(
color: Theme.of(context).colorScheme.primary,
width: 2)),
focusedErrorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: BorderSide(
color: widget.success != null
? AppColors.green.defaultShade
: AppColors.red.defaultShade,
width: 2)),
errorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: BorderSide(
color: widget.success != null
? AppColors.green.defaultShade
: AppColors.red.defaultShade,
width: 2)),
),
),
);
});
}
}