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( BuildContext context, QuillController controller); // Common scaffold for all examples. class DemoScaffold extends StatefulWidget { /// Filename of the document to load into the editor. final String documentFilename; final DemoContentBuilder builder; final List actions; final Widget floatingActionButton; final bool showToolbar; const DemoScaffold({ Key key, @required this.documentFilename, @required this.builder, this.actions, this.showToolbar = true, this.floatingActionButton, }) : super(key: key); @override _DemoScaffoldState createState() => _DemoScaffoldState(); } class _DemoScaffoldState extends State { final _scaffoldKey = GlobalKey(); QuillController _controller; bool _loading = false; @override void didChangeDependencies() { super.didChangeDependencies(); if (_controller == null && !_loading) { _loading = true; _loadFromAssets(); } } @override void dispose() { _controller?.dispose(); super.dispose(); } Future _loadFromAssets() async { try { final result = await rootBundle.loadString('assets/${widget.documentFilename}'); 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 ?? []; 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 : QuillToolbar.basic(controller: _controller), actions: actions, ), floatingActionButton: widget.floatingActionButton, body: _loading ? Center(child: Text('Loading...')) : widget.builder(context, _controller), ); } }