app/lib/common/utils/base_utils.dart

144 lines
4.1 KiB
Dart
Raw Permalink Normal View History

2024-03-01 20:59:43 +08:00
import 'dart:async';
2023-10-09 09:32:07 +08:00
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter/rendering.dart';
import 'dart:ui' as ui;
2023-11-21 01:05:20 +08:00
import 'package:flutter/services.dart';
2024-03-17 16:54:09 +08:00
import 'package:starcitizen_doctor/generated/l10n.dart';
2023-11-21 01:05:20 +08:00
2023-10-09 09:32:07 +08:00
Future showToast(BuildContext context, String msg,
2024-01-07 18:52:06 +08:00
{BoxConstraints? constraints, String? title}) async {
2023-10-09 09:32:07 +08:00
return showBaseDialog(context,
2024-03-17 16:54:09 +08:00
title: title ?? S.current.app_common_tip,
2023-10-09 09:32:07 +08:00
content: Text(msg),
actions: [
FilledButton(
2024-03-17 16:54:09 +08:00
child: Padding(
padding:
const EdgeInsets.only(top: 2, bottom: 2, left: 8, right: 8),
child: Text(S.current.app_common_tip_i_know),
2023-10-09 09:32:07 +08:00
),
onPressed: () => Navigator.pop(context),
),
],
constraints: constraints);
}
Future<bool> showConfirmDialogs(
BuildContext context, String title, Widget content,
2024-03-17 16:54:09 +08:00
{String confirm = "",
String cancel = "",
2023-10-09 09:32:07 +08:00
BoxConstraints? constraints}) async {
2024-03-17 16:54:09 +08:00
if (confirm.isEmpty) confirm = S.current.app_common_tip_confirm;
2024-03-23 18:23:16 +08:00
if (cancel.isEmpty) cancel = S.current.app_common_tip_cancel;
2024-03-17 16:54:09 +08:00
2023-10-09 09:32:07 +08:00
final r = await showBaseDialog(context,
title: title,
content: content,
actions: [
if (confirm.isNotEmpty)
FilledButton(
child: Padding(
padding:
const EdgeInsets.only(top: 2, bottom: 2, left: 8, right: 8),
child: Text(confirm),
),
onPressed: () => Navigator.pop(context, true),
),
if (cancel.isNotEmpty)
Button(
child: Padding(
padding:
const EdgeInsets.only(top: 2, bottom: 2, left: 8, right: 8),
child: Text(cancel),
),
onPressed: () => Navigator.pop(context, false),
),
],
constraints: constraints);
return r == true;
}
2023-11-21 01:05:20 +08:00
Future<String?> showInputDialogs(BuildContext context,
{required String title,
required String content,
BoxConstraints? constraints,
String? initialValue,
List<TextInputFormatter>? inputFormatters}) async {
String? userInput;
constraints ??=
BoxConstraints(maxWidth: MediaQuery.of(context).size.width * .38);
final ok = await showConfirmDialogs(
context,
title,
Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2023-11-22 00:40:36 +08:00
if (content.isNotEmpty)
Text(
content,
style: TextStyle(color: Colors.white.withOpacity(.6)),
),
const SizedBox(height: 8),
2023-11-21 01:05:20 +08:00
TextFormBox(
initialValue: initialValue,
onChanged: (str) {
userInput = str;
},
inputFormatters: inputFormatters,
),
],
),
constraints: constraints);
if (ok == true) return userInput;
return null;
}
2023-10-09 09:32:07 +08:00
Future showBaseDialog(BuildContext context,
{required String title,
required Widget content,
List<Widget>? actions,
BoxConstraints? constraints}) async {
return await showDialog(
context: context,
builder: (context) => ContentDialog(
title: Text(title),
content: content,
2024-02-06 20:19:53 +08:00
constraints: constraints ??
const BoxConstraints(
maxWidth: 512,
maxHeight: 756.0,
),
2023-10-09 09:32:07 +08:00
actions: actions,
),
);
}
bool stringIsNotEmpty(String? s) {
return s != null && (s.isNotEmpty);
}
Future<Uint8List?> widgetToPngImage(GlobalKey repaintBoundaryKey,
{double pixelRatio = 3.0}) async {
RenderRepaintBoundary? boundary = repaintBoundaryKey.currentContext
?.findRenderObject() as RenderRepaintBoundary?;
if (boundary == null) return null;
ui.Image image = await boundary.toImage(pixelRatio: pixelRatio);
ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
if (byteData == null) return null;
var pngBytes = byteData.buffer.asUint8List();
return pngBytes;
}
double roundDoubleTo(double value, double precision) =>
(value * precision).round() / precision;
2024-03-31 15:05:06 +08:00
int getMinNumber(List<int> list) {
if (list.isEmpty) return 0;
list.sort((a, b) => a.compareTo(b));
return list.first;
}