asdasd
This commit is contained in:
149
lib/main.dart
Normal file
149
lib/main.dart
Normal file
@ -0,0 +1,149 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:absensi_karyawan/src/services/notification_services.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_background_service/flutter_background_service.dart';
|
||||
import 'package:flutter_background_service_android/flutter_background_service_android.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
import 'package:logger/logger.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'src/config/routes.dart';
|
||||
import 'src/config/theme.dart';
|
||||
import 'src/provider/login_provider.dart';
|
||||
import 'src/services/storage_service.dart';
|
||||
|
||||
import 'package:flutter_native_timezone/flutter_native_timezone.dart';
|
||||
// import 'package:socket_io_client/socket_io_client.dart';
|
||||
import 'package:timezone/data/latest_all.dart' as tz;
|
||||
import 'package:timezone/timezone.dart' as tz;
|
||||
|
||||
final dev = Logger();
|
||||
Future main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
// await GetStorage.init();
|
||||
await dotenv.load(fileName: ".env");
|
||||
StorageService storage = StorageService();
|
||||
await storage.init();
|
||||
// await initializeService();
|
||||
await _configureLocalTimeZone();
|
||||
await NotificationServices.init();
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
Future<void> initializeService() async {
|
||||
final service = FlutterBackgroundService();
|
||||
await service.configure(
|
||||
androidConfiguration: AndroidConfiguration(
|
||||
// this will be executed when app is in foreground or background in separated isolate
|
||||
onStart: onStart,
|
||||
|
||||
// auto start service
|
||||
autoStart: true,
|
||||
isForegroundMode: true,
|
||||
),
|
||||
iosConfiguration: IosConfiguration(
|
||||
// auto start service
|
||||
autoStart: true,
|
||||
|
||||
// this will be executed when app is in foreground in separated isolate
|
||||
onForeground: onStart,
|
||||
|
||||
// you have to enable background fetch capability on xcode project
|
||||
onBackground: onIosBackground,
|
||||
),
|
||||
);
|
||||
service.startService();
|
||||
}
|
||||
|
||||
// to ensure this is executed
|
||||
// run app from xcode, then from xcode menu, select Simulate Background Fetch
|
||||
bool onIosBackground(ServiceInstance service) {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
dev.i('FLUTTER BACKGROUND FETCH');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void onStart(ServiceInstance service) async {
|
||||
// Only available for flutter 3.0.0 and later
|
||||
DartPluginRegistrant.ensureInitialized();
|
||||
|
||||
// For flutter prior to version 3.0.0
|
||||
// We have to register the plugin manually
|
||||
|
||||
if (service is AndroidServiceInstance) {
|
||||
service.on('setAsForeground').listen((event) {
|
||||
service.setAsForegroundService();
|
||||
});
|
||||
|
||||
service.on('setAsBackground').listen((event) {
|
||||
service.setAsBackgroundService();
|
||||
});
|
||||
}
|
||||
|
||||
service.on('stopService').listen((event) {
|
||||
service.stopSelf();
|
||||
});
|
||||
|
||||
// bring to foreground
|
||||
|
||||
Timer.periodic(const Duration(seconds: 5), (timer) async {
|
||||
// NotificationServices.showNotification(
|
||||
// id: 1,
|
||||
// title: 'Percobaan 2',
|
||||
// body: "ini message",
|
||||
// payload: 'Percobaan 2',
|
||||
// );
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _configureLocalTimeZone() async {
|
||||
if (kIsWeb || Platform.isLinux) {
|
||||
return;
|
||||
}
|
||||
tz.initializeTimeZones();
|
||||
final String timeZoneName = await FlutterNativeTimezone.getLocalTimezone();
|
||||
tz.setLocalLocation(tz.getLocation(timeZoneName));
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
// This widget is the root of your application.
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
SystemChrome.setPreferredOrientations([
|
||||
DeviceOrientation.portraitUp,
|
||||
DeviceOrientation.portraitDown,
|
||||
]);
|
||||
|
||||
SystemChrome.setSystemUIOverlayStyle(
|
||||
const SystemUiOverlayStyle(
|
||||
systemNavigationBarColor: ThemeInfo.primary,
|
||||
statusBarColor: ThemeInfo.primary,
|
||||
),
|
||||
);
|
||||
return MultiProvider(
|
||||
providers: [
|
||||
ChangeNotifierProvider<LoginProvider>(
|
||||
create: (_) => LoginProvider(),
|
||||
),
|
||||
],
|
||||
child: MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
title: 'Absensi Karyawan',
|
||||
initialRoute: 'splash',
|
||||
// initialRoute:'prueba',
|
||||
theme: ThemeInfo.getTheme(),
|
||||
routes: RoutesApp.getRoutes(),
|
||||
builder: EasyLoading.init(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user