main.dart
7.35 KB
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
import 'dart:developer';
import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:tex_markdown/tex_markdown.dart';
import 'package:url_launcher/url_launcher_string.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
ThemeMode _themeMode = ThemeMode.dark;
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
themeMode: _themeMode,
theme: ThemeData(
useMaterial3: true,
brightness: Brightness.light,
colorSchemeSeed: Colors.blue,
),
darkTheme: ThemeData(
useMaterial3: true,
brightness: Brightness.dark,
colorSchemeSeed: Colors.blue,
),
home: MyHomePage(
title: 'Flutter Demo Home Page',
onPressed: () {
setState(() {
_themeMode = (_themeMode == ThemeMode.dark)
? ThemeMode.light
: ThemeMode.dark;
});
},
),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title, required this.onPressed});
final VoidCallback? onPressed;
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
TextDirection _direction = TextDirection.ltr;
final TextEditingController _controller = TextEditingController(text: r'''
# This is H1
## This is H2
### This is H3
#### This is H4
##### This is H5
###### This is H6
This are just regular text.
---
This is a image.

**bold $x^2\cfrac a{\cfrac ab}$ text**
*Italic text $x^2\cfrac a{b}$*
**hello**
$hello$
$sdf \frac a{\frac ab}$
$hello$
[Link]()
- unordered list
1. ordered list 1
2. ordered list 2
(x) Radio checked
() Radio unchecked
[x] checkbox checked
[] Checkbox unchecked
| Name | Country |
| Sohag | Bangladesh |
''');
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
actions: [
IconButton(
onPressed: () {
setState(() {
_direction = TextDirection.values[(_direction.index + 1) % 2];
});
},
icon: const [Text("LTR"), Text("RTL")][_direction.index],
),
IconButton(
onPressed: widget.onPressed,
icon: const Icon(Icons.sunny),
),
],
),
body: Stack(
children: [
Column(
children: [
Expanded(
child: ListView(
children: [
AnimatedBuilder(
animation: _controller,
builder: (context, _) {
return Material(
// color: Theme.of(context).colorScheme.surfaceVariant,
shape: RoundedRectangleBorder(
side: BorderSide(
width: 1,
color: Theme.of(context).colorScheme.outline),
),
child:
LayoutBuilder(builder: (context, constraints) {
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
reverse: _direction == TextDirection.rtl,
child: SizedBox(
width: 400,
child: Theme(
data: Theme.of(context).copyWith(
textTheme: const TextTheme(
// For H1.
headlineLarge: TextStyle(fontSize: 55),
// For H2.
headlineMedium: TextStyle(fontSize: 45),
// For H3.
headlineSmall: TextStyle(fontSize: 35),
// For H4.
titleLarge: TextStyle(fontSize: 25),
// For H5.
titleMedium: TextStyle(fontSize: 15),
// For H6.
titleSmall: TextStyle(fontSize: 10),
),
),
child: TexMarkdown(
_controller.text,
textDirection: _direction,
onLinkTab: (url, title) {
log(title, name: "title");
log(url, name: "url");
},
style: const TextStyle(
// Regular text font size here.
fontSize: 15,
),
),
// child: const Text("Hello"),
),
),
);
}),
);
}),
],
),
),
ConstrainedBox(
constraints: const BoxConstraints(maxHeight: 200),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
label: Text("Type here:")),
maxLines: null,
controller: _controller,
),
),
),
],
),
Align(
alignment: Alignment.topRight,
child: IconButton(
onPressed: () async {
String html = '''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"><title>markdown</title>
</head>
<body>
${TexMarkdown.toHtml(_controller.text)}
</body>
</html>
''';
String? path = await FilePicker.platform.saveFile();
if (path == null) {
return;
}
File(path).writeAsStringSync(html);
launchUrlString(
"file:${Uri(
path: path,
).path}",
mode: LaunchMode.externalApplication,
);
},
icon: const Icon(Icons.html),
),
),
],
),
);
}
}