language localization
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
// network_connectivity.dart
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
@@ -22,13 +20,17 @@ class NetworkConnectivity {
|
||||
void initialize() {
|
||||
_subscription = _connectivity.onConnectivityChanged
|
||||
.listen((List<ConnectivityResult> result) {
|
||||
String status = result.toString();
|
||||
onStatusChange(status);
|
||||
if (result.isNotEmpty) {
|
||||
String status = result.toString();
|
||||
onStatusChange(status);
|
||||
|
||||
if (result[0] == ConnectivityResult.wifi ||
|
||||
result[0] == ConnectivityResult.mobile) {
|
||||
if (goRouter.canPop()) {
|
||||
goRouter.pop(true);
|
||||
if (result[0] == ConnectivityResult.wifi ||
|
||||
result[0] == ConnectivityResult.mobile) {
|
||||
if (goRouter.canPop()) {
|
||||
goRouter.pop(true);
|
||||
}
|
||||
} else {
|
||||
goRouter.pushNamed(RouteName.noInternetScreen);
|
||||
}
|
||||
} else {
|
||||
goRouter.pushNamed(RouteName.noInternetScreen);
|
||||
@@ -42,7 +44,8 @@ class NetworkConnectivity {
|
||||
Future<void> checkInternet() async {
|
||||
final connectivityResult = await _connectivity.checkConnectivity();
|
||||
|
||||
if (connectivityResult[0] != ConnectivityResult.none) {
|
||||
if (connectivityResult.isNotEmpty &&
|
||||
connectivityResult[0] != ConnectivityResult.none) {
|
||||
onStatusChange(connectivityResult.toString());
|
||||
} else {
|
||||
onStatusChange(connectivityResult.toString());
|
||||
|
||||
55
lib/core/utils/language/localizations_delegate.dart
Normal file
55
lib/core/utils/language/localizations_delegate.dart
Normal file
@@ -0,0 +1,55 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
class AppLocalizations {
|
||||
final Locale locale;
|
||||
|
||||
AppLocalizations(this.locale);
|
||||
|
||||
static AppLocalizations of(BuildContext context) {
|
||||
return Localizations.of<AppLocalizations>(context, AppLocalizations)!;
|
||||
}
|
||||
|
||||
static const LocalizationsDelegate<AppLocalizations> delegate =
|
||||
_AppLocalizationsDelegate();
|
||||
|
||||
late Map<String, String> _localizedStrings;
|
||||
|
||||
Future<bool> load() async {
|
||||
String jsonString = await rootBundle
|
||||
.loadString('assets/language/${locale.languageCode}.json');
|
||||
Map<String, dynamic> jsonMap = json.decode(jsonString);
|
||||
|
||||
_localizedStrings = jsonMap.map((key, value) {
|
||||
return MapEntry(key, value.toString());
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
String translate(String key) {
|
||||
return _localizedStrings[key] ?? key;
|
||||
}
|
||||
}
|
||||
|
||||
class _AppLocalizationsDelegate
|
||||
extends LocalizationsDelegate<AppLocalizations> {
|
||||
const _AppLocalizationsDelegate();
|
||||
|
||||
@override
|
||||
bool isSupported(Locale locale) {
|
||||
return ['en', 'ar'].contains(locale.languageCode);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<AppLocalizations> load(Locale locale) async {
|
||||
AppLocalizations localizations = AppLocalizations(locale);
|
||||
await localizations.load();
|
||||
return localizations;
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldReload(_AppLocalizationsDelegate old) => false;
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:local_auth/local_auth.dart';
|
||||
|
||||
@@ -23,13 +25,26 @@ class BiometricBloc extends Bloc<BiometricEvent, BiometricState> {
|
||||
AuthenticateBiometricEvent event, Emitter<BiometricState> emit) async {
|
||||
emit(BiometricAuthenticating());
|
||||
try {
|
||||
bool authenticated = await localAuthentication.authenticate(
|
||||
localizedReason: 'Please authenticate to access the app',
|
||||
options: const AuthenticationOptions(
|
||||
useErrorDialogs: true,
|
||||
stickyAuth: true,
|
||||
),
|
||||
);
|
||||
bool authenticated = false;
|
||||
if (Platform.isIOS) {
|
||||
authenticated = await localAuthentication.authenticate(
|
||||
localizedReason: 'Please authenticate to access the app',
|
||||
options: const AuthenticationOptions(
|
||||
useErrorDialogs: true,
|
||||
stickyAuth: true,
|
||||
biometricOnly: true,
|
||||
),
|
||||
);
|
||||
} else if (Platform.isAndroid) {
|
||||
authenticated = await localAuthentication.authenticate(
|
||||
localizedReason: 'Please authenticate to access the app',
|
||||
options: const AuthenticationOptions(
|
||||
useErrorDialogs: true,
|
||||
stickyAuth: true,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (authenticated) {
|
||||
emit(BiometricAuthenticated());
|
||||
} else {
|
||||
|
||||
@@ -8,10 +8,10 @@ import 'package:flutter_svg/flutter_svg.dart';
|
||||
import '../../../../core/routes/route_name.dart';
|
||||
import '../../../../core/routes/routes.dart';
|
||||
import '../../../../core/styles/app_images.dart';
|
||||
import '../../../../shared/components/device_locked_dialog.dart';
|
||||
import '../bloc/biometric_bloc.dart';
|
||||
import '../bloc/biometric_event.dart';
|
||||
import '../bloc/biometric_state.dart';
|
||||
import '../../../../shared/components/device_locked_dialog.dart';
|
||||
|
||||
class BiometricLayout extends StatelessWidget {
|
||||
const BiometricLayout({super.key});
|
||||
|
||||
@@ -44,14 +44,14 @@ class SplashScreen extends StatelessWidget {
|
||||
// Navigate to the WelcomeScreen using the goRouter
|
||||
goRouter.goNamed(RouteName.welcomeScreen);
|
||||
} else {
|
||||
// if (await secureStorageService.read('biometric') != null &&
|
||||
// await secureStorageService.read('biometric') == 'on') {
|
||||
// goRouter.goNamed(RouteName.biometricScreen);
|
||||
// } else {
|
||||
goRouter.goNamed(RouteName.pinScreen, pathParameters: {
|
||||
"fromScreen": "LoginedInUser",
|
||||
});
|
||||
// }
|
||||
if (await secureStorageService.read('biometric') != null &&
|
||||
await secureStorageService.read('biometric') == 'on') {
|
||||
goRouter.goNamed(RouteName.biometricScreen);
|
||||
} else {
|
||||
goRouter.goNamed(RouteName.pinScreen, pathParameters: {
|
||||
"fromScreen": "LoginedInUser",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||||
import 'package:local_auth/local_auth.dart';
|
||||
|
||||
import 'core/routes/routes.dart';
|
||||
import 'core/utils/connectivity/network_connectivity.dart';
|
||||
import 'core/utils/language/localizations_delegate.dart';
|
||||
import 'features/biometric/presentation/bloc/biometric_bloc.dart';
|
||||
import 'features/biometric/presentation/bloc/biometric_event.dart';
|
||||
import 'features/countrySelection/presentation/bloc/choose_country_bloc.dart';
|
||||
import 'shared/components/bloc/bottom_nav_bar/bottom_navigation_bloc.dart';
|
||||
import 'shared/components/bloc/language/lng_bloc.dart';
|
||||
import 'shared/components/bloc/language/lng_state.dart';
|
||||
|
||||
/* CREATED BY - JAYESH JAIN
|
||||
DATE - 24-05-2024
|
||||
@@ -81,17 +85,43 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
|
||||
BlocProvider(
|
||||
create: (_) =>
|
||||
BiometricBloc(LocalAuthentication())..add(CheckBiometricEvent()),
|
||||
)
|
||||
),
|
||||
BlocProvider(
|
||||
create: (_) => LocalizationBloc(),
|
||||
),
|
||||
],
|
||||
child: ScreenUtilInit(
|
||||
builder: (BuildContext context, Widget? child) => MaterialApp.router(
|
||||
title: 'Tanami Capital',
|
||||
theme: ThemeData(
|
||||
useMaterial3: true,
|
||||
),
|
||||
debugShowCheckedModeBanner: false,
|
||||
routerConfig: goRouter,
|
||||
),
|
||||
builder: (BuildContext context, Widget? child) =>
|
||||
BlocBuilder<LocalizationBloc, LocalizationState>(
|
||||
builder: (context, state) {
|
||||
return MaterialApp.router(
|
||||
title: 'Tanami Capital',
|
||||
locale: state.locale,
|
||||
supportedLocales: const [
|
||||
Locale('en', ''),
|
||||
Locale('ar', ''),
|
||||
],
|
||||
localizationsDelegates: const [
|
||||
AppLocalizations.delegate,
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
localeResolutionCallback: (locale, supportedLocales) {
|
||||
for (var supportedLocale in supportedLocales) {
|
||||
if (supportedLocale.languageCode == locale?.languageCode) {
|
||||
return supportedLocale;
|
||||
}
|
||||
}
|
||||
return supportedLocales.first;
|
||||
},
|
||||
theme: ThemeData(
|
||||
useMaterial3: true,
|
||||
),
|
||||
debugShowCheckedModeBanner: false,
|
||||
routerConfig: goRouter,
|
||||
);
|
||||
}),
|
||||
designSize: const Size(390, 844),
|
||||
),
|
||||
);
|
||||
|
||||
13
lib/shared/components/bloc/language/lng_bloc.dart
Normal file
13
lib/shared/components/bloc/language/lng_bloc.dart
Normal file
@@ -0,0 +1,13 @@
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'lng_event.dart';
|
||||
import 'lng_state.dart';
|
||||
|
||||
class LocalizationBloc extends Bloc<LocalizationEvent, LocalizationState> {
|
||||
LocalizationBloc() : super(const LocalizationState(Locale('en'))) {
|
||||
on<ChangeLanguage>((event, emit) {
|
||||
emit(LocalizationState(event.locale));
|
||||
});
|
||||
}
|
||||
}
|
||||
18
lib/shared/components/bloc/language/lng_event.dart
Normal file
18
lib/shared/components/bloc/language/lng_event.dart
Normal file
@@ -0,0 +1,18 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
abstract class LocalizationEvent extends Equatable {
|
||||
const LocalizationEvent();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class ChangeLanguage extends LocalizationEvent {
|
||||
final Locale locale;
|
||||
|
||||
const ChangeLanguage(this.locale);
|
||||
|
||||
@override
|
||||
List<Object> get props => [locale];
|
||||
}
|
||||
11
lib/shared/components/bloc/language/lng_state.dart
Normal file
11
lib/shared/components/bloc/language/lng_state.dart
Normal file
@@ -0,0 +1,11 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class LocalizationState extends Equatable {
|
||||
final Locale locale;
|
||||
|
||||
const LocalizationState(this.locale);
|
||||
|
||||
@override
|
||||
List<Object> get props => [locale];
|
||||
}
|
||||
Reference in New Issue
Block a user