【发布时间】:2020-10-18 06:41:27
【问题描述】:
我有以下代码:
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:vector_math/vector_math.dart' as vector;
import 'dart:math' as math;
void main() {
debugPaintSizeEnabled = true;
return runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.grey,
body: SafeArea(
child: TestComp(),
),
),
);
}
}
class TestComp extends StatefulWidget {
@override
_TestCompState createState() => _TestCompState();
}
class _TestCompState extends State<TestComp> {
double _y = 0;
double _x = 0;
double _height = 200;
double _width = 300;
int angle = -136;
vector.Vector4 testRotation(var dx, var dy) {
vector.Matrix4 matrix = vector.Matrix4.identity();
matrix..rotateZ(math.pi / 180 * angle);
return matrix * vector.Vector4(dx, dy, 0, 0);
}
double get _aspectRatio {
return _height / _width;
}
@override
Widget build(BuildContext context) {
return Stack(
children: [
Positioned(
top: _y,
left: _x,
child: Transform.rotate(
angle: math.pi / 180 * angle, //-136,
child: Container(
height: _height,
width: _width,
color: Colors.black,
child: Stack(
overflow: Overflow.visible,
children: <Widget>[
Positioned(
top: 0,
left: 0,
child: GestureDetector(
onPanUpdate: (details) {
var newdx =
testRotation(details.delta.dx, details.delta.dy)[0];
var newdy =
testRotation(details.delta.dx, details.delta.dy)[1];
setState(() {
_y += newdy;
_x += newdx;
});
},
child: Image.network(
"https://via.placeholder.com/300x200",
height: _height,
width: _width,
fit: BoxFit.fill,
),
),
),
Positioned(
top: 0,
left: 0,
child: GestureDetector(
onPanUpdate: (DragUpdateDetails details) {
var position = testRotation(
details.delta.dx,
details.delta.dy,
);
// print(position);
var dx = position[0];
var dy = position[1];
var newWidth = _width - dx;
var newHeight = newWidth * _aspectRatio;
setState(() {
_y = _y + (_height - newHeight);
_x = _x + dx;
_width = newWidth;
_height = newHeight;
});
},
child: Container(
width: 10,
height: 10,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(10),
),
),
),
),
],
),
),
),
),
],
);
}
}
图片左上角有一个用于调整大小的句柄。我希望轮换是这样的:
但它不是那样工作的。调整大小搞砸了。请帮帮我。
【问题讨论】:
-
@pskink 感谢您的回答,但这看起来很复杂,哈哈。我会调查的。
-
@pskink 我的意思是你的代码对我来说看起来很复杂。只有我不是一般的。
-
@pskink 代码在哪里?