Flutter GestureDetector onScaleUpdate重置为0
我有一个下面的代码,它基本上使用捏手势检测器旋转/调整大小。但由于某些原因,Flutter GestureDetector onScaleUpdate重置为0,flutter,dart,Flutter,Dart,我有一个下面的代码,它基本上使用捏手势检测器旋转/调整大小。但由于某些原因,比例和角度在比例结束时重置为0 import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'dart:math' as math; void main() { debugPaintSizeEnabled = true; runApp(MyApp()); } class MyApp extend
比例
和角度
在比例结束时重置为0
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'dart:math' as math;
void main() {
debugPaintSizeEnabled = true;
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: PinchRotateZoom(),
);
}
}
class PinchRotateZoom extends StatefulWidget {
@override
_PinchRotateZoomState createState() => _PinchRotateZoomState();
}
class _PinchRotateZoomState extends State<PinchRotateZoom> {
double _width = 300;
double _height = 200;
Matrix4 _matrix = Matrix4(
1, 0, 0, 0, //
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Transform(
transform: _matrix,
alignment: Alignment.center,
child: GestureDetector(
onScaleUpdate: (details) {
var angle = details.rotation;
var scale = details.scale;
print(angle);
print(scale);
var angleMatrix = Matrix4.identity();
angleMatrix[0] = angleMatrix[5] = math.cos(angle);
angleMatrix[1] = math.sin(angle);
angleMatrix[4] = -math.sin(angle);
var scaleMatrix = Matrix4.identity();
scaleMatrix[0] = scaleMatrix[5] = scale;
_matrix = angleMatrix * scaleMatrix;
setState(() {});
},
child: Container(
height: _height,
width: _width,
color: Colors.red,
),
),
),
),
);
}
}
导入“包装:颤振/材料.省道”;
导入“package:flatter/rendering.dart”;
导入'dart:math'作为数学;
void main(){
debugPaintSizeEnabled=true;
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主页:PinchrotateToom(),
);
}
}
类PinchRotateToom扩展StatefulWidget{
@凌驾
_PinchRotateZoomState createState()=>\u PinchRotateZoomState();
}
类_PinchRotateZoomState扩展状态{
双倍宽度=300;
双倍高度=200;
Matrix4_矩阵=Matrix4(
1, 0, 0, 0, //
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
);
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
孩子:变换(
变换:_矩阵,
对齐:对齐.center,
儿童:手势检测器(
规模更新:(详细信息){
变量角度=细节。旋转;
var-scale=details.scale;
打印(角度);
打印(比例);
var angleMatrix=Matrix4.identity();
角度矩阵[0]=角度矩阵[5]=数学cos(角度);
角度矩阵[1]=数学sin(角度);
角度矩阵[4]=-math.sin(角度);
var scaleMatrix=Matrix4.identity();
scaleMatrix[0]=scaleMatrix[5]=比例;
_矩阵=角度矩阵*标度矩阵;
setState((){});
},
子:容器(
高度:_高度,
宽度:_宽度,
颜色:颜色,红色,
),
),
),
),
);
}
}
我是这样解决的:
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'dart:math' as math;
void main() {
debugPaintSizeEnabled = true;
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
// home: EditorScreen(),
home: PinchRotateZoom(),
);
}
}
class PinchRotateZoom extends StatefulWidget {
@override
_PinchRotateZoomState createState() => _PinchRotateZoomState();
}
class _PinchRotateZoomState extends State<PinchRotateZoom> {
double _width = 300;
double _height = 200;
Matrix4 _matrix = Matrix4(
1, 0, 0, 0, //
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
);
double _baseScaleFactor = 1.0;
double _scaleFactor = 1.0;
double _baseAngleFactor = 0;
double _angleFactor = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Transform(
transform: _matrix,
alignment: Alignment.center,
child: GestureDetector(
onScaleStart: (details) {
_baseScaleFactor = _scaleFactor;
_baseAngleFactor = _angleFactor;
},
onScaleUpdate: (details) {
_scaleFactor = _baseScaleFactor * details.scale;
_angleFactor = _baseAngleFactor + details.rotation;
var angleMatrix = Matrix4.identity();
angleMatrix[0] = angleMatrix[5] = math.cos(_angleFactor);
angleMatrix[1] = math.sin(_angleFactor);
angleMatrix[4] = -math.sin(_angleFactor);
var scaleMatrix = Matrix4.identity();
scaleMatrix[0] = scaleMatrix[5] = _scaleFactor;
_matrix = angleMatrix * scaleMatrix;
setState(() {});
},
child: Container(
height: _height,
width: _width,
color: Colors.red,
),
),
),
),
);
}
}
导入“包装:颤振/材料.省道”;
导入“package:flatter/rendering.dart”;
导入'dart:math'作为数学;
void main(){
debugPaintSizeEnabled=true;
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
//主页:编辑器屏幕(),
主页:PinchrotateToom(),
);
}
}
类PinchRotateToom扩展StatefulWidget{
@凌驾
_PinchRotateZoomState createState()=>\u PinchRotateZoomState();
}
类_PinchRotateZoomState扩展状态{
双倍宽度=300;
双倍高度=200;
Matrix4_矩阵=Matrix4(
1, 0, 0, 0, //
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
);
double _baseScaleFactor=1.0;
双_scaleFactor=1.0;
双基准角系数=0;
双角度系数=0;
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
孩子:变换(
变换:_矩阵,
对齐:对齐.center,
儿童:手势检测器(
规模启动:(详细信息){
_baseScaleFactor=\u scaleFactor;
_baseAngleFactor=_angleFactor;
},
规模更新:(详细信息){
_scaleFactor=\u baseScaleFactor*details.scale;
_角度因子=_baseAngleFactor+details.rotation;
var angleMatrix=Matrix4.identity();
angleMatrix[0]=angleMatrix[5]=数学cos(_angleFactor);
角度矩阵[1]=数学sin(_angleFactor);
angleMatrix[4]=-math.sin(_angleFactor);
var scaleMatrix=Matrix4.identity();
scaleMatrix[0]=scaleMatrix[5]=\u scaleFactor;
_矩阵=角度矩阵*标度矩阵;
setState((){});
},
子:容器(
高度:_高度,
宽度:_宽度,
颜色:颜色,红色,
),
),
),
),
);
}
}
我是这样解决的:
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'dart:math' as math;
void main() {
debugPaintSizeEnabled = true;
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
// home: EditorScreen(),
home: PinchRotateZoom(),
);
}
}
class PinchRotateZoom extends StatefulWidget {
@override
_PinchRotateZoomState createState() => _PinchRotateZoomState();
}
class _PinchRotateZoomState extends State<PinchRotateZoom> {
double _width = 300;
double _height = 200;
Matrix4 _matrix = Matrix4(
1, 0, 0, 0, //
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
);
double _baseScaleFactor = 1.0;
double _scaleFactor = 1.0;
double _baseAngleFactor = 0;
double _angleFactor = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Transform(
transform: _matrix,
alignment: Alignment.center,
child: GestureDetector(
onScaleStart: (details) {
_baseScaleFactor = _scaleFactor;
_baseAngleFactor = _angleFactor;
},
onScaleUpdate: (details) {
_scaleFactor = _baseScaleFactor * details.scale;
_angleFactor = _baseAngleFactor + details.rotation;
var angleMatrix = Matrix4.identity();
angleMatrix[0] = angleMatrix[5] = math.cos(_angleFactor);
angleMatrix[1] = math.sin(_angleFactor);
angleMatrix[4] = -math.sin(_angleFactor);
var scaleMatrix = Matrix4.identity();
scaleMatrix[0] = scaleMatrix[5] = _scaleFactor;
_matrix = angleMatrix * scaleMatrix;
setState(() {});
},
child: Container(
height: _height,
width: _width,
color: Colors.red,
),
),
),
),
);
}
}
导入“包装:颤振/材料.省道”;
导入“package:flatter/rendering.dart”;
导入'dart:math'作为数学;
void main(){
debugPaintSizeEnabled=true;
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
//主页:编辑器屏幕(),
主页:PinchrotateToom(),
);
}
}
类PinchRotateToom扩展StatefulWidget{
@凌驾
_PinchRotateZoomState createState()=>\u PinchRotateZoomState();
}
类_PinchRotateZoomState扩展状态{
双倍宽度=300;
双倍高度=200;
Matrix4_矩阵=Matrix4(
1, 0, 0, 0, //
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
);
double _baseScaleFactor=1.0;
双_scaleFactor=1.0;
双基准角系数=0;
双角度系数=0;
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
孩子:变换(
变换:_矩阵,
对齐:对齐.center,
儿童:手势检测器(
规模启动:(详细信息){
_baseScaleFactor=\u scaleFactor;
_baseAngleFactor=_angleFactor;
},
规模更新:(详细信息){
_scaleFactor=\u baseScaleFactor*details.scale;
_角度因子=_baseAngleFactor+details.rotation;
var angleMatrix=Matrix4.identity();
angleMatrix[0]=angleMatrix[5]=数学cos(_angleFactor);
角度矩阵[1]=数学sin(_angleFactor);
angleMatrix[4]=-math.sin(_angleFactor);
var scaleMatrix=Matrix4.identity();
scaleMatrix[0]=scaleMatrix[5]=\u scaleFactor;
_矩阵=角度矩阵*标度矩阵;
setState((){});
},
子:容器(
高度:_高度,
宽度:_宽度,
颜色:颜色,红色,
),
),
),
),
);