ba75ce8d6d
- removed obsolete deps - fixed many issues - improved features - added: * favourites * account console * push notifications * background sync * improved settings
118 lines
3.9 KiB
Dart
118 lines
3.9 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:meincantor/dashboard.dart';
|
|
import 'package:meincantor/login.dart';
|
|
import 'dart:math';
|
|
import 'package:background_fetch/background_fetch.dart';
|
|
|
|
void backgroundFetchHeadlessTask(HeadlessTask task) async {
|
|
String taskId = task.taskId;
|
|
bool isTimeout = task.timeout;
|
|
if (isTimeout) {
|
|
// This task has exceeded its allowed running-time.
|
|
// You must stop what you're doing and immediately .finish(taskId)
|
|
print("[BackgroundFetch] Headless task timed-out: $taskId");
|
|
BackgroundFetch.finish(taskId);
|
|
return;
|
|
}
|
|
print('[BackgroundFetch] Headless event received.');
|
|
// Do your work here...
|
|
BackgroundFetch.finish(taskId);
|
|
}
|
|
|
|
void main() async {
|
|
runApp(const App());
|
|
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
|
|
FlutterLocalNotificationsPlugin();
|
|
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
|
|
const AndroidInitializationSettings initializationSettingsAndroid =
|
|
AndroidInitializationSettings('app_icon');
|
|
const IOSInitializationSettings initializationSettingsIOS =
|
|
IOSInitializationSettings();
|
|
const MacOSInitializationSettings initializationSettingsMacOS =
|
|
MacOSInitializationSettings();
|
|
const InitializationSettings initializationSettings = InitializationSettings(
|
|
android: initializationSettingsAndroid,
|
|
iOS: initializationSettingsIOS,
|
|
macOS: initializationSettingsMacOS);
|
|
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
|
|
BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask);
|
|
}
|
|
|
|
class App extends StatefulWidget {
|
|
const App({Key? key}) : super(key: key);
|
|
@override
|
|
_AppState createState() => _AppState();
|
|
}
|
|
|
|
class _AppState extends State<App> {
|
|
MaterialColor generateMaterialColor(Color color) {
|
|
return MaterialColor(color.value, {
|
|
50: tintColor(color, 0.5),
|
|
100: tintColor(color, 0.4),
|
|
200: tintColor(color, 0.3),
|
|
300: tintColor(color, 0.2),
|
|
400: tintColor(color, 0.1),
|
|
500: tintColor(color, 0),
|
|
600: tintColor(color, -0.1),
|
|
700: tintColor(color, -0.2),
|
|
800: tintColor(color, -0.3),
|
|
900: tintColor(color, -0.4),
|
|
});
|
|
}
|
|
|
|
int tintValue(int value, double factor) =>
|
|
max(0, min((value + ((255 - value) * factor)).round(), 255));
|
|
|
|
Color tintColor(Color color, double factor) => Color.fromRGBO(
|
|
tintValue(color.red, factor),
|
|
tintValue(color.green, factor),
|
|
tintValue(color.blue, factor),
|
|
1);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
theme: ThemeData(
|
|
primaryColor: Palette.primary,
|
|
colorScheme: ColorScheme.fromSwatch(
|
|
primarySwatch: generateMaterialColor(Palette.primary),
|
|
).copyWith(
|
|
secondary: generateMaterialColor(Palette.accent),
|
|
),
|
|
),
|
|
darkTheme: ThemeData.from(
|
|
colorScheme: const ColorScheme.dark(
|
|
primary: Palette.accent, secondary: Palette.accent)),
|
|
title: "GCG.MeinCantor",
|
|
home: buildHomePage(),
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<bool> apiKeyEmpty() async {
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
String? apiKey = prefs.getString("api_key");
|
|
return apiKey == null || apiKey.isEmpty;
|
|
}
|
|
|
|
Widget buildHomePage() {
|
|
return FutureBuilder(
|
|
future: apiKeyEmpty(),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.data == true) {
|
|
return Login();
|
|
} else if (snapshot.data == false) {
|
|
return const Dashboard();
|
|
} else {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
});
|
|
}
|
|
|
|
class Palette {
|
|
static const Color primary = Color(0xFF1A1A37);
|
|
static const Color accent = Color(0xFFFFBC3B);
|
|
}
|