
import 'package:flutter/material.dart';

import 'package:pweb/generated/i18n/app_localizations.dart';

//TODO check with /widgets/username.dart

class EmailField extends StatelessWidget {
  final TextEditingController controller;

  const EmailField({super.key, required this.controller});

  static final _emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      controller: controller,
      keyboardType: TextInputType.emailAddress,
      decoration: InputDecoration(
        labelText: AppLocalizations.of(context)!.username,
        hintText: AppLocalizations.of(context)!.usernameHint,
      ),
      validator: (value) {
        if (value == null || !_emailRegex.hasMatch(value)) {
          return AppLocalizations.of(context)!.usernameErrorInvalid;
        }
        return null;
      },
    );
  }
}