Shared Preferences implementation
This commit is contained in:
84
lib/app.dart
84
lib/app.dart
@@ -16,6 +16,7 @@ import 'features/service/screens/service_screen.dart';
|
||||
import 'features/dashboard/screens/dashboard_screen.dart';
|
||||
import 'features/auth/screens/mpin_screen.dart';
|
||||
import 'package:local_auth/local_auth.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class KMobile extends StatefulWidget {
|
||||
const KMobile({super.key});
|
||||
@@ -24,18 +25,19 @@ class KMobile extends StatefulWidget {
|
||||
State<KMobile> createState() => _KMobileState();
|
||||
|
||||
static void setLocale(BuildContext context, Locale newLocale) {
|
||||
final _KMobileState? state = context
|
||||
.findAncestorStateOfType<_KMobileState>();
|
||||
final _KMobileState? state = context.findAncestorStateOfType<_KMobileState>();
|
||||
state?.setLocale(newLocale);
|
||||
}
|
||||
}
|
||||
|
||||
class _KMobileState extends State<KMobile> {
|
||||
bool _showSplash = false;
|
||||
Locale? _locale;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadLocale();
|
||||
// Simulate a splash screen delay
|
||||
Future.delayed(const Duration(seconds: 2), () {
|
||||
setState(() {
|
||||
@@ -44,8 +46,17 @@ class _KMobileState extends State<KMobile> {
|
||||
});
|
||||
}
|
||||
|
||||
Locale? _locale;
|
||||
Future<void> _loadLocale() async{
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final String? langCode = prefs.getString('locale');
|
||||
if (langCode != null) {
|
||||
setState(() {
|
||||
_locale = Locale(langCode);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void setLocale(Locale locale) {
|
||||
setState(() {
|
||||
_locale = locale;
|
||||
@@ -101,38 +112,43 @@ class _KMobileState extends State<KMobile> {
|
||||
}
|
||||
*/
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Set status bar color
|
||||
SystemChrome.setSystemUIOverlayStyle(
|
||||
const SystemUiOverlayStyle(
|
||||
statusBarColor: Colors.transparent,
|
||||
statusBarIconBrightness: Brightness.dark,
|
||||
),
|
||||
);
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Set status bar color
|
||||
SystemChrome.setSystemUIOverlayStyle(
|
||||
const SystemUiOverlayStyle(
|
||||
statusBarColor: Colors.transparent,
|
||||
statusBarIconBrightness: Brightness.dark,
|
||||
),
|
||||
);
|
||||
|
||||
return MultiBlocProvider(
|
||||
providers: [BlocProvider<AuthCubit>(create: (_) => getIt<AuthCubit>())],
|
||||
child: MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
locale: _locale, // Use your existing locale variable
|
||||
supportedLocales: const [Locale('en'), Locale('hi')],
|
||||
localizationsDelegates: const [
|
||||
AppLocalizations.delegate,
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
title: 'kMobile',
|
||||
theme: AppThemes.lightTheme,
|
||||
// darkTheme: AppThemes.darkTheme,
|
||||
themeMode: ThemeMode.system, // Use system theme by default
|
||||
onGenerateRoute: AppRoutes.generateRoute,
|
||||
initialRoute: AppRoutes.splash,
|
||||
home: _showSplash ? const SplashScreen() : const AuthGate(),
|
||||
),
|
||||
);
|
||||
}
|
||||
return MultiBlocProvider(
|
||||
providers: [
|
||||
BlocProvider<AuthCubit>(create: (_) => getIt<AuthCubit>()),
|
||||
],
|
||||
child: MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
locale: _locale, // Dynamic locale
|
||||
supportedLocales: const [
|
||||
Locale('en'),
|
||||
Locale('hi'),
|
||||
],
|
||||
localizationsDelegates: const [
|
||||
AppLocalizations.delegate,
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
title: 'kMobile',
|
||||
theme: AppThemes.lightTheme,
|
||||
// darkTheme: AppThemes.darkTheme,
|
||||
themeMode: ThemeMode.system,
|
||||
onGenerateRoute: AppRoutes.generateRoute,
|
||||
initialRoute: AppRoutes.splash,
|
||||
home: _showSplash ? const SplashScreen() : const AuthGate(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class AuthGate extends StatefulWidget {
|
||||
|
Reference in New Issue
Block a user