Add Pixel eye logic. Can comparison with 2 image and monitor

This commit is contained in:
Siyi Guo committed 2023-04-12 20:13:37 +08:00
1 parent da7cb5ea23
commit e8bd345be6
22 files changed
+993 -73

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
import 'dart:collection';
import 'package:flutter/material.dart';
const designWidth = 360;
Size screenSize = const Size(360, 690);
extension IterableExtension<E> on Iterable<E> {
E? getOrNull(int index) {
if (index >= 0 && index < length) {
return elementAt(index);
}
return null;
}
Queue<E> toQueue() {
return Queue<E>.from(this);
}
}
extension ListExtention<E> on List<E> {
E? get firstOrNull {
Iterator<E> it = iterator;
if (!it.moveNext()) {
return null;
}
return it.current;
}
E? getAllowOverflow(int index) {
if (index >= length) {
return null;
}
return this[index];
}
E? firstOrNullWhere(bool test(E element)) {
for (E element in this) {
if (test(element)) return element;
}
return null;
}
}
extension MapExtention on Map {
dynamic get(dynamic key) {
return this[key];
}
}
extension ScopeFunc<T extends Object> on T {
R let<R>(R Function(T it) op) => op(this);
T also(void Function(T it) op) {
op(this);
return this;
}
}
extension DoubleGap on double {
SizedBox get gapColumn => SizedBox(height: this);
SizedBox get gapRow => SizedBox(width: this);
double get dp => this * screenSize.width / designWidth;
}
extension IntGap on int {
SizedBox get gapColumn => SizedBox(height: toDouble());
Widget gapColumnWithBg(Color color) => Container(color: color, height: toDouble());
SizedBox get gapRow => SizedBox(width: toDouble());
double get dp => this * screenSize.width / designWidth;
}
extension Localization on String {
Text toText({
double? size,
Color? color,
FontWeight weight = FontWeight.w400,
double? lineHeight,
TextStyle? style,
}) {
size ??= 16.dp;
style ??= TextStyle(fontWeight: weight, fontSize: size, color: color);
if (lineHeight != null) {
style = style.copyWith(height: lineHeight / size);
}
return Text(this, style: style);
}
}
+59
View File
@@ -0,0 +1,59 @@
import 'package:flutter/material.dart';
class DraggableArea extends StatefulWidget {
final Widget child;
const DraggableArea({
Key? key,
required this.child,
this.moveChanged,
this.dx = 0,
this.dy = 0,
}) : super(key: key);
final Function(double dx, double dy)? moveChanged;
final double dx;
final double dy;
@override
State<DraggableArea> createState() => _DraggableAreaState();
}
class _DraggableAreaState extends State<DraggableArea> {
late double left = widget.dx;
late double top = widget.dy;
double? first;
double? end;
@override
Widget build(BuildContext context) {
return Stack(
children: [
Positioned(
left: left,
top: top,
child: Draggable(
feedback: widget.child,
childWhenDragging: const SizedBox.shrink(),
onDragUpdate: (u) => onUpdate(u),
onDragEnd: (detail) => onEnd(detail.offset),
child: widget.child,
),
),
],
);
}
void onEnd(Offset offset) {
left = offset.dx;
top = top + (end ?? 0) - (first ?? 0);
first = null;
end = null;
widget.moveChanged?.call(left, top);
}
onUpdate(DragUpdateDetails u) {
first ??= u.globalPosition.dy;
end = u.globalPosition.dy;
}
}
+233
View File
@@ -0,0 +1,233 @@
import 'package:bot_toast/bot_toast.dart';
import 'package:extended_image/extended_image.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:px_eye/extension/extension.dart';
import 'package:px_eye/image/drag.dart';
import 'package:px_eye/widget/button.dart';
import 'package:window_manager/window_manager.dart';
class ImagePage extends StatefulWidget {
const ImagePage({Key? key}) : super(key: key);
@override
State<ImagePage> createState() => _ImagePageState();
}
class _ImagePageState extends State<ImagePage> with WindowListener {
File? image;
File? image2;
bool isImageSelected1 = true;
bool isImageSelected2 = true;
double opacity1 = 1;
double opacity2 = 1;
double opacityWindow = 1;
double offsetDx1 = 0;
double offsetDy1 = 0;
double offsetDx2 = 0;
double offsetDy2 = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Image Comparison'),
actions: [
SizedBox(
width: 50.dp,
child: EasyButton(
color: Colors.blue,
text: 'reset',
onPressed: () {
offsetDx1 = 0;
offsetDy1 = 0;
offsetDx2 = 0;
offsetDy2 = 0;
setState(() {});
}),
)
],
),
body: Column(
children: <Widget>[
Row(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: 'Window Opacity'.toText(size: 12.dp),
),
Expanded(
child: Slider(
value: opacityWindow,
onChanged: (v) {
opacityWindow = v;
windowManager.setOpacity(opacityWindow);
setState(() {});
},
),
),
],
),
getSelectedButton(),
5.gapColumnWithBg(Colors.grey.shade300),
Expanded(
child: Stack(
children: [
getImageView(
image,
selectImage1,
opacity1,
show: isImageSelected1,
moveChanged: (dx, dy) {
offsetDx1 = dx;
offsetDy1 = dy;
setState(() {});
},
dx: offsetDx1,
dy: offsetDy1,
),
getImageView(
image2,
selectImage2,
opacity2,
show: isImageSelected2,
moveChanged: (dx, dy) {
offsetDx2 = dx;
offsetDy2 = dy;
setState(() {});
},
dx: offsetDx2,
dy: offsetDy2,
),
],
),
),
],
));
}
Widget getImageView(
File? imageFile,
VoidCallback callback,
double opacity, {
bool show = true,
double dx = 0,
double dy = 0,
Function(double dx, double dy)? moveChanged,
}) {
if (!show || imageFile == null) {
return const SizedBox();
}
var width = MediaQuery.of(context).size.width - 100;
return DraggableArea(
key: ValueKey('${DateTime.now()}'),
dx: dx,
dy: dy,
moveChanged: moveChanged,
child: Opacity(
opacity: opacity,
child: SizedBox(
width: width,
child: getImage(imageFile),
),
),
);
}
Widget getImage(File imageFile) {
return Image.file(
imageFile,
fit: BoxFit.fitWidth,
);
}
Future<File?> selectImage() async {
BotToast.showLoading();
FilePickerResult? result = await FilePicker.platform.pickFiles();
BotToast.cleanAll();
var path = result?.files.single.path;
if (path != null) {
return File(path);
}
return null;
}
Widget getSelectedButton() {
return Container(
margin: EdgeInsets.symmetric(vertical: 5.dp),
height: 20.dp,
child: Row(
children: [
SizedBox(
width: 20.dp,
height: 20.dp,
child: Switch(
value: isImageSelected1,
onChanged: (v) {
isImageSelected1 = v;
setState(() {});
},
),
),
SizedBox(
width: 50.dp,
child: EasyButton(text: 'image1', onPressed: selectImage1, type: image != null ? ButtonType.secondary : ButtonType.primary),
),
SizedBox(
width: 80.dp,
child: Slider(
value: opacity1,
onChanged: (v) {
opacity1 = v;
setState(() {});
},
),
),
const Spacer(),
SizedBox(
width: 20.dp,
height: 20.dp,
child: Switch(
value: isImageSelected2,
onChanged: (v) {
isImageSelected2 = v;
setState(() {});
},
),
),
SizedBox(
width: 50.dp,
child: EasyButton(text: 'image2', onPressed: selectImage2, type: image2 != null ? ButtonType.secondary : ButtonType.primary),
),
SizedBox(
width: 80.dp,
child: Slider(
value: opacity2,
onChanged: (v) {
opacity2 = v;
setState(() {});
},
),
)
],
),
);
}
void selectImage1() async {
image = await selectImage();
setState(() {});
}
void selectImage2() async {
image2 = await selectImage();
setState(() {});
}
@override
void onWindowResize() {
setState(() {});
super.onWindowResize();
}
}
+101 -72
View File
@@ -1,6 +1,25 @@
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:px_eye/image/image_page.dart';
import 'package:px_eye/widget/button.dart';
import 'package:window_manager/window_manager.dart';
import 'extension/extension.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Must add this line.
await windowManager.ensureInitialized();
WindowOptions windowOptions = const WindowOptions(
size: Size(800, 1600),
);
windowManager.waitUntilReadyToShow(windowOptions, () async {
await windowManager.show();
await windowManager.focus();
});
void main() {
runApp(const MyApp());
}
@@ -11,20 +30,18 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
builder: BotToastInit(),
title: 'Pixel eye',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
primaryColor: Colors.transparent,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
navigatorObservers: [BotToastNavigatorObserver()],
//2. registered route observer
home: Builder(builder: (context) {
screenSize = MediaQuery.of(context).size;
return const MyHomePage(title: 'Pixel eye');
}),
);
}
}
@@ -32,15 +49,6 @@ class MyApp extends StatelessWidget {
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
@@ -48,68 +56,89 @@ class MyHomePage extends StatefulWidget {
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
int width = 800;
int height = 1600;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
Future<void> onClickEnter() async {
resetScreen();
Navigator.push(
context,
PageRouteBuilder<void>(
opaque: false,
pageBuilder: (_, __, ___) => const ImagePage(),
));
}
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);
setState(() {});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
child: Padding(
padding: EdgeInsets.all(8.dp),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
children: [
'width:'.toText(size: 9.dp),
SizedBox(
height: 20.dp,
width: 50.dp,
child: TextField(
controller: TextEditingController(text: '$width'),
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly],
onChanged: (number) {
width = int.tryParse(number) ?? 800;
},
style: TextStyle(fontSize: 14.dp, fontWeight: FontWeight.w600),
decoration: InputDecoration(
contentPadding: EdgeInsets.all(2.dp),
hintStyle: TextStyle(fontSize: 14.dp, color: Colors.grey, fontWeight: FontWeight.w600),
),
onSubmitted: (_) => onClickEnter(),
),
),
'height:'.toText(size: 9.dp),
SizedBox(
height: 20.dp,
width: 50.dp,
child: TextField(
controller: TextEditingController(text: '$height'),
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly],
onChanged: (number) {
height = int.tryParse(number) ?? 800;
},
style: TextStyle(fontSize: 14.dp, fontWeight: FontWeight.w600),
decoration: InputDecoration(
contentPadding: EdgeInsets.all(2.dp),
hintStyle: TextStyle(fontSize: 14.dp, color: Colors.grey, fontWeight: FontWeight.w600),
),
onSubmitted: (_) => onClickEnter(),
),
),
],
),
5.dp.gapColumn,
EasyButton(
text: 'Enter',
onPressed: () => onClickEnter(),
),
],
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
+71
View File
@@ -0,0 +1,71 @@
import 'package:flutter/material.dart';
enum ButtonType { primary, secondary }
class EasyButton extends StatelessWidget {
const EasyButton({
Key? key,
required this.text,
required this.onPressed,
this.type = ButtonType.primary,
this.padding,
this.height = 48,
this.minWidth = double.infinity,
this.borderSide,
this.textColor,
this.color,
this.fontWeight,
this.borderRadius = 6,
}) : super(key: key);
final String text;
final VoidCallback? onPressed;
final ButtonType type;
final EdgeInsets? padding;
final double height;
final double minWidth;
final BorderSide? borderSide;
final Color? textColor;
final Color? color;
final FontWeight? fontWeight;
final double borderRadius;
@override
Widget build(BuildContext context) {
return Container(
color: Colors.transparent,
padding: padding,
child: TextButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.resolveWith<Color>((Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return Colors.grey.withOpacity(0.5);
}
return color ?? (type == ButtonType.secondary ? Colors.white : Colors.blue);
}),
foregroundColor: MaterialStateProperty.resolveWith<Color>((Set<MaterialState> states) {
if (states.contains(MaterialState.disabled)) {
return Colors.grey.withOpacity(0.1);
}
return color ?? (type == ButtonType.secondary ? Colors.white : Colors.blue);
}),
minimumSize: MaterialStateProperty.all(Size(minWidth, height)),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(borderRadius)),
side: type == ButtonType.secondary ? borderSide ?? BorderSide(color: Colors.grey.withOpacity(0.9)) : BorderSide.none,
),
),
),
onPressed: onPressed,
child: Text(
text,
style: TextStyle(
fontWeight: fontWeight ?? FontWeight.normal,
fontSize: 20,
color: textColor ?? (type == ButtonType.secondary ? Colors.grey : Colors.white)),
),
),
);
}
}