flutter_login_widget/lib/flutter_login_view.dart

169 lines
4.7 KiB
Dart
Raw Normal View History

2022-09-20 16:32:46 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_login/backend/login_repository.dart';
import '../default_translation.dart';
import '../plugins/login/login_email_password.dart';
import 'login_config.dart';
import 'plugins/login/choose_login.dart';
export '../plugins/form/form.dart';
export '../plugins/login/email_password_form.dart';
export '../plugins/login/login_email_password.dart';
export 'buttons.dart';
export 'login_config.dart';
2022-09-20 16:57:03 +02:00
export 'model/login_confirmation_result.dart';
export 'model/login_user.dart';
2022-09-20 16:32:46 +02:00
export 'plugins/settings/control.dart' show Control;
2022-09-21 14:58:46 +02:00
class FlutterLogin extends InheritedWidget {
const FlutterLogin({
2022-09-20 16:32:46 +02:00
required this.config,
required this.repository,
required Widget child,
required this.app,
Key? key,
}) : super(key: key, child: child);
static Function(Object?) logError = (error) {};
static Map<String, Map<String, String>> get defaultTranslations =>
defaultTranslation;
final ConfigData config;
final LoginRepository repository;
final Widget app;
static FlutterLogin of(BuildContext context) {
2022-09-21 14:58:46 +02:00
var inheritedLogin =
2022-09-20 16:32:46 +02:00
context.dependOnInheritedWidgetOfExactType<FlutterLogin>();
2022-09-21 14:58:46 +02:00
if (inheritedLogin == null) {
2022-09-20 16:32:46 +02:00
throw FlutterError(
'You are retrieving an flutter login from a context that does not contain an flutter login. Make sure you keep the flutter login in your inheritence tree',
);
}
2022-09-21 14:58:46 +02:00
return inheritedLogin;
2022-09-20 16:32:46 +02:00
}
@override
bool updateShouldNotify(FlutterLogin oldWidget) => config != oldWidget.config;
}
2022-09-21 14:58:46 +02:00
extension LoginRetrieval on BuildContext {
2022-09-20 16:32:46 +02:00
static LoginRepository? _cachedBackend;
2022-09-21 10:03:08 +02:00
FlutterLogin login() => FlutterLogin.of(this);
LoginRepository loginRepository() {
_cachedBackend ??= login().repository;
2022-09-20 16:32:46 +02:00
return _cachedBackend!;
}
}
extension StringFormat on String {
String _interpolate(String string, List<dynamic> params) {
var result = string;
for (var i = 1; i < params.length + 1; i++) {
result = result.replaceAll('%$i\$', params[i - 1]?.toString() ?? '');
}
return result;
}
String format(List<dynamic> params) => _interpolate(this, params);
}
2022-09-21 14:58:46 +02:00
extension LoginTranslate on BuildContext {
2022-09-20 16:32:46 +02:00
String? _getDefaultTranslation(String key, List arguments) {
var locale = Localizations.localeOf(this);
var code = locale.countryCode ?? 'nl';
var translationMap = FlutterLogin.defaultTranslations[code] ??
FlutterLogin.defaultTranslations['nl'];
return translationMap?[key]?.toString().format(arguments);
}
2022-09-21 14:58:46 +02:00
String translate(
String key, {
String? defaultValue,
List<dynamic> arguments = const [],
}) {
2022-09-21 10:03:08 +02:00
dynamic translateFunction = login().config.translate;
2022-09-20 16:32:46 +02:00
if (translateFunction == null) {
return _getDefaultTranslation(key, arguments) ?? defaultValue ?? key;
}
return translateFunction(
this,
key,
defaultvalue: defaultValue,
);
}
}
2022-09-21 14:58:46 +02:00
class LoginMain extends StatefulWidget {
const LoginMain({
2022-09-20 16:32:46 +02:00
required this.child,
super.key,
});
final Widget child;
2022-09-21 14:58:46 +02:00
@override
State<LoginMain> createState() => _LoginMainState();
}
class _LoginMainState extends State<LoginMain> {
bool _checkedIfLoggedIn = false;
bool _isLoggedIn = false;
2022-09-20 16:32:46 +02:00
@override
2022-09-21 14:58:46 +02:00
Widget build(BuildContext context) {
if (!_checkedIfLoggedIn) {
context.loginRepository().reLogin(
onLoggedIn: () => setState(
() {
_isLoggedIn = true;
},
),
);
_checkedIfLoggedIn = true;
}
return _isLoggedIn
? widget.child
: Builder(
builder: (context) => FlutterLogin.of(context)
.config
.loginOptions
.loginMethod
.contains(LoginMethod.LoginInteractiveWithSocial) ||
FlutterLogin.of(context)
.config
.loginOptions
.loginMethod
.contains(LoginMethod.LoginInteractiveWithPhoneNumber)
? ChooseLogin(
child: widget.child,
)
: EmailPasswordLogin(
onPressedForgotPassword: FlutterLogin.of(context)
.config
.loginOptions
.onPressForgotPassword,
child: widget.child,
),
);
}
2022-09-20 16:32:46 +02:00
}
2022-09-21 14:58:46 +02:00
class LoginException implements Exception {
LoginException(
2022-09-20 16:32:46 +02:00
this.error, [
this.stackTrace = StackTrace.empty,
]);
/// The unhandled [error] object.
final Object error;
final StackTrace stackTrace;
@override
String toString() {
2022-09-21 14:58:46 +02:00
return 'Unhandled error occurred in login: $error\n'
2022-09-20 16:32:46 +02:00
'$stackTrace';
}
}