fix: çift tıklama BoxFit cover/contain toggle
Build Windows / build-windows (push) Has been cancelled
Build Windows / build-windows (push) Has been cancelled
This commit is contained in:
1 parent
31b1032661
commit
86a63e0bb6
4 files changed
+5
-46
No files matched your search
Binary file not shown.
Binary file not shown.
@@ -14,30 +14,13 @@ class ImageCanvas extends StatefulWidget {
|
|||||||
class _ImageCanvasState extends State<ImageCanvas> {
|
class _ImageCanvasState extends State<ImageCanvas> {
|
||||||
late TransformationController _controller;
|
late TransformationController _controller;
|
||||||
double _currentScale = 1.0;
|
double _currentScale = 1.0;
|
||||||
bool _isFilled = false; // false = fit, true = fill screen
|
bool _isFilled = false; // false=fit, true=cover
|
||||||
Size? _imageSize;
|
|
||||||
Size? _viewportSize;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_controller = TransformationController();
|
_controller = TransformationController();
|
||||||
_controller.addListener(_onTransformChanged);
|
_controller.addListener(_onTransformChanged);
|
||||||
_loadImageSize();
|
|
||||||
}
|
|
||||||
|
|
||||||
void _loadImageSize() {
|
|
||||||
final image = Image.file(File(widget.filePath));
|
|
||||||
image.image.resolve(const ImageConfiguration()).addListener(
|
|
||||||
ImageStreamListener((info, _) {
|
|
||||||
setState(() {
|
|
||||||
_imageSize = Size(
|
|
||||||
info.image.width.toDouble(),
|
|
||||||
info.image.height.toDouble(),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -45,7 +28,6 @@ class _ImageCanvasState extends State<ImageCanvas> {
|
|||||||
super.didUpdateWidget(oldWidget);
|
super.didUpdateWidget(oldWidget);
|
||||||
if (oldWidget.filePath != widget.filePath) {
|
if (oldWidget.filePath != widget.filePath) {
|
||||||
_resetView();
|
_resetView();
|
||||||
_loadImageSize();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,31 +42,11 @@ class _ImageCanvasState extends State<ImageCanvas> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _toggleFillFit() {
|
void _toggleFillFit() {
|
||||||
if (_imageSize == null || _viewportSize == null) return;
|
setState(() {
|
||||||
|
_isFilled = !_isFilled;
|
||||||
if (_isFilled) {
|
|
||||||
// Go to fit mode — reset transform
|
|
||||||
_controller.value = Matrix4.identity();
|
_controller.value = Matrix4.identity();
|
||||||
_currentScale = 1.0;
|
_currentScale = 1.0;
|
||||||
_isFilled = false;
|
});
|
||||||
} else {
|
|
||||||
// Go to fill mode — scale to cover viewport
|
|
||||||
final scaleX = _viewportSize!.width / _imageSize!.width;
|
|
||||||
final scaleY = _viewportSize!.height / _imageSize!.height;
|
|
||||||
final fillScale = scaleX > scaleY ? scaleX : scaleY;
|
|
||||||
|
|
||||||
// Center the image
|
|
||||||
final offsetX = (_viewportSize!.width - _imageSize!.width * fillScale) / 2;
|
|
||||||
final offsetY = (_viewportSize!.height - _imageSize!.height * fillScale) / 2;
|
|
||||||
|
|
||||||
_controller.value = Matrix4.identity()
|
|
||||||
// ignore: deprecated_member_use
|
|
||||||
..translate(offsetX, offsetY)
|
|
||||||
// ignore: deprecated_member_use
|
|
||||||
..scale(fillScale);
|
|
||||||
_currentScale = fillScale;
|
|
||||||
_isFilled = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void _handleDoubleTap() {
|
void _handleDoubleTap() {
|
||||||
@@ -95,8 +57,6 @@ class _ImageCanvasState extends State<ImageCanvas> {
|
|||||||
final renderBox = context.findRenderObject() as RenderBox?;
|
final renderBox = context.findRenderObject() as RenderBox?;
|
||||||
if (renderBox == null) return;
|
if (renderBox == null) return;
|
||||||
|
|
||||||
_viewportSize = renderBox.size;
|
|
||||||
|
|
||||||
final localPosition = renderBox.globalToLocal(event.position);
|
final localPosition = renderBox.globalToLocal(event.position);
|
||||||
|
|
||||||
final delta = event.scrollDelta.dy;
|
final delta = event.scrollDelta.dy;
|
||||||
@@ -137,7 +97,6 @@ class _ImageCanvasState extends State<ImageCanvas> {
|
|||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return LayoutBuilder(
|
return LayoutBuilder(
|
||||||
builder: (context, constraints) {
|
builder: (context, constraints) {
|
||||||
_viewportSize = Size(constraints.maxWidth, constraints.maxHeight);
|
|
||||||
return Listener(
|
return Listener(
|
||||||
onPointerSignal: (event) {
|
onPointerSignal: (event) {
|
||||||
if (event is PointerScrollEvent) {
|
if (event is PointerScrollEvent) {
|
||||||
@@ -156,7 +115,7 @@ class _ImageCanvasState extends State<ImageCanvas> {
|
|||||||
child: Center(
|
child: Center(
|
||||||
child: Image.file(
|
child: Image.file(
|
||||||
File(widget.filePath),
|
File(widget.filePath),
|
||||||
fit: BoxFit.contain,
|
fit: _isFilled ? BoxFit.cover : BoxFit.contain,
|
||||||
filterQuality: FilterQuality.high,
|
filterQuality: FilterQuality.high,
|
||||||
errorBuilder: (context, error, stackTrace) {
|
errorBuilder: (context, error, stackTrace) {
|
||||||
return const Center(
|
return const Center(
|
||||||
|
|||||||
Binary file not shown.
Reference in new issue
Block a user