senior_platform_authentication_ui 9.0.0 senior_platform_authentication_ui: ^9.0.0 copied to clipboard
A package that make it easy to implement the Senior X authentication for Flutter. Built to be used with the bloc state management package.
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:senior_authentication_ui_example/login_screen.dart';
import 'package:senior_design_system/senior_design_system.dart';
import 'package:senior_platform_authentication_ui/senior_platform_authentication_ui.dart';
import 'home_screen.dart';
import 'splash_screen.dart';
void main() {
SeniorAuthentication.initialize(
automaticLogon: true,
enableLoginOffline: true,
restUrl: 'https://cloud-leaf.senior.com.br/t/senior.com.br/bridge/1.0/rest',
platformEnvironment: PlatformEnvironment.cloudLeaf,
enableBiometry: true,
enableBiometryOnly: false,
);
runApp(const ExampleApp());
}
class ExampleApp extends StatefulWidget {
const ExampleApp({super.key});
@override
State<ExampleApp> createState() => _ExampleAppState();
}
class _ExampleAppState extends State<ExampleApp> {
@override
Widget build(BuildContext context) {
return SeniorDesignSystem(
child: BlocProvider(
create: (context) => AuthenticationBloc()
..add(
CheckAuthenticationRequested(
username: AuthenticationBloc().state.username,
),
),
child: const AppView(),
),
);
}
}
class AppView extends StatefulWidget {
const AppView({super.key});
@override
State<AppView> createState() => _AppViewState();
}
class _AppViewState extends State<AppView> with WidgetsBindingObserver {
final _navigatorKey = GlobalKey<NavigatorState>();
bool isBiometricRequested = false;
bool isHidden = false;
DateTime? backgroundTime;
@override
void initState() {
WidgetsBinding.instance.addObserver(this);
super.initState();
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) async {
//Example for when the application goes to background you need to request biometrics again
if (isHidden && state == AppLifecycleState.resumed) {
final authenticationBloc = context.read<AuthenticationBloc>();
if (authenticationBloc.state.status ==
AuthenticationStatus.authenticated) {
if (backgroundTime != null) {
// Check the elapsed time when returning to the foreground.
final currentTime = DateTime.now();
final elapsedDuration = currentTime.difference(backgroundTime!);
if (elapsedDuration.inMinutes > 3) {
// If the elapsed time is greater than 2 minutes, request biometrics.
authenticationBloc.add(
CheckAuthenticationRequested(
username: authenticationBloc.state.username,
),
);
}
}
}
isHidden = false;
}
if (!isHidden) {
isHidden = state == AppLifecycleState.hidden;
if (isHidden) {
// Registre a hora em que o aplicativo vai para background.
backgroundTime = DateTime.now();
}
}
}
NavigatorState get _navigator => _navigatorKey.currentState!;
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: _navigatorKey,
debugShowCheckedModeBanner: false,
builder: (context, child) {
return BlocListener<AuthenticationBloc, AuthenticationState>(
listener: (context, state) {
switch (state.status) {
case AuthenticationStatus.authenticated:
_navigator.pushAndRemoveUntil<void>(
MaterialPageRoute<void>(builder: (_) {
return const HomeScreen();
}),
(route) => false,
);
break;
case AuthenticationStatus.unauthenticated:
_navigator.pushAndRemoveUntil<void>(
MaterialPageRoute<void>(
builder: (_) => const LoginScreen(),
),
(route) => false,
);
break;
case AuthenticationStatus.unknown:
break;
case AuthenticationStatus.offline:
_navigator.pushAndRemoveUntil<void>(
MaterialPageRoute<void>(builder: (_) => const HomeScreen()),
(route) => false,
);
break;
}
},
child: child,
);
},
onGenerateRoute: (_) => SplashScreen.route(),
);
}
Widget blockWidget() {
return BackdropFilter(
filter: ImageFilter.blur(
sigmaX: 15.0,
sigmaY: 15.0,
),
child: Container(
decoration: BoxDecoration(
color: Colors.deepPurpleAccent.shade200.withOpacity(.5),
),
),
);
}
}