add sp and support save the width and height

This commit is contained in:
Siyi Guo committed 2023-04-13 15:21:40 +08:00
1 parent 1c53032b3f
commit 595ee6fd6a
9 files changed
+187 -10

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
mixin Consts {
static const widthKey = 'window.width';
static const heightKey = 'window.height';
}
+1 -5
View File
@@ -28,7 +28,6 @@ class _ImagePageState extends State<ImagePage> with WindowListener {
double offsetDy1 = 0;
double offsetDx2 = 0;
double offsetDy2 = 0;
var windowFocusNode = FocusNode(canRequestFocus: false);
@override
Widget build(BuildContext context) {
@@ -84,7 +83,6 @@ class _ImagePageState extends State<ImagePage> with WindowListener {
moveChanged: (dx, dy) {
offsetDx1 = dx;
offsetDy1 = dy;
// setState(() {});
},
dx: offsetDx1,
dy: offsetDy1,
@@ -97,8 +95,6 @@ class _ImagePageState extends State<ImagePage> with WindowListener {
moveChanged: (dx, dy) {
offsetDx2 = dx;
offsetDy2 = dy;
windowFocusNode.unfocus();
// setState(() {});
},
dx: offsetDx2,
dy: offsetDy2,
@@ -119,10 +115,10 @@ class _ImagePageState extends State<ImagePage> with WindowListener {
double dy = 0,
Function(double dx, double dy)? moveChanged,
}) {
var width = MediaQuery.of(context).size.width - 100;
if (!show || imageFile == null) {
return const SizedBox();
}
var width = MediaQuery.of(context).size.width - 100;
return DraggableArea(
key: ValueKey('${DateTime.now()}'),
dx: dx,
+20
View File
@@ -0,0 +1,20 @@
import 'package:px_eye/const/const.dart';
import 'package:px_eye/extension/extension.dart';
import 'package:px_eye/sp/sp.dart';
import 'package:window_manager/window_manager.dart';
class WindowListenerImp extends WindowListener {
@override
void onWindowResized() {
super.onWindowResized();
saveWindowSize();
}
void saveWindowSize() async {
var size = await windowManager.getSize();
screenSize = size;
SP.setDouble(Consts.widthKey, size.width);
SP.setDouble(Consts.heightKey, size.height);
}
}
+23 -5
View File
@@ -1,7 +1,10 @@
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:px_eye/const/const.dart';
import 'package:px_eye/image/image_page.dart';
import 'package:px_eye/interface/window_interface.dart';
import 'package:px_eye/sp/sp.dart';
import 'package:px_eye/widget/button.dart';
import 'package:window_manager/window_manager.dart';
@@ -9,17 +12,25 @@ import 'extension/extension.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await SP.init();
var width = SP.getDouble(Consts.widthKey, 800);
var height = SP.getDouble(Consts.heightKey, 1200);
// Must add this line.
await windowManager.ensureInitialized();
WindowOptions windowOptions = const WindowOptions(
size: Size(800, 1600),
WindowOptions windowOptions = WindowOptions(
size: Size(width, height),
);
windowManager.waitUntilReadyToShow(windowOptions, () async {
await windowManager.show();
await windowManager.focus();
});
windowManager.addListener(WindowListenerImp());
runApp(const MyApp());
}
@@ -57,7 +68,7 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> {
int width = 800;
int height = 1600;
int height = 1200;
Future<void> onClickEnter() async {
resetScreen();
@@ -69,10 +80,17 @@ class _MyHomePageState extends State<MyHomePage> {
));
}
@override
initState() {
super.initState();
width = SP.getDouble(Consts.widthKey, 800).toInt();
height = SP.getDouble(Consts.heightKey, 1200).toInt();
setState(() {});
}
Future<void> resetScreen() async {
var size = Size(width.toDouble(), height.toDouble());
Offset newPosition = await calcWindowPosition(size, Alignment.center);
await windowManager.setBounds(null, position: newPosition, size: size, animate: true);
await windowManager.setBounds(null, position: null, size: size, animate: true);
setState(() {});
}
+17
View File
@@ -0,0 +1,17 @@
import 'package:shared_preferences/shared_preferences.dart';
class SP {
static late SharedPreferences prefs;
static Future<void> init() async {
prefs = await SharedPreferences.getInstance();
}
static double getDouble(String key, [double def = 0]) {
return prefs.getDouble(key) ?? def;
}
static Future<bool> setDouble(String key, double value) {
return prefs.setDouble(key, value);
}
}