mirror of
https://mirror.ghproxy.com/https://github.com/StarCitizenToolBox/app.git
synced 2024-12-23 20:33:42 +08:00
133 lines
3.8 KiB
Dart
133 lines
3.8 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:fluent_ui/fluent_ui.dart';
|
|
import 'package:flutter/rendering.dart';
|
|
import 'dart:ui' as ui;
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
Future showToast(BuildContext context, String msg,
|
|
{BoxConstraints? constraints, String? title}) async {
|
|
return showBaseDialog(context,
|
|
title: title ?? "提示",
|
|
content: Text(msg),
|
|
actions: [
|
|
FilledButton(
|
|
child: const Padding(
|
|
padding: EdgeInsets.only(top: 2, bottom: 2, left: 8, right: 8),
|
|
child: Text('我知道了'),
|
|
),
|
|
onPressed: () => Navigator.pop(context),
|
|
),
|
|
],
|
|
constraints: constraints);
|
|
}
|
|
|
|
Future<bool> showConfirmDialogs(
|
|
BuildContext context, String title, Widget content,
|
|
{String confirm = "确认",
|
|
String cancel = "取消",
|
|
BoxConstraints? constraints}) async {
|
|
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;
|
|
}
|
|
|
|
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: [
|
|
if (content.isNotEmpty)
|
|
Text(
|
|
content,
|
|
style: TextStyle(color: Colors.white.withOpacity(.6)),
|
|
),
|
|
const SizedBox(height: 8),
|
|
TextFormBox(
|
|
initialValue: initialValue,
|
|
onChanged: (str) {
|
|
userInput = str;
|
|
},
|
|
inputFormatters: inputFormatters,
|
|
),
|
|
],
|
|
),
|
|
constraints: constraints);
|
|
if (ok == true) return userInput;
|
|
return null;
|
|
}
|
|
|
|
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,
|
|
constraints: constraints ??
|
|
const BoxConstraints(
|
|
maxWidth: 512,
|
|
maxHeight: 756.0,
|
|
),
|
|
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;
|