|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | + |
| 3 | +import 'package:flutter_markdown/flutter_markdown.dart' show MarkdownBody; |
| 4 | + |
| 5 | +class HackerNewsEditor extends StatefulWidget { |
| 6 | + final String labelText; |
| 7 | + final String initialValue; |
| 8 | + final ValueChanged<String> onChanged; |
| 9 | + |
| 10 | + HackerNewsEditor ({ |
| 11 | + this.labelText = 'Text', |
| 12 | + this.initialValue, |
| 13 | + this.onChanged, |
| 14 | + Key key, |
| 15 | + }): super(key: key); |
| 16 | + |
| 17 | + @override |
| 18 | + createState () => new HackerNewsEditorState(); |
| 19 | +} |
| 20 | + |
| 21 | +class HackerNewsEditorState extends State<HackerNewsEditor> { |
| 22 | + final TextEditingController _controller = new TextEditingController(); |
| 23 | + |
| 24 | + @override |
| 25 | + void initState () { |
| 26 | + super.initState(); |
| 27 | + |
| 28 | + this._controller.text = widget.initialValue; |
| 29 | + this._controller.addListener(() { |
| 30 | + setState(() {}); |
| 31 | + if (widget.onChanged != null) widget.onChanged(this._controller.text); |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + @override |
| 36 | + void dispose () { |
| 37 | + super.dispose(); |
| 38 | + this._controller.dispose(); |
| 39 | + } |
| 40 | + |
| 41 | + String get value => this._controller.text ?? ''; |
| 42 | + |
| 43 | + void _padSelection (String padding) { |
| 44 | + final selection = this._controller.selection.textInside(this._controller.text); |
| 45 | + final modified = '$padding$selection$padding'; |
| 46 | + final modifiedOffset = this._controller.selection.extentOffset + padding.length * 2; |
| 47 | + this._controller.text = |
| 48 | + this._controller.selection.textBefore(this._controller.text) + |
| 49 | + modified + |
| 50 | + this._controller.selection.textAfter(this._controller.text); |
| 51 | + this._controller.selection = new TextSelection.fromPosition(new TextPosition( |
| 52 | + offset: modifiedOffset, |
| 53 | + )); |
| 54 | + } |
| 55 | + |
| 56 | + void _createBlock (String blockString) { |
| 57 | + final selection = this._controller.selection.textInside(this._controller.text); |
| 58 | + final modified = selection.replaceAll('\n', '\n$blockString'); |
| 59 | + this._controller.text = |
| 60 | + this._controller.selection.textBefore(this._controller.text) + |
| 61 | + '\n$blockString$modified\n' + |
| 62 | + this._controller.selection.textAfter(this._controller.text); |
| 63 | + } |
| 64 | + |
| 65 | + void _insertQuote () => this._createBlock('> '); |
| 66 | + |
| 67 | + void _insertListBullet () => this._createBlock('- '); |
| 68 | + |
| 69 | + void _insertListNumber () => this._createBlock('1. '); |
| 70 | + |
| 71 | + void _makeSelectionBold () => this._padSelection('**'); |
| 72 | + |
| 73 | + void _makeSelectionItalic () => this._padSelection('*'); |
| 74 | + |
| 75 | + void _makeSelectionUnderlined () => this._padSelection('__'); |
| 76 | + |
| 77 | + void _makeSelectionStruckThrough () => this._padSelection('~~'); |
| 78 | + |
| 79 | + @override |
| 80 | + Widget build (BuildContext context) { |
| 81 | + return new DefaultTabController( |
| 82 | + length: 2, |
| 83 | + child: new Column( |
| 84 | + children: <Widget>[ |
| 85 | + new Expanded( |
| 86 | + child: new TabBarView( |
| 87 | + children: <Widget>[ |
| 88 | + this._buildEditor(context), |
| 89 | + this._buildPreview(context), |
| 90 | + ], |
| 91 | + ), |
| 92 | + ), |
| 93 | + new Container( |
| 94 | + color: Theme.of(context).primaryColor, |
| 95 | + child: new TabBar( |
| 96 | + tabs: <Tab>[ |
| 97 | + new Tab( |
| 98 | + icon: const Icon(Icons.edit), |
| 99 | + ), |
| 100 | + new Tab( |
| 101 | + icon: const Icon(Icons.remove_red_eye), |
| 102 | + ), |
| 103 | + ], |
| 104 | + ), |
| 105 | + ), |
| 106 | + ], |
| 107 | + ), |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + Widget _buildEditor (BuildContext context) { |
| 112 | + return new Column( |
| 113 | + children: <Widget>[ |
| 114 | + new Expanded( |
| 115 | + child: new Padding( |
| 116 | + padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0), |
| 117 | + child: new TextField( |
| 118 | + controller: this._controller, |
| 119 | + autofocus: true, |
| 120 | + autocorrect: true, |
| 121 | + keyboardType: TextInputType.multiline, |
| 122 | + maxLines: null, |
| 123 | + decoration: new InputDecoration( |
| 124 | + labelText: widget.labelText, |
| 125 | + border: const OutlineInputBorder(), |
| 126 | + ), |
| 127 | + ), |
| 128 | + ), |
| 129 | + ), |
| 130 | + new SizedBox( |
| 131 | + height: 48.0, |
| 132 | + child: new ListView( |
| 133 | + scrollDirection: Axis.horizontal, |
| 134 | + children: <Widget>[ |
| 135 | + new IconButton( |
| 136 | + icon: const Icon(Icons.format_bold), |
| 137 | + onPressed: _makeSelectionBold, |
| 138 | + ), |
| 139 | + new IconButton( |
| 140 | + icon: const Icon(Icons.format_italic), |
| 141 | + onPressed: _makeSelectionItalic, |
| 142 | + ), |
| 143 | + new IconButton( |
| 144 | + icon: const Icon(Icons.format_quote), |
| 145 | + onPressed: this._insertQuote, |
| 146 | + ), |
| 147 | + new IconButton( |
| 148 | + icon: const Icon(Icons.format_strikethrough), |
| 149 | + onPressed: this._makeSelectionStruckThrough, |
| 150 | + ), |
| 151 | + new IconButton( |
| 152 | + icon: const Icon(Icons.format_underlined), |
| 153 | + onPressed: this._makeSelectionUnderlined, |
| 154 | + ), |
| 155 | + new IconButton( |
| 156 | + icon: const Icon(Icons.format_list_bulleted), |
| 157 | + onPressed: this._insertListBullet, |
| 158 | + ), |
| 159 | + new IconButton( |
| 160 | + icon: const Icon(Icons.format_list_numbered), |
| 161 | + onPressed: this._insertListNumber, |
| 162 | + ), |
| 163 | + ], |
| 164 | + ), |
| 165 | + ), |
| 166 | + ], |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + Widget _buildPreview (BuildContext context) { |
| 171 | + return new Padding( |
| 172 | + padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0), |
| 173 | + child: new MarkdownBody(data: this._controller.text), |
| 174 | + ); |
| 175 | + } |
| 176 | +} |
0 commit comments