Update example main.dart

pull/13/head
singerdmx 4 years ago
parent 0d5884f321
commit c4d6edc60e
  1. 122
      example/main.dart
  2. 17
      lib/widgets/controller.dart
  3. 17
      lib/widgets/editor.dart

@ -1,11 +1,7 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:app/pages/read_only_page.dart';
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/editor.dart';
import 'package:flutter_quill/widgets/toolbar.dart';
@ -16,112 +12,26 @@ class HomePage extends StatefulWidget {
}
class _HomePageState extends State<HomePage> {
QuillController _controller;
final FocusNode _focusNode = FocusNode();
@override
void initState() {
super.initState();
_loadFromAssets();
}
Future<void> _loadFromAssets() async {
try {
final result = await rootBundle.loadString('assets/sample_data.json');
final doc = Document.fromJson(jsonDecode(result));
setState(() {
_controller = QuillController(
document: doc, selection: TextSelection.collapsed(offset: 0));
});
} catch (error) {
final doc = Document()..insert(0, 'Empty asset');
setState(() {
_controller = QuillController(
document: doc, selection: TextSelection.collapsed(offset: 0));
});
}
}
QuillController _controller = QuillController.basic();
@override
Widget build(BuildContext context) {
if (_controller == null) {
return Scaffold(body: Center(child: Text('Loading...')));
}
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.grey.shade800,
elevation: 0,
centerTitle: false,
title: Text(
'Flutter Quill',
),
actions: [],
),
drawer: Material(
color: Colors.grey.shade800,
child: _buildMenuBar(context),
),
body: _buildWelcomeEditor(context),
);
body: Column(
children: [
QuillToolbar.basic(
controller: _controller, uploadFileCallback: _uploadImageCallBack),
Expanded(
child: Container(
child: QuillEditor.basic(_controller),
),
)
],
));
}
Widget _buildWelcomeEditor(BuildContext context) {
return Column(
children: [
QuillToolbar.basic(
controller: _controller,
uploadFileCallback: _fakeUploadImageCallBack),
Divider(height: 1, thickness: 1, color: Colors.grey.shade200),
Expanded(
child: Container(
color: Colors.white,
padding: const EdgeInsets.only(left: 16.0, right: 16.0),
child: QuillEditor(
controller: _controller,
scrollController: ScrollController(),
scrollable: true,
focusNode: _focusNode,
autoFocus: true,
readOnly: false,
enableInteractiveSelection: true,
expands: false,
padding: EdgeInsets.zero,
),
),
),
],
);
Future<String> _uploadImageCallBack(File file) async {
// call upload file API and return file's absolute url
return new Completer<String>().future;
}
Future<String> _fakeUploadImageCallBack(File file) async {
print(file);
var completer = new Completer<String>();
completer.complete(
'https://user-images.githubusercontent.com/122956/72955931-ccc07900-3d52-11ea-89b1-d468a6e2aa2b.png');
return completer.future;
}
Widget _buildMenuBar(BuildContext context) {
final itemStyle = TextStyle(color: Colors.white);
return ListView(
children: [
ListTile(
title: Text('Read only demo', style: itemStyle),
dense: true,
visualDensity: VisualDensity.compact,
onTap: _readOnly,
)
],
);
}
void _readOnly() {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => ReadOnlyPage(),
),
);
}
}
}

@ -17,11 +17,16 @@ class QuillController extends ChangeNotifier {
: assert(document != null),
assert(selection != null);
factory QuillController.basic() {
return QuillController(
document: Document(), selection: TextSelection.collapsed(offset: 0));
}
TextEditingValue get plainTextEditingValue => TextEditingValue(
text: document.toPlainText(),
selection: selection,
composing: TextRange.empty,
);
text: document.toPlainText(),
selection: selection,
composing: TextRange.empty,
);
Style getSelectionStyle() {
return document
@ -37,7 +42,7 @@ class QuillController extends ChangeNotifier {
try {
delta = document.replace(index, len, data);
} catch (e) {
print ('document.replace failed: $e');
print('document.replace failed: $e');
throw e;
}
if (delta != null &&
@ -71,7 +76,7 @@ class QuillController extends ChangeNotifier {
ChangeSource.LOCAL,
);
} catch (e) {
print ('getPositionDelta or getPositionDelta error: $e');
print('getPositionDelta or getPositionDelta error: $e');
throw e;
}
}

@ -113,7 +113,7 @@ class QuillEditor extends StatefulWidget {
QuillEditor(
{@required this.controller,
this.focusNode,
@required this.focusNode,
@required this.scrollController,
@required this.scrollable,
@required this.padding,
@ -132,10 +132,25 @@ class QuillEditor extends StatefulWidget {
: assert(controller != null),
assert(scrollController != null),
assert(scrollable != null),
assert(focusNode != null),
assert(autoFocus != null),
assert(readOnly != null),
assert(embedBuilder != null);
factory QuillEditor.basic(QuillController controller) {
return QuillEditor(
controller: controller,
scrollController: ScrollController(),
scrollable: true,
focusNode: FocusNode(),
autoFocus: true,
readOnly: false,
// change to true to be view only mode
enableInteractiveSelection: true,
expands: false,
padding: EdgeInsets.zero);
}
@override
_QuillEditorState createState() => _QuillEditorState();
}

Loading…
Cancel
Save