gpt4 book ai didi

flutter - 在 flutter 中使用按钮手势旋转文本或图像

转载 作者:行者123 更新时间:2023-12-03 14:27:14 24 4
gpt4 key购买 nike

现在,当我做一个 360 度的手势时,图像只能从左向右平滑旋转。

要求的结果:

  • 当我们做出 360 度的手势时,应该从右向左旋转。
  • 完成:当我们做出 360 度的手势时,应该从右向左旋转。
  • 一旦我们在某个点开始从左向右旋转,然后再次从右向左旋转,它应该从做出手势的任一方向旋转。

enter image description here

  import 'dart:math';

import 'package:flutter/material.dart';

class RotateImage extends StatefulWidget {
RotateImage({Key key}) : super(key: key); // changed

@override
_RotateImageState createState() => _RotateImageState();
}

class _RotateImageState extends State<RotateImage> {
double finalAngle = 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,
origin: Offset(0, 0),
child: Container(
height: 100.0,
width: 100.0,
child: Image.network(
'https://picsum.photos/250?image=9',
),
),
),
),
GestureDetector(
onPanStart: (detials) {},
onPanEnd: (detials) {},
onPanUpdate: (details) {
setState(
() {
finalAngle += details.delta.distance * -pi / 180;
},
);
},
child: Container(
margin: EdgeInsets.only(top: 30),
color: Colors.black54,
width: 50,
height: 50,
child: Icon(
Icons.rotate_left,
color: Colors.white,
),
),
)
],
),
),
);
}
}

最佳答案

在这里,如果您在图标周围做手势(用一根手指),它就会旋转。

源代码 1:(这里的角度是基于距离中心的手指位置 GestureDetector)

演示: DartPad ,

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;

@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',
),
),
),
),
Container(
width: 250,
height: 250,
color: Colors.grey,
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: Icon(
Icons.arrow_forward,
color: Colors.white,
size: 200,
),
),
);
},
),
)
],
),
),
);
}
}

源代码 2:(这里的角度是持久化并将在每个 onPanStart 上继续更新)

演示: DartPad ,

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 offsetAngle = 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',
),
),
),
),
Container(
width: 250,
height: 250,
color: Colors.grey,
margin: EdgeInsets.all(30.0),
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: 200.0,
),
),
);
},
),
)
],
),
),
);
}
}

关于flutter - 在 flutter 中使用按钮手势旋转文本或图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59906976/

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