-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathenough_html_editor_example.dart
163 lines (150 loc) · 5.12 KB
/
enough_html_editor_example.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
import 'package:enough_html_editor/enough_html_editor.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) => MaterialApp(
title: 'enough_html_editor Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: const EditorPage(),
);
}
/// Example how to use the simplified [PackagedHtmlEditor]
/// that combines the default controls and the editor.
class EditorPage extends StatefulWidget {
const EditorPage({Key? key}) : super(key: key);
@override
_EditorPageState createState() => _EditorPageState();
}
class _EditorPageState extends State<EditorPage> {
HtmlEditorApi? _editorApi;
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: const Text('PackagedHtmlEditor Demo'),
actions: [
IconButton(
icon: const Icon(Icons.send),
onPressed: () async {
final text = await _editorApi!.getText();
print('got text: [$text]');
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ResultScreen(htmlText: text),
),
);
},
),
],
),
body: PackagedHtmlEditor(
onCreated: (api) {
_editorApi = api;
},
initialContent: '''<p>Here is some text</p>
<p>Here is <b>bold</b> text with a <a href="https://github.com/Enough-Software/enough_html_editor">link</a>.</p>
<p>Here is <i>some italic sic</i> text</p>
<p>Here is <i><b>bold and italic</b></i> text</p>
<p style="text-align: center;">Here is <u><i><b>bold and italic and underline</b></i></u> text</p>
<ul><li>one list element</li><li>another point</li></ul>
<blockquote>Here is a quote<br/>
that spans several lines<br/>
<blockquote>
Another second level blockqote
</blockquote>
</blockquote>
''',
),
);
}
/// Example how to use editor within a a CustomScrollView
class CustomScrollEditorPage extends StatefulWidget {
const CustomScrollEditorPage({Key? key}) : super(key: key);
@override
_CustomScrollEditorPageState createState() => _CustomScrollEditorPageState();
}
class _CustomScrollEditorPageState extends State<CustomScrollEditorPage> {
HtmlEditorApi? _editorApi;
@override
Widget build(BuildContext context) => Scaffold(
body: CustomScrollView(
physics: const BouncingScrollPhysics(),
slivers: [
SliverAppBar(
title: const Text('Editor Demo'),
floating: false,
pinned: true,
stretch: true,
actions: [
IconButton(
icon: const Icon(Icons.send),
onPressed: () async {
final text = await _editorApi!.getText();
print('got text: [$text]');
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ResultScreen(htmlText: text),
),
);
},
),
],
),
const SliverToBoxAdapter(
child: Padding(
padding: EdgeInsets.all(8.0),
child:
TextField(decoration: InputDecoration(hintText: 'Subject')),
),
),
if (_editorApi != null) ...{
SliverHeaderHtmlEditorControls(editorApi: _editorApi),
},
SliverToBoxAdapter(
child: HtmlEditor(
onCreated: (api) {
setState(() {
_editorApi = api;
});
},
initialContent: '''<p>Here is some text</p>
<p>Here is <b>bold</b> text</p>
<p>Here is <i>some italic sic</i> text</p>
<p>Here is <i><b>bold and italic</b></i> text</p>
<p style="text-align: center;">Here is <u><i><b>bold and italic and underline</b></i></u> text</p>
<ul><li>one list element</li><li>another point</li></ul>
<blockquote>Here is a quote<br/>
that spans several lines<br/>
<blockquote>
Another second level blockqote
</blockquote>
</blockquote>
''',
),
),
],
),
);
}
/// Displays the resulting HTML code
class ResultScreen extends StatelessWidget {
/// Creates a new result page
const ResultScreen({Key? key, required this.htmlText}) : super(key: key);
/// The HTML code
final String htmlText;
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: const Text('Result'),
),
body: SingleChildScrollView(
child: SelectableText(htmlText),
),
);
}