Skip to content

Commit e044a90

Browse files
committed
changed example, added nonScrolling markdownBody, improved min max lines logic in textfield
1 parent 66c95a3 commit e044a90

File tree

5 files changed

+232
-177
lines changed

5 files changed

+232
-177
lines changed

example/lib/main.dart

Lines changed: 35 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -17,64 +17,14 @@ class MyApp extends StatelessWidget {
1717
}
1818
}
1919

20-
class HomeScreen extends StatelessWidget {
20+
class HomeScreen extends StatefulWidget {
2121
const HomeScreen({Key? key}) : super(key: key);
2222

2323
@override
24-
Widget build(BuildContext context) {
25-
return Scaffold(
26-
appBar: AppBar(
27-
title: const Text("Home Screen"),
28-
),
29-
body: Center(
30-
child: Column(
31-
mainAxisAlignment: MainAxisAlignment.center,
32-
children: [
33-
MaterialButton(
34-
onPressed: () {
35-
Navigator.push(context,
36-
MaterialPageRoute(builder: (_) => const EditorScreen()));
37-
},
38-
color: Colors.blue,
39-
child: const Text(
40-
"Editor Screen 1",
41-
style: TextStyle(
42-
color: Colors.white,
43-
),
44-
),
45-
),
46-
const SizedBox(height: 10),
47-
MaterialButton(
48-
onPressed: () {
49-
Navigator.push(
50-
context,
51-
MaterialPageRoute(
52-
builder: (_) => const EditorTestPreviewUnfocused()));
53-
},
54-
color: Colors.blue,
55-
child: const Text(
56-
"Editor Screen 2",
57-
style: TextStyle(
58-
color: Colors.white,
59-
),
60-
),
61-
),
62-
],
63-
),
64-
),
65-
);
66-
}
67-
}
68-
69-
// HomeScreen Editor
70-
class EditorScreen extends StatefulWidget {
71-
const EditorScreen({Key? key}) : super(key: key);
72-
73-
@override
74-
_EditorScreenState createState() => _EditorScreenState();
24+
State<HomeScreen> createState() => _HomeScreenState();
7525
}
7626

77-
class _EditorScreenState extends State<EditorScreen> {
27+
class _HomeScreenState extends State<HomeScreen> {
7828
final TextEditingController _controller = TextEditingController();
7929

8030
@override
@@ -87,114 +37,41 @@ class _EditorScreenState extends State<EditorScreen> {
8737
Widget build(BuildContext context) {
8838
return Scaffold(
8939
appBar: AppBar(
90-
title: const Text("Markdown Editor"),
91-
actions: [
92-
IconButton(
93-
onPressed: () {
94-
Navigator.push(
95-
context,
96-
MaterialPageRoute(
97-
builder: (_) => SecondScreen(
98-
data: _controller.text,
99-
),
100-
),
101-
);
40+
title: const Text("Home Screen"),
41+
),
42+
body: Column(
43+
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
44+
children: [
45+
MarkdownFormField(
46+
controller: _controller,
47+
enableToolBar: true,
48+
emojiConvert: true,
49+
autoCloseAfterSelectEmoji: false,
50+
// maxLines: 10,
51+
// minLines: 1,
52+
// expands: true,
53+
onChanged: (s) {
54+
setState(() {});
10255
},
103-
icon: const Icon(Icons.view_compact),
10456
),
105-
],
106-
),
107-
body: SafeArea(
108-
child: Column(
109-
children: [
110-
Expanded(
111-
child: MarkdownFormField(
112-
controller: _controller,
113-
enableToolBar: true,
114-
emojiConvert: true,
115-
autoCloseAfterSelectEmoji: false,
116-
),
117-
),
118-
],
119-
),
120-
),
121-
);
122-
}
123-
}
124-
125-
class SecondScreen extends StatelessWidget {
126-
const SecondScreen({Key? key, required this.data}) : super(key: key);
127-
128-
final String data;
129-
130-
@override
131-
Widget build(BuildContext context) {
132-
return Scaffold(
133-
appBar: AppBar(
134-
title: const Text("Markdown Parse"),
135-
),
136-
body: MarkdownParse(
137-
data: data,
138-
onTapHastag: (String name, String match) {
139-
// example : #hashtag
140-
// name => hashtag
141-
// match => #hashtag
142-
},
143-
onTapMention: (String name, String match) {
144-
// example : @mention
145-
// name => mention
146-
// match => #mention
147-
},
148-
),
149-
);
150-
}
151-
}
152-
153-
// HomeScreen Editor
154-
class EditorTestPreviewUnfocused extends StatefulWidget {
155-
const EditorTestPreviewUnfocused({Key? key}) : super(key: key);
156-
157-
@override
158-
_EditorTestPreviewUnfocusedState createState() =>
159-
_EditorTestPreviewUnfocusedState();
160-
}
161-
162-
class _EditorTestPreviewUnfocusedState
163-
extends State<EditorTestPreviewUnfocused> {
164-
final TextEditingController _controller = TextEditingController();
165-
166-
@override
167-
void dispose() {
168-
_controller.dispose();
169-
super.dispose();
170-
}
171-
172-
@override
173-
Widget build(BuildContext context) {
174-
return Scaffold(
175-
appBar: AppBar(
176-
title: const Text("Markdown Editor"),
177-
),
178-
body: SafeArea(
179-
child: Column(
180-
children: [
181-
Container(
182-
height: 300,
183-
decoration: BoxDecoration(
184-
border: Border.all(color: Colors.black),
185-
borderRadius: BorderRadius.circular(20),
186-
),
187-
child: MarkdownFormField(
188-
controller: _controller,
189-
enableToolBar: true,
190-
emojiConvert: true,
191-
autoCloseAfterSelectEmoji: false,
192-
),
57+
SizedBox(
58+
height: 100,
59+
child: MarkdownParse(
60+
data: _controller.text,
61+
selectable: true,
62+
onTapHastag: (String name, String match) {
63+
// example : #hashtag
64+
// name => hashtag
65+
// match => #hashtag
66+
},
67+
onTapMention: (String name, String match) {
68+
// example : @mention
69+
// name => mention
70+
// match => #mention
71+
},
19372
),
194-
const SizedBox(height: 10),
195-
TextFormField(),
196-
],
197-
),
73+
),
74+
],
19875
),
19976
);
20077
}

lib/widgets/markdown_field.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class MarkdownField extends StatelessWidget {
1414
this.readOnly = false,
1515
this.cursorColor,
1616
this.focusNode,
17-
this.maxLines = 1,
17+
this.maxLines,
1818
this.minLines,
1919
this.expands = false,
2020
this.decoration = const InputDecoration(hintText: 'Type here...'),
@@ -122,8 +122,8 @@ class MarkdownField extends StatelessWidget {
122122
padding: padding,
123123
child: TextField(
124124
key: const ValueKey<String>("markdown_editor_plus"),
125-
maxLines: maxLines,
126-
minLines: minLines,
125+
maxLines: expands ? null : maxLines,
126+
minLines: expands ? null : minLines,
127127
expands: expands,
128128
focusNode: focusNode,
129129
controller: controller,

lib/widgets/markdown_form_field.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'markdown_field.dart';
3-
import 'markdown_parse.dart';
3+
import 'markdown_parse_body.dart';
44
import 'markdown_toolbar.dart';
55

66
class MarkdownFormField extends StatefulWidget {
@@ -20,7 +20,7 @@ class MarkdownFormField extends StatefulWidget {
2020
this.focusNode,
2121
this.toolbarBackground,
2222
this.expandableBackground,
23-
this.maxLines = 1,
23+
this.maxLines,
2424
this.minLines,
2525
this.expands = false,
2626
this.decoration = const InputDecoration(hintText: 'Type here...'),
@@ -159,11 +159,11 @@ class _MarkdownFormFieldState extends State<MarkdownFormField> {
159159

160160
@override
161161
void initState() {
162-
_internalController = widget.controller != null
163-
? widget.controller!
164-
: TextEditingController();
165-
_internalFocus = widget.focusNode != null ? widget.focusNode! : FocusNode();
166-
_internalFocus.addListener(() => _requestFocused());
162+
_internalController = widget.controller ?? TextEditingController();
163+
_internalFocus = widget.focusNode ?? FocusNode();
164+
165+
_internalFocus.addListener(_requestFocused);
166+
167167
super.initState();
168168
}
169169

@@ -187,8 +187,8 @@ class _MarkdownFormFieldState extends State<MarkdownFormField> {
187187
_internalFocus.requestFocus();
188188
setState(() {});
189189
},
190-
child: MarkdownParse(
191-
key: const ValueKey<String>("zmarkdownparse"),
190+
child: MarkdownParseBody(
191+
key: const ValueKey<String>("zmarkdown-parse-body"),
192192
data: _internalController.text == ""
193193
? "Type here. . ."
194194
: _internalController.text,

lib/widgets/markdown_parse.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import 'package:markdown/markdown.dart' as md;
55
import '../src/markdown_syntax.dart';
66
import 'image_network.dart';
77

8-
typedef MarkdownTapTagCallback = void Function(
9-
String name,
10-
String fullText,
11-
);
8+
typedef MarkdownTapTagCallback = void Function(String name, String fullText);
129

1310
class MarkdownParse extends StatelessWidget {
1411
/// Creates a scrolling widget that parses and displays Markdown.
@@ -28,6 +25,7 @@ class MarkdownParse extends StatelessWidget {
2825
this.imageBuilder,
2926
this.checkboxBuilder,
3027
this.builders = const <String, MarkdownElementBuilder>{},
28+
this.selectable = false,
3129
this.inlineSyntaxes,
3230
this.blockSyntaxes,
3331
this.checkboxIconSize,
@@ -102,13 +100,15 @@ class MarkdownParse extends StatelessWidget {
102100
final List<md.InlineSyntax>? inlineSyntaxes;
103101
final List<md.BlockSyntax>? blockSyntaxes;
104102

103+
final bool selectable;
104+
105105
@override
106106
Widget build(BuildContext context) {
107107
return Markdown(
108-
key: const Key("defaultmarkdownformatter"),
108+
key: const Key("default-markdown-formatter"),
109109
data: data,
110-
selectable: true,
111-
padding: const EdgeInsets.all(10),
110+
selectable: selectable,
111+
padding: const EdgeInsets.all(8),
112112
physics: physics,
113113
controller: controller,
114114
shrinkWrap: shrinkWrap,

0 commit comments

Comments
 (0)