【发布时间】:2020-06-30 17:28:35
【问题描述】:
我有一个用于光标滚轮小部件的颤振代码。
import 'package:flutter/material.dart';
import 'dart:math';
void main() {
runApp(MyApp());
}
class MyApp 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 offsetAngle = 0.0;
@override
Widget build(BuildContext context) {
return _defaultApp(context);
}
_defaultApp(BuildContext context) {
var radius = 300.0;
var left = MediaQuery.of(context).size.width / 2 - radius;
var size = 1.6 * radius;
var place = 60;
var item = 100.0;
var distance = 180;
return Scaffold(
appBar: AppBar(
title: Text('Single finger Rotate text'), // changed
),
body: new Stack(
children: <Widget>[
Positioned(
left: left,
bottom: place - radius,
child: Container(
width: 2 * radius,
height: 2 * radius,
decoration: new BoxDecoration(
shape: BoxShape.circle,
color: Colors.grey,
),
child: LayoutBuilder(
builder: (context, constraints) {
return GestureDetector(
behavior: HitTestBehavior.translucent,
onPanStart: (details) {
Offset centerOfGestureDetector = Offset(constraints.maxWidth / 2, constraints.maxHeight / 2);
final touchPositionFromCenter = details.localPosition - centerOfGestureDetector;
offsetAngle = touchPositionFromCenter.direction - finalAngle;
},
onPanUpdate: (details) {
Offset centerOfGestureDetector = Offset(constraints.maxWidth / 2, constraints.maxHeight / 2);
final touchPositionFromCenter = details.localPosition - centerOfGestureDetector;
setState(() {
finalAngle = touchPositionFromCenter.direction - offsetAngle;
});
},
child: Transform.rotate(
angle: finalAngle,
child: Icon(
Icons.settings,
color: Colors.white,
size: size,
),
),
);
},
),
)
),
Positioned(
left: left + radius / 2 + item + distance * cos(finalAngle - pi / 2),
bottom: place - item / 2 - distance * sin(finalAngle - pi / 2),
child: Container(
height: item,
width: item,
child: Image.network(
'https://picsum.photos/250?image=9',
),
)
),
]
)
);
}
}
它可以正常工作并通过拖动旋转,但我不会停留在一个带有动画的菜单项上并自动校准。
当它在径向区域范围内时,如何使其旋转(使用 finalAngle 浮点数)以重新定位在项目的中心?
谢谢。
【问题讨论】:
标签: flutter