-
Notifications
You must be signed in to change notification settings - Fork 19
/
day088_horizontal_drag.dart
111 lines (105 loc) · 3.34 KB
/
day088_horizontal_drag.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
class Day88HorizontalDrag extends StatefulWidget {
@override
_Day88HorizontalDragState createState() => _Day88HorizontalDragState();
}
class _Day88HorizontalDragState extends State<Day88HorizontalDrag> {
Offset _offset;
String onHorizontalDragText;
String isDragLeftOrRight;
@override
void initState() {
super.initState();
_offset = Offset(0, 0);
onHorizontalDragText = 'Start dragging up and down';
isDragLeftOrRight = 'Drag Status';
}
@override
Widget build(BuildContext context) {
double deviceWidth = MediaQuery.of(context).size.width;
double deviceHeight = MediaQuery.of(context).size.height;
return Scaffold(
body: GestureDetector(
onHorizontalDragStart: (details) {
setState(() {
onHorizontalDragText = 'onHorizontalDragStart';
});
},
onHorizontalDragEnd: (details) {
setState(() {
onHorizontalDragText = 'onHorizontalDragEnd';
isDragLeftOrRight = 'Dragging Stopped';
});
},
onHorizontalDragUpdate: (details) {
bool isRight = deviceWidth * 0.8 < _offset.dx;
bool isLeft = -deviceWidth * 0.8 > _offset.dx;
if (details.delta.dx < 0) {
// ! on draging left....
if (!isLeft) {
// ! check is not left already.....
setState(() {
isDragLeftOrRight = "Dragging Left";
_offset = Offset(_offset.dx + details.delta.dx, _offset.dy);
});
}
} else {
// ! on draging right....
if (!isRight) {
// ! check is not right already.....
setState(() {
isDragLeftOrRight = "Dragging Right";
_offset = Offset(_offset.dx + details.delta.dx, _offset.dy);
});
}
}
},
child: Stack(
fit: StackFit.expand,
children: [
Container(
width: deviceWidth,
height: deviceHeight,
color: Colors.yellow,
),
Transform.translate(
offset:
_offset, // ! This is the Container with blue color is Draggable by Translate....
child: Container(
width: deviceWidth,
height: deviceHeight,
color: Colors.blue,
),
),
Center(
child: Text(
'$onHorizontalDragText\n$isDragLeftOrRight',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20),
))
],
),
),
appBar: AppBar(
title: Text('HorizontalDrag'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.help),
onPressed: () async {
const url =
'https://github.com/sanjaysanju618/100-Days-Of-Flutter-Widgets/' +
'blob/master/hundred_days_of_flutter_widget/' +
'lib/day088_horizontal_drag.dart';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
},
)
],
),
);
}
}