import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter_web_browser/flutter_web_browser.dart'; import 'package:get/get.dart'; import 'package:isekai_wiki/global.dart'; import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher_string.dart'; import '../styles.dart'; Future alert(BuildContext context, String content, {String? title}) { var c = Completer(); showCupertinoDialog( context: context, builder: (context) => CupertinoAlertDialog( title: title != null ? Text(title) : null, content: Text(content), actions: [ CupertinoDialogAction( isDefaultAction: true, onPressed: () { c.complete(); Navigator.of(context).pop(); }, child: const Text("好"), ), ], ), ); return c.future; } Future confirm(BuildContext context, String content, {String? title, String? positiveText, String? negativeText, bool isDanger = false}) { var c = Completer(); positiveText ??= "好"; negativeText ??= "取消"; showCupertinoDialog( context: context, builder: (context) => CupertinoAlertDialog( title: title != null ? Text(title) : null, content: Text(content), actions: [ CupertinoDialogAction( onPressed: () { Navigator.of(context).pop(); c.complete(false); }, child: Text(negativeText!), ), CupertinoDialogAction( isDestructiveAction: isDanger, isDefaultAction: !isDanger, onPressed: () { Navigator.of(context).pop(); c.complete(true); }, child: Text(positiveText!), ), ], ), ); return c.future; } Future openUrl(String url, {bool inApp = false}) async { if (!kIsWeb && inApp) { // APP部分 if (GetPlatform.isAndroid || GetPlatform.isIOS) { // 安卓和IOS使用应用内浏览器打开 await FlutterWebBrowser.openWebPage( url: url, customTabsOptions: const CustomTabsOptions( defaultColorSchemeParams: CustomTabsColorSchemeParams( toolbarColor: Styles.websiteNavbarColor, ), shareState: CustomTabsShareState.off, showTitle: true, ), safariVCOptions: const SafariViewControllerOptions( barCollapsingEnabled: true, )); return; } } // 其他平台直接调用浏览器打开 launchUrlString(url, mode: LaunchMode.externalApplication); } Future showLoading(BuildContext context, {String? title, required VoidFutureCallback callback}) async { title ??= "加载中"; showCupertinoDialog( context: context, builder: (context) => CupertinoAlertDialog( title: Row( children: [ const CupertinoActivityIndicator(radius: 14), const SizedBox(width: 20), Text(title!), ], ), actions: const [], ), ); try { var ret = await callback.call(); Navigator.of(Get.context!).pop(); return ret; } catch (_) { Navigator.of(Get.context!).pop(); rethrow; } }