gpt4 book ai didi

flutter - 在抖动中使用GestureDetector移动旋转的图像

转载 作者:行者123 更新时间:2023-12-03 04:34:31 25 4
gpt4 key购买 nike

我有以下代码:

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'dart:math' as math;

void main() {
debugPaintSizeEnabled = true;
return runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
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;

@override
Widget build(BuildContext context) {
return Stack(
children: [
Positioned(
top: _y,
left: _x,
child: Transform.rotate(
angle: math.pi / 180 * 90,
child: Container(
height: 200,
width: 300,
color: Colors.black,
child: Stack(
children: <Widget>[
Positioned(
top: 0,
left: 0,
child: GestureDetector(
onPanUpdate: (details) {
var dx = details.delta.dx;
var dy = details.delta.dy;
setState(() {
_y += dy;
_x += dx;
});
},
child: Image.network("https://via.placeholder.com/300x200"),
),
),
],
),
),
),
),
],
);
}
}
基本上,功能是使图像可拖动。但是当我旋转它并尝试移动图像时,坐标变得困惑。它没有朝正确的方向移动。当 Angular 为0时,它可以完美工作。我们将不胜感激。

最佳答案

您可以在下面复制粘贴运行完整代码
您可以向下移动Transform.rotate以包装Image.network请参阅下面的工作演示
程式码片段

 Transform.rotate(
angle: math.pi / 180 * 90,
child: Image.network(
"https://via.placeholder.com/300x200")),
),
工作演示
enter image description here
完整的代码
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'dart:math' as math;

void main() {
debugPaintSizeEnabled = true;
return runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
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;

@override
Widget build(BuildContext context) {
return Stack(
children: [
Positioned(
top: _y,
left: _x,
child: Container(
height: 200,
width: 300,
color: Colors.black,
child: Stack(
children: <Widget>[
Positioned(
top: 0,
left: 0,
child: GestureDetector(
onPanUpdate: (details) {
var dx = details.delta.dx;
var dy = details.delta.dy;
setState(() {
_y += dy;
_x += dx;
});
},
child: Transform.rotate(
angle: math.pi / 180 * 90,
child: Image.network(
"https://via.placeholder.com/300x200")),
),
),
],
),
),
),
],
);
}
}

关于flutter - 在抖动中使用GestureDetector移动旋转的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64370954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com