Skip to content

Commit

Permalink
Merge pull request #100 from appKom/native-alerts
Browse files Browse the repository at this point in the history
Native alerts
  • Loading branch information
johanneskhage authored Oct 20, 2024
2 parents 08fa857 + aa48d82 commit 33f7ea7
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 85 deletions.
57 changes: 15 additions & 42 deletions lib/pages/event/cards/event_card_buttons.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_platform_alert/flutter_platform_alert.dart';
import 'package:go_router/go_router.dart';
import 'package:http/http.dart' as http;
import 'package:native_ios_dialog/native_ios_dialog.dart';
Expand Down Expand Up @@ -158,48 +159,20 @@ class _EventCardButtonsState extends State<EventCardButtons> {
);
}

void showUnregisterDialog() {
final bool isIOS = Platform.isIOS;

if (isIOS) {
NativeIosDialog(title: 'Bekreft avmelding', actions: [
NativeIosDialogAction(
text: 'Avbryt',
style: NativeIosDialogActionStyle.cancel,
onPressed: () {},
),
NativeIosDialogAction(
text: 'Bekreft',
style: NativeIosDialogActionStyle.destructive,
onPressed: () {
unregisterForEvent(widget.model.id, context);
},
),
]).show();
} else {
// TODO: Implement Android dialog
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Bekreft avemelding'),
content: const Text('Er du sikker på at du vil melde av?'),
actions: [
TextButton(
onPressed: () => rootNavigator.currentState!.pop(),
child: Text('Avbryt'),
),
TextButton(
child: const Text('Meld av'),
onPressed: () {
rootNavigator.currentState!.pop();
unregisterForEvent(widget.model.id, context);
},
),
],
);
},
);
Future<void> showUnregisterDialog() async {
final result = await FlutterPlatformAlert.showCustomAlert(
windowTitle: 'Bekreft avmelding',
text: 'Er du sikker på at du vil melde deg av?',
iconStyle: IconStyle.warning,
negativeButtonTitle: 'Meld av',
neutralButtonTitle: 'Avbryt',
options: PlatformAlertOptions(
windows: WindowsAlertOptions(preferMessageBox: true),
),
);

if (result == CustomButton.negativeButton) {
unregisterForEvent(widget.model.id, context);
}
}

Expand Down
58 changes: 15 additions & 43 deletions lib/pages/menu/menu_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:native_ios_dialog/native_ios_dialog.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:flutter_platform_alert/flutter_platform_alert.dart';

import '/components/animated_button.dart';
import '/components/online_scaffold.dart';
Expand Down Expand Up @@ -133,52 +134,23 @@ class MenuPageState extends State<MenuPage> {
database = Databases(client);
}

void initiateDeletion(BuildContext context) {
Future<void> initiateDeletion(BuildContext context) async {
final bool isIOS = Theme.of(context).platform == TargetPlatform.iOS;

if (isIOS) {
cupertionDeleteDialog();
} else {
showDialog(context: context, builder: (context) => materialDeleteDialog());
}
}

void cupertionDeleteDialog() {
NativeIosDialog(
title: 'Bekreft sletting',
message: 'Er du sikker på at du vil slette brukerdataene dine?',
actions: [
NativeIosDialogAction(
text: 'Avbryt',
style: NativeIosDialogActionStyle.cancel,
onPressed: () {},
),
NativeIosDialogAction(
text: 'Slett',
style: NativeIosDialogActionStyle.destructive,
onPressed: deleteUserData,
),
],
).show();
}

Widget materialDeleteDialog() {
return AlertDialog(
title: const Text('Bekreft sletting'),
content: const Text('Er du sikker på at du vil slette brukerdataene dine?'),
actions: [
TextButton(
onPressed: () {
AppNavigator.pop();
},
child: const Text('Avbryt'),
),
TextButton(
onPressed: deleteUserData,
child: const Text('Slett'),
),
],
final result = await FlutterPlatformAlert.showCustomAlert(
windowTitle: 'Bekreft sletting',
text: 'Er du sikker på at du vil slette brukerdataene dine?',
iconStyle: IconStyle.warning,
negativeButtonTitle: 'Slett',
neutralButtonTitle: 'Avbryt',
options: PlatformAlertOptions(
windows: WindowsAlertOptions(preferMessageBox: true),
),
);

if (result == CustomButton.negativeButton) {
deleteUserData();
}
}

void deleteUserData() {
Expand Down
4 changes: 4 additions & 0 deletions linux/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "generated_plugin_registrant.h"

#include <file_selector_linux/file_selector_plugin.h>
#include <flutter_platform_alert/flutter_platform_alert_plugin.h>
#include <flutter_secure_storage_linux/flutter_secure_storage_linux_plugin.h>
#include <url_launcher_linux/url_launcher_plugin.h>
#include <window_to_front/window_to_front_plugin.h>
Expand All @@ -15,6 +16,9 @@ void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) file_selector_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FileSelectorPlugin");
file_selector_plugin_register_with_registrar(file_selector_linux_registrar);
g_autoptr(FlPluginRegistrar) flutter_platform_alert_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterPlatformAlertPlugin");
flutter_platform_alert_plugin_register_with_registrar(flutter_platform_alert_registrar);
g_autoptr(FlPluginRegistrar) flutter_secure_storage_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterSecureStorageLinuxPlugin");
flutter_secure_storage_linux_plugin_register_with_registrar(flutter_secure_storage_linux_registrar);
Expand Down
1 change: 1 addition & 0 deletions linux/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

list(APPEND FLUTTER_PLUGIN_LIST
file_selector_linux
flutter_platform_alert
flutter_secure_storage_linux
url_launcher_linux
window_to_front
Expand Down
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import firebase_core
import firebase_messaging
import flutter_inappwebview_macos
import flutter_local_notifications
import flutter_platform_alert
import flutter_secure_storage_macos
import flutter_web_auth_2
import native_ios_dialog
Expand All @@ -32,6 +33,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FLTFirebaseMessagingPlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseMessagingPlugin"))
InAppWebViewFlutterPlugin.register(with: registry.registrar(forPlugin: "InAppWebViewFlutterPlugin"))
FlutterLocalNotificationsPlugin.register(with: registry.registrar(forPlugin: "FlutterLocalNotificationsPlugin"))
FlutterPlatformAlertPlugin.register(with: registry.registrar(forPlugin: "FlutterPlatformAlertPlugin"))
FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin"))
FlutterWebAuth2Plugin.register(with: registry.registrar(forPlugin: "FlutterWebAuth2Plugin"))
NativeIosDialogPlugin.register(with: registry.registrar(forPlugin: "NativeIosDialogPlugin"))
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.7.3+2"
flutter_platform_alert:
dependency: "direct main"
description:
name: flutter_platform_alert
sha256: "29a27c81660468bfb7746bc78205f79e07f18ca785e0aeaf70eac28d7a011edb"
url: "https://pub.dev"
source: hosted
version: "0.6.1"
flutter_plugin_android_lifecycle:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies:
swipeable_page_route: ^0.4.2
native_ios_dialog: ^0.2.1
go_router: ^14.3.0
flutter_platform_alert: ^0.6.1

dev_dependencies:
flutter_test:
Expand Down
3 changes: 3 additions & 0 deletions windows/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <file_selector_windows/file_selector_windows.h>
#include <firebase_core/firebase_core_plugin_c_api.h>
#include <flutter_inappwebview_windows/flutter_inappwebview_windows_plugin_c_api.h>
#include <flutter_platform_alert/flutter_platform_alert_plugin.h>
#include <flutter_secure_storage_windows/flutter_secure_storage_windows_plugin.h>
#include <url_launcher_windows/url_launcher_windows.h>
#include <window_to_front/window_to_front_plugin.h>
Expand All @@ -20,6 +21,8 @@ void RegisterPlugins(flutter::PluginRegistry* registry) {
registry->GetRegistrarForPlugin("FirebaseCorePluginCApi"));
FlutterInappwebviewWindowsPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FlutterInappwebviewWindowsPluginCApi"));
FlutterPlatformAlertPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FlutterPlatformAlertPlugin"));
FlutterSecureStorageWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FlutterSecureStorageWindowsPlugin"));
UrlLauncherWindowsRegisterWithRegistrar(
Expand Down
1 change: 1 addition & 0 deletions windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
file_selector_windows
firebase_core
flutter_inappwebview_windows
flutter_platform_alert
flutter_secure_storage_windows
url_launcher_windows
window_to_front
Expand Down

0 comments on commit 33f7ea7

Please sign in to comment.