Skip to content

Instantly share code, notes, and snippets.

@Kurogoma4D
Last active July 8, 2024 04:27
Show Gist options
  • Save Kurogoma4D/297609e398dd27651738bc8e36d1d2fa to your computer and use it in GitHub Desktop.
Save Kurogoma4D/297609e398dd27651738bc8e36d1d2fa to your computer and use it in GitHub Desktop.
From ChatGPT
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('InteractiveViewer Example'),
),
body: Center(
child: LayoutBuilder(
builder: (context, constraints) => InteractiveViewerExample(size: constraints.biggest),
),
),
),
);
}
}
class InteractiveViewerExample extends StatefulWidget {
const InteractiveViewerExample({required this.size});
final Size size;
@override
_InteractiveViewerExampleState createState() => _InteractiveViewerExampleState();
}
const _childSize = 3000.0;
class _InteractiveViewerExampleState extends State<InteractiveViewerExample> {
TransformationController _transformationController = TransformationController();
final double _scaleFactor = 1.5;
@override
void initState() {
super.initState();
_transformationController.value = Matrix4.translationValues(
-_childSize / 2 + widget.size.width / 2,
-_childSize / 2 + widget.size.height / 2,
0
);
}
void _zoomIn() {
setState(() {
_transformationController.value = _scaleMatrix(_scaleFactor);
});
}
void _zoomOut() {
setState(() {
_transformationController.value = _scaleMatrix(1 / _scaleFactor);
});
}
Matrix4 _scaleMatrix(double scale) {
final center = MediaQuery.of(context).size.center(Offset.zero);
final translationToCenter = Matrix4.translationValues(center.dx, center.dy, 0);
final scaleMatrix = Matrix4.diagonal3Values(scale, scale, scale);
final translationBack = Matrix4.translationValues(-center.dx, -center.dy, 0);
return translationToCenter * scaleMatrix * translationBack * _transformationController.value;
}
@override
Widget build(BuildContext context) {
return Column(
children: [
Expanded(
child: InteractiveViewer(
transformationController: _transformationController,
boundaryMargin: EdgeInsets.all(20.0),
minScale: 0.1,
maxScale: 5.0,
constrained: false,
child: Container(
width: _childSize,
height: _childSize,
color: Colors.blue,
child: Center(
child: Text(
'Pinch or zoom',
style: TextStyle(color: Colors.white, fontSize: 24),
),
),
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _zoomIn,
child: Text('Zoom In'),
),
SizedBox(width: 20),
ElevatedButton(
onPressed: _zoomOut,
child: Text('Zoom Out'),
),
],
),
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment