import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import '../theme/theme.dart'; import 'modal_presentation.dart'; /// Confirms a destructive action using a UIKit alert on iOS. /// Returns true only when the person explicitly chooses the destructive action. Future showDestructiveConfirmation({ required BuildContext context, required String title, required String message, required String confirmLabel, }) => showConfirmation( context: context, title: title, message: message, confirmLabel: confirmLabel, destructive: true, ); /// Confirms an action using a UIKit alert on iOS. /// Returns true only when the person explicitly chooses the confirm action. Future showConfirmation({ required BuildContext context, required String title, required String message, required String confirmLabel, bool destructive = false, }) async { if (!kIsWeb && defaultTargetPlatform == TargetPlatform.iOS) { return await const MethodChannel( 'buzz/confirmation_dialog', ).invokeMethod('present', { 'title': title, 'message': message, 'confirmLabel': confirmLabel, 'cancelLabel': 'Cancel', 'destructive': destructive, 'dark': context.theme.brightness == Brightness.dark, }) == true; } return await showBuzzDialog( context: context, builder: (dialogContext) => AlertDialog.adaptive( title: Text(title), content: Text(message), actions: [ TextButton( onPressed: () => Navigator.of(dialogContext).pop(false), child: const Text('Cancel'), ), FilledButton( onPressed: () => Navigator.of(dialogContext).pop(true), style: destructive ? FilledButton.styleFrom( backgroundColor: context.colors.error, ) : null, child: Text(confirmLabel), ), ], ), ) == true; } /// Asks whether the people being added to a DM see its history, using a UIKit /// alert on iOS. Returns true for Include history, false for Start a new DM, and /// null when cancelled. Future showDmHistoryChoice({ required BuildContext context, required String title, }) async { const message = 'Include history to add them to this DM with its earlier messages.'; const options = ['Start a new DM', 'Include history']; if (!kIsWeb && defaultTargetPlatform == TargetPlatform.iOS) { final index = await const MethodChannel('buzz/confirmation_dialog') .invokeMethod('choose', { 'title': title, 'message': message, 'options': options, 'cancelLabel': 'Cancel', 'dark': context.theme.brightness == Brightness.dark, }); return index == null ? null : index == 1; } return showBuzzDialog( context: context, builder: (dialogContext) => AlertDialog.adaptive( title: Text(title), content: const Text(message), actions: [ TextButton( onPressed: () => Navigator.of(dialogContext).pop(), child: const Text('Cancel'), ), TextButton( onPressed: () => Navigator.of(dialogContext).pop(false), child: Text(options[0]), ), FilledButton( onPressed: () => Navigator.of(dialogContext).pop(true), child: Text(options[1]), ), ], ), ); }