Files
flutter/lib/widgets/auth/api_token_field.dart
Roland Geider ece38a39d2 Refactor login screen
The api token toggle is now only visible when showing a custom server, since at
the moment this is the only time when such an auth method makes sense (plus it
keeps the rest of the UI cleaner). The different fields in the screen have been
moved to individual files, to make the structure clearer.
2025-04-30 22:50:55 +02:00

64 lines
1.8 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
class ApiTokenField extends StatefulWidget {
final TextEditingController controller;
final Function(String?) onSaved;
const ApiTokenField({
required this.controller,
required this.onSaved,
super.key,
});
@override
State<ApiTokenField> createState() => _ApiTokenFieldState();
}
class _ApiTokenFieldState extends State<ApiTokenField> {
bool isObscure = true;
@override
Widget build(BuildContext context) {
final i18n = AppLocalizations.of(context);
return TextFormField(
key: const ValueKey('inputApiToken'),
decoration: InputDecoration(
labelText: i18n.apiToken,
helperText:
'Only needed when using an authentication proxy for the backend, please consult the documentation',
errorMaxLines: 2,
helperMaxLines: 2,
prefixIcon: const Icon(Icons.password),
suffixIcon: IconButton(
icon: Icon(isObscure ? Icons.visibility_off : Icons.visibility),
onPressed: () {
setState(() {
isObscure = !isObscure;
});
},
),
),
obscureText: isObscure,
controller: widget.controller,
textInputAction: TextInputAction.next,
keyboardType: TextInputType.emailAddress,
validator: (value) {
if (value == null || value.isEmpty) {
return i18n.invalidApiToken;
}
if (!RegExp(r'^[a-f0-9]{40}$').hasMatch(value)) {
return i18n.apiTokenValidChars;
}
return null;
},
inputFormatters: [
FilteringTextInputFormatter.deny(RegExp(r'\s\b|\b\s')),
],
onSaved: widget.onSaved,
);
}
}