|
1 |
| -# example |
| 1 | +# Example |
2 | 2 |
|
3 |
| -A new Flutter project. |
| 3 | +Detailed Example for flutter_html package |
4 | 4 |
|
5 |
| -## Getting Started |
| 5 | +# Basic Example |
| 6 | +```dart |
| 7 | + Widget html = Html( |
| 8 | + data: """ |
| 9 | + <div> |
| 10 | + <h1>Demo Page</h1> |
| 11 | + <p>This is a fantastic product that you should buy!</p> |
| 12 | + <h3>Features</h3> |
| 13 | + <ul> |
| 14 | + <li>It actually works</li> |
| 15 | + <li>It exists</li> |
| 16 | + <li>It doesn't cost much!</li> |
| 17 | + </ul> |
| 18 | + <!--You can pretty much put any html in here!--> |
| 19 | + </div> |
| 20 | + """, |
| 21 | + //Optional parameters: |
| 22 | + backgroundColor: Colors.white70, |
| 23 | + onLinkTap: (url) { |
| 24 | + // open url in a webview |
| 25 | + }, |
| 26 | + style: { |
| 27 | + "div": Style( |
| 28 | + block: Block( |
| 29 | + margin: EdgeInsets.all(16), |
| 30 | + border: Border.all(width: 6), |
| 31 | + backgroundColor: Colors.grey, |
| 32 | + ), |
| 33 | + textStyle: TextStyle( |
| 34 | + color: Colors.red, |
| 35 | + ), |
| 36 | + ), |
| 37 | + }, |
| 38 | + onImageTap: (src) { |
| 39 | + // Display the image in large form. |
| 40 | + }, |
| 41 | + ); |
| 42 | +``` |
6 | 43 |
|
7 |
| -This project is a starting point for a Flutter application. |
| 44 | +# Detailed Example |
8 | 45 |
|
9 |
| -A few resources to get you started if this is your first Flutter project: |
| 46 | +## Example HTML data string/body |
10 | 47 |
|
11 |
| -- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab) |
12 |
| -- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook) |
| 48 | +```html |
| 49 | +const htmlData = """ |
| 50 | +<h1>Header 1</h1> |
| 51 | +<h2>Header 2</h2> |
| 52 | +<h3>Header 3</h3> |
| 53 | +<h4>Header 4</h4> |
| 54 | +<h5>Header 5</h5> |
| 55 | +<h6>Header 6</h6> |
| 56 | +<h3>Ruby Support:</h3> |
| 57 | + <p> |
| 58 | + <ruby> |
| 59 | + 漢<rt>かん</rt> |
| 60 | + 字<rt>じ</rt> |
| 61 | + </ruby> |
| 62 | + is Japanese Kanji. |
| 63 | + </p> |
| 64 | + <h3>Support for <code>sub</code>/<code>sup</code></h3> |
| 65 | + Solve for <var>x<sub>n</sub></var>: log<sub>2</sub>(<var>x</var><sup>2</sup>+<var>n</var>) = 9<sup>3</sup> |
| 66 | + <p>One of the most <span>common</span> equations in all of physics is <br /><var>E</var>=<var>m</var><var>c</var><sup>2</sup>.</p> |
| 67 | + <h3>Table support (with custom styling!):</h3> |
| 68 | + <p> |
| 69 | + <q>Famous quote...</q> |
| 70 | + </p> |
| 71 | + <table> |
| 72 | + <colgroup> |
| 73 | + <col width="50%" /> |
| 74 | + <col width="25%" /> |
| 75 | + <col width="25%" /> |
| 76 | + </colgroup> |
| 77 | + <thead> |
| 78 | + <tr><th>One</th><th>Two</th><th>Three</th></tr> |
| 79 | + </thead> |
| 80 | + <tbody> |
| 81 | + <tr> |
| 82 | + <td>Data</td><td>Data</td><td>Data</td> |
| 83 | + </tr> |
| 84 | + <tr> |
| 85 | + <td>Data</td><td>Data</td><td>Data</td> |
| 86 | + </tr> |
| 87 | + </tbody> |
| 88 | + <tfoot> |
| 89 | + <tr><td>fData</td><td>fData</td><td>fData</td></tr> |
| 90 | + </tfoot> |
| 91 | + </table> |
| 92 | + <h3>Custom Element Support:</h3> |
| 93 | + <flutter></flutter> |
| 94 | + <flutter horizontal></flutter> |
| 95 | + <h3>SVG support:</h3> |
| 96 | + <svg id='svg1' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'> |
| 97 | + <circle r="32" cx="35" cy="65" fill="#F00" opacity="0.5"/> |
| 98 | + <circle r="32" cx="65" cy="65" fill="#0F0" opacity="0.5"/> |
| 99 | + <circle r="32" cx="50" cy="35" fill="#00F" opacity="0.5"/> |
| 100 | + </svg> |
| 101 | + <h3>List support:</h3> |
| 102 | + <ol> |
| 103 | + <li>This</li> |
| 104 | + <li><p>is</p></li> |
| 105 | + <li>an</li> |
| 106 | + <li> |
| 107 | + ordered |
| 108 | + <ul> |
| 109 | + <li>With<br /><br />...</li> |
| 110 | + <li>a</li> |
| 111 | + <li>nested</li> |
| 112 | + <li>unordered |
| 113 | + <ol> |
| 114 | + <li>With a nested</li> |
| 115 | + <li>ordered list.</li> |
| 116 | + </ol> |
| 117 | + </li> |
| 118 | + <li>list</li> |
| 119 | + </ul> |
| 120 | + </li> |
| 121 | + <li>list! Lorem ipsum dolor sit amet.</li> |
| 122 | + <li><h2>Header 2</h2></li> |
| 123 | + <h2><li>Header 2</li></h2> |
| 124 | + </ol> |
| 125 | + <h3>Link support:</h3> |
| 126 | + <p> |
| 127 | + Linking to <a href='https://github.com'>websites</a> has never been easier. |
| 128 | + </p> |
| 129 | + <h3>Image support:</h3> |
| 130 | + <p> |
| 131 | + <img alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' /> |
| 132 | + <a href='https://google.com'><img alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' /></a> |
| 133 | + <img alt='Alt Text of an intentionally broken image' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30d' /> |
| 134 | + </p> |
| 135 | + <!-- |
| 136 | + <h3>Video support:</h3> |
| 137 | + <video controls> |
| 138 | + <source src="https://www.w3schools.com/html/mov_bbb.mp4" /> |
| 139 | + </video> |
| 140 | + <h3>Audio support:</h3> |
| 141 | + <audio controls> |
| 142 | + <source src="https://www.w3schools.com/html/horse.mp3" /> |
| 143 | + </audio> |
| 144 | + <h3>IFrame support:</h3> |
| 145 | + <iframe src="https://google.com"></iframe> |
| 146 | + --> |
| 147 | +"""; |
| 148 | +``` |
13 | 149 |
|
14 |
| -For help getting started with Flutter, view our |
15 |
| -[online documentation](https://flutter.dev/docs), which offers tutorials, |
16 |
| -samples, guidance on mobile development, and a full API reference. |
| 150 | + |
| 151 | +## How to use |
| 152 | +```dart |
| 153 | + return new Scaffold( |
| 154 | + appBar: AppBar( |
| 155 | + title: Text('flutter_html Example'), |
| 156 | + centerTitle: true, |
| 157 | + ), |
| 158 | + body: SingleChildScrollView( |
| 159 | + child: Html( |
| 160 | + data: htmlData, |
| 161 | + //Optional parameters: |
| 162 | + style: { |
| 163 | + "html": Style( |
| 164 | + backgroundColor: Colors.black12, |
| 165 | +// color: Colors.white, |
| 166 | + ), |
| 167 | +// "h1": Style( |
| 168 | +// textAlign: TextAlign.center, |
| 169 | +// ), |
| 170 | + "table": Style( |
| 171 | + backgroundColor: Color.fromARGB(0x50, 0xee, 0xee, 0xee), |
| 172 | + ), |
| 173 | + "tr": Style( |
| 174 | + border: Border(bottom: BorderSide(color: Colors.grey)), |
| 175 | + ), |
| 176 | + "th": Style( |
| 177 | + padding: EdgeInsets.all(6), |
| 178 | + backgroundColor: Colors.grey, |
| 179 | + ), |
| 180 | + "td": Style( |
| 181 | + padding: EdgeInsets.all(6), |
| 182 | + ), |
| 183 | + "var": Style(fontFamily: 'serif'), |
| 184 | + }, |
| 185 | + customRender: { |
| 186 | + "flutter": (RenderContext context, Widget child, attributes, _) { |
| 187 | + return FlutterLogo( |
| 188 | + style: (attributes['horizontal'] != null) |
| 189 | + ? FlutterLogoStyle.horizontal |
| 190 | + : FlutterLogoStyle.markOnly, |
| 191 | + textColor: context.style.color, |
| 192 | + size: context.style.fontSize.size * 5, |
| 193 | + ); |
| 194 | + }, |
| 195 | + }, |
| 196 | + onLinkTap: (url) { |
| 197 | + print("Opening $url..."); |
| 198 | + }, |
| 199 | + onImageTap: (src) { |
| 200 | + print(src); |
| 201 | + }, |
| 202 | + onImageError: (exception, stackTrace) { |
| 203 | + print(exception); |
| 204 | + }, |
| 205 | + ), |
| 206 | + ), |
| 207 | + ); |
| 208 | +``` |
0 commit comments