Skip to content

Commit 8914e38

Browse files
committed
feat: support zooming through gestures.
1 parent c7b9687 commit 8914e38

File tree

6 files changed

+71
-19
lines changed

6 files changed

+71
-19
lines changed

.pubignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/docs

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
## 1.1.1
2+
- fix usage of both Scale and Pan on mobile platforms. ( [#12](https://github.com/graph-cn/flutter_graph_view/pull/12), via: [Mykyta Sadchenko](https://github.com/muknta))
3+
- feat: support zooming through gestures.
4+
15
## 1.1.0
26
- feat: add interface to `GraphComponent`: addVertex, addEdge, mergeGraph
7+
- feat: add implementation of PersistenceDecorator to store position of vertex. ([#10](https://github.com/graph-cn/flutter_graph_view/pull/10) [#11](https://github.com/graph-cn/flutter_graph_view/pull/11), via: [jersonal-com](https://github.com/jersonal-com))
38

49
### Behavior change:
510
- interface change: add a graph parameter to `DataConvertor.convertGraph`

lib/core/graph_algorithm.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import 'dart:math';
66

7-
import 'package:flame/events.dart';
87
import 'package:flutter/widgets.dart';
98
import 'package:flutter_graph_view/flutter_graph_view.dart';
109

lib/flutter_graph_widget.dart

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) 2023- All flutter_graph_view authors. All rights reserved.
22
//
33
// This source code is licensed under Apache 2.0 License.
4+
import 'package:flame/extensions.dart';
45
import 'package:flame/game.dart';
56
import 'package:flutter/material.dart';
67
import 'package:flutter_graph_view/flutter_graph_view.dart';
@@ -42,16 +43,48 @@ class _FlutterGraphWidgetState extends State<FlutterGraphWidget> {
4243

4344
@override
4445
Widget build(BuildContext context) {
45-
return GameWidget(
46-
backgroundBuilder: widget.options?.backgroundBuilder,
47-
overlayBuilderMap: overlayBuilderMap2,
48-
game: graphCpn = GraphComponent(
49-
data: widget.data,
50-
convertor: widget.convertor,
51-
algorithm: widget.algorithm,
52-
options: widget.options,
53-
context: context,
54-
),
46+
double? scale;
47+
Vector2 center = Vector2.zero();
48+
return Stack(
49+
children: [
50+
Positioned.fill(
51+
child: GameWidget(
52+
backgroundBuilder: widget.options?.backgroundBuilder,
53+
overlayBuilderMap: overlayBuilderMap2,
54+
game: graphCpn = GraphComponent(
55+
data: widget.data,
56+
convertor: widget.convertor,
57+
algorithm: widget.algorithm,
58+
options: widget.options,
59+
context: context,
60+
),
61+
),
62+
),
63+
Positioned.fill(
64+
child: MouseRegion(
65+
onHover: (ev) => center = ev.localPosition.toVector2(),
66+
opaque: false,
67+
)),
68+
Positioned.fill(
69+
child: GestureDetector(
70+
onScaleUpdate: (ScaleUpdateDetails details) {
71+
// single finger pan
72+
if (details.pointerCount == 1) {
73+
var v = details.focalPointDelta.toVector2();
74+
graphCpn.onPanUpdate(v);
75+
} else if (details.pointerCount == 2) {
76+
graphCpn.onZoom(
77+
zoomCenter: center,
78+
zoomDelta: scale == null
79+
? (details.scale - 1)
80+
: (details.scale - scale!),
81+
);
82+
scale = details.scale;
83+
}
84+
},
85+
onScaleEnd: (details) => scale = null,
86+
))
87+
],
5588
);
5689
}
5790
}

lib/widgets/graph_component.dart

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,16 @@ class GraphComponent extends FlameGame
169169

170170
@override
171171
void onScroll(PointerScrollInfo info) {
172+
var zoomCenter = info.eventPosition.widget;
173+
var zoomDelta = info.scrollDelta.global.y.sign * zoomPerScrollUnit;
174+
onZoom(zoomCenter: zoomCenter, zoomDelta: zoomDelta);
175+
}
176+
177+
void onZoom({required Vector2 zoomCenter, double zoomDelta = 0}) {
172178
var vf = camera.viewfinder;
173179
var opg = vf.localToGlobal(Vector2.zero());
174180
var oz = vf.zoom;
175-
var zoomDelta = info.scrollDelta.global.y.sign * zoomPerScrollUnit;
181+
176182
if (vf.zoom + zoomDelta > 0) {
177183
vf.zoom += zoomDelta;
178184
}
@@ -181,14 +187,23 @@ class GraphComponent extends FlameGame
181187
if (vf.zoom <= options.scaleRange.x || vf.zoom >= options.scaleRange.y) {
182188
return;
183189
}
184-
185-
keepMousePosition(info, opg, zoomDelta, vf, oz);
190+
keepMousePosition(null, opg, zoomDelta, vf, oz, zoomCenter);
186191
}
187192

188193
/// ![](https://gitee.com/graph-cn/flutter_graph_view/raw/main/lib/widgets/GraphComponent_scale_explain.jpg)
189-
void keepMousePosition(PointerScrollInfo info, Vector2 opg, double zoomDelta,
190-
Viewfinder vf, double oz) {
191-
var wp = info.eventPosition.widget;
194+
void keepMousePosition(
195+
PointerScrollInfo? info,
196+
Vector2 opg,
197+
double zoomDelta,
198+
Viewfinder vf,
199+
double oz, [
200+
Vector2? wp,
201+
]) {
202+
assert(
203+
info != null || wp != null,
204+
'scroll info and widgetPosition cannot be null at the same time',
205+
);
206+
wp = wp ?? info!.eventPosition.widget;
192207
var wpg = wp - opg;
193208
var wpgDelta = wpg * zoomDelta;
194209
var npg = vf.localToGlobal(Vector2.zero());
@@ -216,6 +231,5 @@ class GraphComponent extends FlameGame
216231

217232
legendCount = i;
218233
}
219-
print(legendCount);
220234
}
221235
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: flutter_graph_view
22
description: Widgets for beautiful graphic data structures, such as force-oriented diagrams.
3-
version: 1.1.0
3+
version: 1.1.1
44
repository: https://github.com/dudu-ltd/flutter_graph_view
55
homepage: https://graph-cn.github.io/flutter_graph_view
66

0 commit comments

Comments
 (0)