-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
kanji_group_widget.dart
142 lines (127 loc) · 4.31 KB
/
kanji_group_widget.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Flutter imports:
import 'package:flutter/material.dart';
// Package imports:
import 'package:flutter_svg/flutter_svg.dart';
import 'package:graphview/GraphView.dart';
import 'package:tuple/tuple.dart';
// Project imports:
import 'package:da_kanji_mobile/application/dictionary/kanji_vg.dart';
import 'package:da_kanji_mobile/entities/navigation_arguments.dart';
import 'package:da_kanji_mobile/globals.dart';
class KanjiGroupWidget extends StatefulWidget {
/// Tree containing the kanji group hirarchy to be displayed
final String kanjiVG;
/// the height of this widget
final double height;
/// the width of this widget
final double width;
const KanjiGroupWidget(
this.kanjiVG,
this.width,
this.height,
{super.key}
);
@override
State<KanjiGroupWidget> createState() => _KanjiGroupWidgetState();
}
class _KanjiGroupWidgetState extends State<KanjiGroupWidget> {
/// graph of the KanjiVG element
late Graph graph;
/// builder configuration for the GraphView
late BuchheimWalkerConfiguration builder;
/// List containing all sub SVGs of the KanjiVG entry and its order matches
/// all `Node.Id` in `graph`
late List<String> kanjiVGStringList;
/// List containing the unicode characters matching the order of
/// `kanjiVGStringList`
/// Empty strings in the list mean that this character part does not exist
/// in unicode
late List<String> kanjiVGChars;
@override
void initState() {
init();
super.initState();
}
@override
void didUpdateWidget(covariant KanjiGroupWidget oldWidget) {
init();
super.didUpdateWidget(oldWidget);
}
void init(){
graph = Graph()..isTree = true;
Tuple2 tmp = kanjiVGToGraph(widget.kanjiVG, graph);
kanjiVGStringList = tmp.item1;
kanjiVGChars = tmp.item2;
builder = BuchheimWalkerConfiguration()
..siblingSeparation = (10)
..levelSeparation = (15)
..subtreeSeparation = (30)
..orientation = (BuchheimWalkerConfiguration.ORIENTATION_TOP_BOTTOM);
}
@override
Widget build(BuildContext context) {
return Center(
child: FittedBox(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: GraphView(
graph: graph,
algorithm: BuchheimWalkerAlgorithm(builder, TreeEdgeRenderer(builder)),
paint: Paint()
..color = Colors.black
..strokeWidth = 2
..style = PaintingStyle.stroke,
builder: (Node node) {
return MouseRegion(
cursor: kanjiVGChars[node.key!.value] != ""
? SystemMouseCursors.click
: MouseCursor.defer,
child: GestureDetector(
onLongPress: (){
if(kanjiVGChars[node.key!.value] != ""){
Navigator.pushNamedAndRemoveUntil(
context,
'/dictionary',
(route) => false,
arguments: NavigationArguments(
false, dictInitialSearch: kanjiVGChars[node.key!.value]
)
);
}
},
onTap: () {
if(kanjiVGChars[node.key!.value] != ""){
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("copied: ${kanjiVGChars[node.key!.value]}"),
duration: const Duration(seconds: 1),
)
);
}
},
child: Container(
width: 50,
height: 50,
decoration: BoxDecoration(
border: Border.all(
width: 2,
color: kanjiVGChars[node.key!.value] != ""
? g_Dakanji_green
: Colors.black,
)
),
child: Center(
child: SvgPicture.string(
kanjiVGStringList[(node.key!.value as int)]
),
),
),
),
);
},
),
),
),
);
}
}