-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathhome.dart
235 lines (216 loc) · 6.09 KB
/
home.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import 'dart:convert';
import 'package:example/src/read_only_view.dart';
import 'package:file/local.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:zefyrka/zefyrka.dart';
import 'forms_decorated_field.dart';
import 'layout.dart';
import 'layout_expanded.dart';
import 'layout_scrollable.dart';
import 'settings.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
ZefyrController? _controller;
final FocusNode _focusNode = FocusNode();
Settings? _settings;
void _handleSettingsLoaded(Settings value) {
setState(() {
_settings = value;
_loadFromAssets();
});
}
@override
void initState() {
super.initState();
Settings.load().then(_handleSettingsLoaded);
}
Future<void> _loadFromAssets() async {
try {
final result = await rootBundle.loadString('assets/welcome.note');
final doc = NotusDocument.fromJson(jsonDecode(result));
setState(() {
_controller = ZefyrController(doc);
});
} catch (error) {
final doc = NotusDocument()..insert(0, 'Empty asset');
setState(() {
_controller = ZefyrController(doc);
});
}
}
Future<void> _save() async {
final fs = LocalFileSystem();
final file = fs.directory(_settings!.assetsPath).childFile('welcome.note');
final data = jsonEncode(_controller!.document);
await file.writeAsString(data);
}
@override
Widget build(BuildContext context) {
if (_settings == null || _controller == null) {
return Scaffold(body: Center(child: Text('Loading...')));
}
return SettingsProvider(
settings: _settings,
child: PageLayout(
appBar: AppBar(
backgroundColor: Colors.grey.shade800,
elevation: 0,
centerTitle: false,
title: Text(
'Zefyr',
style: GoogleFonts.fondamento(color: Colors.white),
),
actions: [
IconButton(
icon: Icon(Icons.settings, size: 16),
onPressed: _showSettings,
),
if (_settings!.assetsPath!.isNotEmpty)
IconButton(
icon: Icon(Icons.save, size: 16),
onPressed: _save,
)
],
),
menuBar: Material(
color: Colors.grey.shade800,
child: _buildMenuBar(context),
),
body: _buildWelcomeEditor(context),
),
);
}
void _showSettings() async {
final result = await showSettingsDialog(context, _settings);
if (mounted && result != null) {
setState(() {
_settings = result;
});
}
}
Widget _buildMenuBar(BuildContext context) {
final headerStyle = TextStyle(fontSize: 11, color: Colors.grey.shade500, fontWeight: FontWeight.bold);
final itemStyle = TextStyle(color: Colors.white);
return ListView(
children: [
ListTile(
title: Text('BASIC EXAMPLES', style: headerStyle),
// dense: true,
visualDensity: VisualDensity.compact,
),
ListTile(
title: Text('¶ Read only view', style: itemStyle),
dense: true,
visualDensity: VisualDensity.compact,
onTap: _readOnlyView,
),
ListTile(
title: Text('LAYOUT EXAMPLES', style: headerStyle),
// dense: true,
visualDensity: VisualDensity.compact,
),
ListTile(
title: Text('¶ Expandable', style: itemStyle),
dense: true,
visualDensity: VisualDensity.compact,
onTap: _expanded,
),
ListTile(
title: Text('¶ Custom scrollable', style: itemStyle),
dense: true,
visualDensity: VisualDensity.compact,
onTap: _scrollable,
),
ListTile(
title: Text('FORMS AND FIELDS EXAMPLES', style: headerStyle),
// dense: true,
visualDensity: VisualDensity.compact,
),
ListTile(
title: Text('¶ Decorated field', style: itemStyle),
dense: true,
visualDensity: VisualDensity.compact,
onTap: _decoratedField,
),
],
);
}
Widget _buildWelcomeEditor(BuildContext context) {
return Column(
children: [
ZefyrToolbar.basic(controller: _controller!),
Divider(height: 1, thickness: 1, color: Colors.grey.shade200),
Expanded(
child: Container(
color: Colors.white,
padding: const EdgeInsets.only(left: 16.0, right: 16.0),
child: ZefyrEditor(
controller: _controller!,
focusNode: _focusNode,
autofocus: true,
// readOnly: true,
// padding: EdgeInsets.only(left: 16, right: 16),
onLaunchUrl: _launchUrl,
),
),
),
],
);
}
void _launchUrl(Uri url) async {
final result = await canLaunchUrl(url);
if (result) {
await launchUrl(url);
}
}
void _expanded() {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => SettingsProvider(
settings: _settings,
child: ExpandedLayout(),
),
),
);
}
void _readOnlyView() {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => SettingsProvider(
settings: _settings,
child: ReadOnlyView(),
),
),
);
}
void _scrollable() {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => SettingsProvider(
settings: _settings,
child: ScrollableLayout(),
),
),
);
}
void _decoratedField() {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => SettingsProvider(
settings: _settings,
child: DecoratedFieldDemo(),
),
),
);
}
}