Skip to content

Instantly share code, notes, and snippets.

@kumar-aakash86
Last active July 8, 2020 16:24
Show Gist options
  • Save kumar-aakash86/252d06a0a31b72e268840b40ac4ff1b3 to your computer and use it in GitHub Desktop.
Save kumar-aakash86/252d06a0a31b72e268840b40ac4ff1b3 to your computer and use it in GitHub Desktop.
Rotate With Handler
import 'dart:math';
import 'package:flutter/material.dart';
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 oldAngle = 0.0;
double upsetAngle = 0.0;
@override
Widget build(BuildContext context) {
return _defaultApp(context);
}
Offset centerOfGestureDetector;
_defaultApp(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Single finger Rotate text'), // changed
),
body: Center(
child: Column(
children: <Widget>[
Text(finalAngle.toString()),
Text(rotationalChange.toString()),
Container(
color: Colors.red,
width: 80,
height: 80,
padding: EdgeInsets.all(10),
margin: EdgeInsets.only(top: 20),
child: LayoutBuilder(builder: (context, constraints) {
// print('1 $constraints');
centerOfGestureDetector = Offset(
constraints.maxWidth / 2, constraints.maxHeight / 2);
return Transform.rotate(
angle: finalAngle,
child: Stack(
children: [
Container(
height: 100.0,
width: 100.0,
child: Image.network(
'https://picsum.photos/250?image=9',
),
),
Positioned(
top: 0,
right: 0,
child: Container(
color: Colors.white,
child: GestureDetector(
onPanUpdate: _panHandler,
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;
// print(
// '${details.localPosition} - ${touchPositionFromCenter.direction} - $centerOfGestureDetector');
// setState(
// () {
// finalAngle =
// touchPositionFromCenter.direction +
// upsetAngle;
// },
// );
// },
// onPanUpdate: (details) {
// Offset off = new Offset(constraints.maxWidth-details.localPosition.dx, details.localPosition.dy);
// print('${details.localPosition} - ${centerOfGestureDetector} - ${off}');
// final touchPositionFromCenter =
// off -
// centerOfGestureDetector;
// // print(
// // '${details.localPosition} - ${touchPositionFromCenter.direction} - $centerOfGestureDetector');
// // print('${details.localPosition} - ${touchPositionFromCenter.direction *
// // 180 /
// // pi} - $centerOfGestureDetector');
// setState(
// () {
// finalAngle =
// touchPositionFromCenter.direction;
// },
// );
// },
child: Container(
width: 16,
height: 16,
child: Icon(Icons.add, size: 12)
),
),
),
),
],
),
);
})),
Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("180"),
Container(
width: 150,
height: 150,
color: Colors.grey,
margin: EdgeInsets.all(30.0),
child: LayoutBuilder(
builder: (context, constraints) {
// print('2 $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);
print(
'${details.localPosition} - ${touchPositionFromCenter.direction} - $centerOfGestureDetector');
setState(
() {
finalAngle = touchPositionFromCenter.direction;
},
);
},
child: Transform.rotate(
angle: finalAngle,
child: Icon(
Icons.arrow_forward,
color: Colors.white,
size: 100,
),
),
);
},
),
),
Text("0")
],
)
],
),
),
);
}
double rotationalChange = 0;
void _panHandler(DragUpdateDetails d) {
/// Pan location on the wheel
bool onTop = true;
bool onLeftSide = true;
bool onRightSide = !onLeftSide;
bool onBottom = !onTop;
// print('$onTop $onLeftSide $onRightSide $onBottom');
/// Pan movements
bool panUp = d.delta.dy <= 0.0;
bool panLeft = d.delta.dx <= 0.0;
bool panRight = !panLeft;
bool panDown = !panUp;
/// Absoulte change on axis
double yChange = d.delta.dy.abs();
double xChange = d.delta.dx.abs();
/// Directional change on wheel
double verticalRotation = (onRightSide && panDown) || (onLeftSide && panUp)
? yChange
: yChange * -1;
double horizontalRotation =
(onTop && panRight) || (onBottom && panLeft) ? xChange : xChange * -1;
// Total computed change
rotationalChange = verticalRotation + horizontalRotation;
bool movingClockwise = rotationalChange > 0;
bool movingCounterClockwise = rotationalChange < 0;
// Now do something interesting with these computations!
print('$verticalRotation $horizontalRotation $rotationalChange $movingClockwise $movingCounterClockwise');
setState(() {});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment