This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import '../../../../../application/auth/check_phone_form/check_phone_form_bloc.dart';
|
||||
@@ -36,6 +37,9 @@ class _LoginPhoneFieldState extends State<LoginPhoneField> {
|
||||
|
||||
void _clearText() {
|
||||
_controller.clear();
|
||||
context.read<CheckPhoneFormBloc>().add(
|
||||
const CheckPhoneFormEvent.phoneNumberChanged(''),
|
||||
);
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
@@ -44,13 +48,19 @@ class _LoginPhoneFieldState extends State<LoginPhoneField> {
|
||||
return AppTextFormField(
|
||||
title: 'Masukkan no telepon',
|
||||
hintText: '8712671212',
|
||||
helperText: 'Harus diawali angka 8',
|
||||
controller: _controller,
|
||||
focusNode: _focusNode,
|
||||
keyboardType: TextInputType.phone,
|
||||
inputFormatters: [_PhoneAfterCountryCodeFormatter()],
|
||||
validator: (value) {
|
||||
if (context.read<CheckPhoneFormBloc>().state.phoneNumber.isEmpty) {
|
||||
final phoneNumber = context.read<CheckPhoneFormBloc>().state.phoneNumber;
|
||||
if (phoneNumber.isEmpty) {
|
||||
return 'Masukkan no telepon';
|
||||
}
|
||||
if (!phoneNumber.startsWith('8')) {
|
||||
return 'No telepon harus diawali angka 8';
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
@@ -80,3 +90,27 @@ class _LoginPhoneFieldState extends State<LoginPhoneField> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Prefix +62 sudah tampil di field, jadi input harus langsung diawali 8.
|
||||
/// Ketikan `0`/`6` di awal ditolak; tempelan `08…`, `62…`, atau `+62…`
|
||||
/// dirapikan jadi `8…`.
|
||||
class _PhoneAfterCountryCodeFormatter extends TextInputFormatter {
|
||||
@override
|
||||
TextEditingValue formatEditUpdate(
|
||||
TextEditingValue oldValue,
|
||||
TextEditingValue newValue,
|
||||
) {
|
||||
var digits = newValue.text.replaceAll(RegExp(r'\D'), '');
|
||||
if (digits.startsWith('62')) digits = digits.substring(2);
|
||||
digits = digits.replaceFirst(RegExp(r'^0+'), '');
|
||||
|
||||
if (digits.isEmpty) return const TextEditingValue();
|
||||
if (!digits.startsWith('8')) return oldValue;
|
||||
if (digits == newValue.text) return newValue;
|
||||
|
||||
return TextEditingValue(
|
||||
text: digits,
|
||||
selection: TextSelection.collapsed(offset: digits.length),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user