我有一幅大小为359kb的图像(图像名称:compass.jpg
)。
Download at
:https://ufile.io/b2rmfx1o
我在https://dartpad.dartlang.org/c4546f1af1e9474f8e159db1b4658801使用代码
用于旋转图像(compass.jpg
)
但是每次我旋转图像(北->东)时,图像都会重置第一个位置(北)
当我旋转时,图像旋转不平滑。
如何解决?
这是我的代码:
body: Center(
child: Column(
children: <Widget>[
Center(
child: Container(
width: MediaQuery.of(context).size.width,
height: 350,
margin: EdgeInsets.all(30.0),
child: LayoutBuilder(
builder: (context, constraints) {
return GestureDetector(
behavior: HitTestBehavior.translucent,
onPanUpdate: (details) {
Offset centerOfGestureDetector = Offset(
constraints.maxWidth / 2,
constraints.maxHeight / 2);
final touchPositionFromCenter =
details.localPosition - centerOfGestureDetector;
print(touchPositionFromCenter.direction * 180 / pi);
setState(
() {
finalAngle = touchPositionFromCenter.direction;
},
);
},
child: Transform.rotate(
angle: finalAngle,
child: Container(
width: 300,
height: 300,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: AssetImage(
"images/compass.jpg"),
fit: BoxFit.scaleDown,
),
),
),
),
);
},
),
),
)
],
),
),
答案 0 :(得分:0)
我修复了它。通过我的解决方案,我使用onPanStart()
处理了onPanEnd()
,onPanUpdate()
。
这是我的代码:
import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(DemoApp());
}
class DemoApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: RotateText(),
);
}
}
class RotateText extends StatefulWidget {
RotateText({Key key}) : super(key: key); // changed
@override
_RotateTextState createState() => _RotateTextState();
}
class _RotateTextState extends State<RotateText> {
double finalAngle = 0.0;
double oldAngle = 0.0;
double upsetAngle = 0.0;
@override
Widget build(BuildContext context) {
return _defaultApp(context);
}
_defaultApp(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Single finger Rotate text'), // changed
),
body: Center(
child: Column(
children: <Widget>[
Container(
color: Colors.red,
padding: EdgeInsets.all(10),
margin: EdgeInsets.only(top: 50),
child: Transform.rotate(
angle: finalAngle,
child: Container(
height: 100.0,
width: 100.0,
child: Image.network(
'https://picsum.photos/250?image=9',
),
),
),
),
Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("180"),
Container(
width: 250,
height: 250,
color: Colors.grey,
margin: EdgeInsets.all(30.0),
child: LayoutBuilder(
builder: (context, constraints) {
Offset centerOfGestureDetector = Offset(
constraints.maxWidth / 2, constraints.maxHeight / 2);
return GestureDetector(
behavior: HitTestBehavior.translucent,
onPanStart: (details) {
final touchPositionFromCenter =
details.localPosition - centerOfGestureDetector;
upsetAngle =
oldAngle - touchPositionFromCenter.direction;
},
onPanEnd: (details) {
setState(
() {
oldAngle = finalAngle;
},
);
},
onPanUpdate: (details) {
final touchPositionFromCenter =
details.localPosition - centerOfGestureDetector;
setState(
() {
finalAngle = touchPositionFromCenter.direction +
upsetAngle;
},
);
},
child: Transform.rotate(
angle: finalAngle,
child: Icon(
Icons.arrow_forward,
color: Colors.white,
size: 200,
),
),
);
},
),
),
Text("0")
],
)
],
),
),
);
}
}