dartlangeditorflutterflutter-appsflutter-examplesflutter-packageflutter-widgetquillquill-deltaquilljsreactquillrich-textrich-text-editorwysiwygwysiwyg-editor
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
2.8 KiB
105 lines
2.8 KiB
4 years ago
|
import 'dart:convert';
|
||
|
|
||
|
import 'package:flutter/material.dart';
|
||
|
import 'package:flutter/services.dart';
|
||
|
import 'package:flutter_quill/models/documents/document.dart';
|
||
|
import 'package:flutter_quill/widgets/controller.dart';
|
||
|
import 'package:flutter_quill/widgets/toolbar.dart';
|
||
|
|
||
|
typedef DemoContentBuilder = Widget Function(
|
||
4 years ago
|
BuildContext context, QuillController? controller);
|
||
4 years ago
|
|
||
|
// Common scaffold for all examples.
|
||
|
class DemoScaffold extends StatefulWidget {
|
||
|
/// Filename of the document to load into the editor.
|
||
|
final String documentFilename;
|
||
|
final DemoContentBuilder builder;
|
||
4 years ago
|
final List<Widget>? actions;
|
||
|
final Widget? floatingActionButton;
|
||
4 years ago
|
final bool showToolbar;
|
||
|
|
||
|
const DemoScaffold({
|
||
4 years ago
|
Key? key,
|
||
|
required this.documentFilename,
|
||
|
required this.builder,
|
||
4 years ago
|
this.actions,
|
||
|
this.showToolbar = true,
|
||
|
this.floatingActionButton,
|
||
|
}) : super(key: key);
|
||
|
|
||
|
@override
|
||
|
_DemoScaffoldState createState() => _DemoScaffoldState();
|
||
|
}
|
||
|
|
||
|
class _DemoScaffoldState extends State<DemoScaffold> {
|
||
|
final _scaffoldKey = GlobalKey<ScaffoldState>();
|
||
4 years ago
|
QuillController? _controller;
|
||
4 years ago
|
|
||
|
bool _loading = false;
|
||
|
|
||
|
@override
|
||
|
void didChangeDependencies() {
|
||
|
super.didChangeDependencies();
|
||
|
if (_controller == null && !_loading) {
|
||
|
_loading = true;
|
||
|
_loadFromAssets();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
void dispose() {
|
||
|
_controller?.dispose();
|
||
|
super.dispose();
|
||
|
}
|
||
|
|
||
|
Future<void> _loadFromAssets() async {
|
||
|
try {
|
||
|
final result =
|
||
4 years ago
|
await rootBundle.loadString('assets/${widget.documentFilename}');
|
||
4 years ago
|
final doc = Document.fromJson(jsonDecode(result));
|
||
|
setState(() {
|
||
|
_controller = QuillController(
|
||
|
document: doc, selection: TextSelection.collapsed(offset: 0));
|
||
|
_loading = false;
|
||
|
});
|
||
|
} catch (error) {
|
||
|
final doc = Document()..insert(0, 'Empty asset');
|
||
|
setState(() {
|
||
|
_controller = QuillController(
|
||
|
document: doc, selection: TextSelection.collapsed(offset: 0));
|
||
|
_loading = false;
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
final actions = widget.actions ?? <Widget>[];
|
||
|
return Scaffold(
|
||
|
key: _scaffoldKey,
|
||
|
appBar: AppBar(
|
||
|
elevation: 0,
|
||
|
backgroundColor: Theme.of(context).canvasColor,
|
||
|
centerTitle: false,
|
||
|
titleSpacing: 0,
|
||
|
leading: IconButton(
|
||
|
icon: Icon(
|
||
|
Icons.chevron_left,
|
||
|
color: Colors.grey.shade800,
|
||
|
size: 18,
|
||
|
),
|
||
|
onPressed: () => Navigator.pop(context),
|
||
|
),
|
||
|
title: _loading || widget.showToolbar == false
|
||
|
? null
|
||
4 years ago
|
: QuillToolbar.basic(controller: _controller!),
|
||
4 years ago
|
actions: actions,
|
||
|
),
|
||
|
floatingActionButton: widget.floatingActionButton,
|
||
|
body: _loading
|
||
|
? Center(child: Text('Loading...'))
|
||
|
: widget.builder(context, _controller),
|
||
|
);
|
||
|
}
|
||
|
}
|