aman-satu-flutter/lib/main.dart

150 lines
4.7 KiB
Dart

import 'dart:io';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
import 'package:satu/routes/router.dart';
import 'package:satu/shared/app_colors.dart';
import 'package:satu/views/start_up/start_up_view.dart';
import 'package:satu/widgets/dialog/dialog_manager.dart';
import 'core/redux/store.dart';
import 'core/services/dialog_service.dart';
import 'core/services/navigator_service.dart';
import 'core/utils/locator.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
//initialize locator
await LocatorInjector.setupLocator();
HttpOverrides.global = MyHttpOverrides();
LicenseRegistry.addLicense(() async* {
final license = await rootBundle.loadString('assets/google_fonts/OFL.txt');
yield LicenseEntryWithLineBreaks(['google_fonts'], license);
});
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
await messaging();
await crashing();
await Redux.init();
runApp(MainApplication());
}
Future<void> messaging() async {
FirebaseMessaging messaging = FirebaseMessaging.instance;
await messaging.setAutoInitEnabled(true);
NotificationSettings settings = await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
print('User granted permission: ${settings.authorizationStatus}');
await FirebaseMessaging.instance.subscribeToTopic('all');
if (Platform.isAndroid) {
// Android-specific code
await FirebaseMessaging.instance.subscribeToTopic('android');
} else if (Platform.isIOS) {
// iOS-specific code
await FirebaseMessaging.instance.subscribeToTopic('ios');
}
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
if (message.notification != null) {
locator<DialogService>()
.showDialog(description: message.notification?.body ?? '');
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async {
if (message.notification != null) {
locator<DialogService>()
.showDialog(description: message.notification?.body ?? '');
}
});
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
}
Future<void> crashing() async {
FlutterError.onError = (errorDetails) {
FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
};
// Pass all uncaught asynchronous errors that aren't handled by the Flutter framework to Crashlytics
PlatformDispatcher.instance.onError = (error, stack) {
FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
return true;
};
}
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
await Firebase.initializeApp();
}
class MainApplication extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StoreProvider<AppState>(
store: Redux.store!,
child: ScreenUtilInit(
designSize: const Size(
375,
812,
),
builder: (context, child) => MaterialApp(
theme: ThemeData(
backgroundColor: backgroundColor,
primaryColor: whiteColor,
scaffoldBackgroundColor: backgroundColor,
colorScheme:
ColorScheme.fromSwatch().copyWith(secondary: primaryColor),
// textTheme: GoogleFonts.robotoTextTheme(
// Theme.of(context).textTheme,
// )
),
debugShowCheckedModeBanner: false,
builder: (context, child) => Navigator(
key: locator<DialogService>().dialogNavigationKey,
onGenerateRoute: (settings) => MaterialPageRoute(
builder: (context) => DialogManager(child: child!),
),
),
navigatorKey: locator<NavigatorService>().navigatorKey,
home: StartUpView(),
// first page
onGenerateRoute: generateRoute,
),
),
);
}
}
class MyHttpOverrides extends HttpOverrides {
@override
HttpClient createHttpClient(SecurityContext? context) {
return super.createHttpClient(context)
..badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
}
}