2021-05-30 04:36:44 +07:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2021-05-27 05:42:34 +07:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:rich_editor/rich_editor.dart';
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
runApp(MyApp());
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyApp extends StatelessWidget {
|
|
|
|
// This widget is the root of your application.
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return MaterialApp(
|
|
|
|
title: 'Flutter Demo',
|
|
|
|
theme: ThemeData(
|
|
|
|
primarySwatch: Colors.blue,
|
|
|
|
),
|
2021-05-27 17:20:54 +07:00
|
|
|
home: MyHomePage(title: 'Rich Editor Demo'),
|
2021-05-27 05:42:34 +07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyHomePage extends StatefulWidget {
|
|
|
|
MyHomePage({Key? key, required this.title}) : super(key: key);
|
|
|
|
final String title;
|
|
|
|
|
|
|
|
@override
|
|
|
|
_MyHomePageState createState() => _MyHomePageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _MyHomePageState extends State<MyHomePage> {
|
2021-05-30 04:36:44 +07:00
|
|
|
GlobalKey<RichEditorState> keyEditor = GlobalKey();
|
|
|
|
|
2021-05-27 05:42:34 +07:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2021-05-30 04:36:44 +07:00
|
|
|
appBar: AppBar(
|
|
|
|
title: Text(widget.title),
|
|
|
|
actions: [
|
|
|
|
PopupMenuButton(
|
|
|
|
child: IconButton(
|
|
|
|
icon: Icon(Icons.more_vert),
|
|
|
|
onPressed: null,
|
|
|
|
disabledColor: Colors.white,
|
|
|
|
),
|
|
|
|
itemBuilder: (context) {
|
|
|
|
return [
|
|
|
|
PopupMenuItem(
|
|
|
|
child: Text('Get HTML'),
|
|
|
|
value: 0,
|
|
|
|
),
|
|
|
|
PopupMenuItem(
|
|
|
|
child: Text('Clear content'),
|
|
|
|
value: 1,
|
|
|
|
),
|
|
|
|
PopupMenuItem(
|
|
|
|
child: Text('Hide keyboard'),
|
|
|
|
value: 2,
|
|
|
|
),
|
|
|
|
PopupMenuItem(
|
|
|
|
child: Text('Show Keyboard'),
|
|
|
|
value: 3,
|
|
|
|
),
|
|
|
|
];
|
|
|
|
},
|
|
|
|
onSelected: (val) async {
|
2021-06-01 04:19:49 +07:00
|
|
|
switch (val) {
|
|
|
|
case 0:
|
2021-06-03 06:37:45 +07:00
|
|
|
String? html = await keyEditor.currentState?.getHtml();
|
|
|
|
print(html);
|
2021-06-01 04:19:49 +07:00
|
|
|
break;
|
|
|
|
case 1:
|
2021-06-03 06:37:45 +07:00
|
|
|
await keyEditor.currentState?.clear();
|
2021-06-01 04:19:49 +07:00
|
|
|
break;
|
|
|
|
case 2:
|
2021-06-03 06:37:45 +07:00
|
|
|
await keyEditor.currentState?.unFocus();
|
2021-06-01 04:19:49 +07:00
|
|
|
break;
|
|
|
|
case 3:
|
2021-06-03 06:37:45 +07:00
|
|
|
await keyEditor.currentState?.focus();
|
2021-06-01 04:19:49 +07:00
|
|
|
break;
|
2021-05-30 04:36:44 +07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
body: RichEditor(
|
|
|
|
key: keyEditor,
|
2021-06-01 04:19:49 +07:00
|
|
|
// value: '''
|
|
|
|
// <h1>Heading 1</h1>
|
|
|
|
// <h2>Heading 2</h2>
|
|
|
|
// <h3>Heading 3</h3>
|
|
|
|
// <h4>Heading 4</h4>
|
|
|
|
// <h5>Heading 5</h5>
|
|
|
|
// <h6>Heading 6</h6>
|
2021-06-03 06:32:29 +07:00
|
|
|
// ''', // initial HTML data
|
2021-06-01 04:19:49 +07:00
|
|
|
placeholder: 'Start typing',
|
2021-06-03 06:32:29 +07:00
|
|
|
// backgroundColor: Colors.blueGrey, // Editor's bg color
|
2021-06-01 04:19:49 +07:00
|
|
|
// baseTextColor: Colors.white,
|
2021-06-03 06:32:29 +07:00
|
|
|
// editor padding
|
2021-06-03 06:13:12 +07:00
|
|
|
padding: EdgeInsets.symmetric(horizontal: 5.0),
|
2021-06-03 06:32:29 +07:00
|
|
|
// font name
|
2021-06-03 06:13:12 +07:00
|
|
|
baseFontFamily: 'sans-serif',
|
2021-06-03 06:32:29 +07:00
|
|
|
// Position of the editing bar (BarPosition.TOP or BarPosition.BOTTOM)
|
|
|
|
barPosition: BarPosition.TOP,
|
2021-05-30 04:36:44 +07:00
|
|
|
// You can return a Link (maybe you need to upload the image to your
|
|
|
|
// storage before displaying in the editor or you can also use base64
|
|
|
|
getImageUrl: (image) {
|
|
|
|
String link = 'https://avatars.githubusercontent.com/u/24323581?v=4';
|
|
|
|
String base64 = base64Encode(image.readAsBytesSync());
|
|
|
|
String base64String = 'data:image/png;base64, $base64';
|
|
|
|
return base64String;
|
|
|
|
},
|
|
|
|
),
|
2021-05-27 05:42:34 +07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|