markdown.dart 3.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';

import 'markdown_raw.dart';
import 'markdown_style.dart';

/// A [Widget] that renders markdown formatted text. It supports all standard
/// markdowns from the original markdown specification found here:
/// https://daringfireball.net/projects/markdown/ The rendered markdown is
/// placed in a padded scrolling view port. If you do not want the scrolling
/// behaviour, use the [MarkdownBody] class instead.
class Markdown extends MarkdownRaw {

  /// Creates a new Markdown [Widget] that renders the markdown formatted string
  /// passed in as [data]. By default the markdown will be rendered using the
  /// styles from the current theme, but you can optionally pass in a custom
  /// [markdownStyle] that specifies colors and fonts to use. Code blocks are
  /// by default not using syntax highlighting, but it's possible to pass in
  /// a custom [syntaxHighlighter].
  ///
  ///     new Markdown(data: "Hello _world_!");
  Markdown({
    String data,
    SyntaxHighlighter syntaxHighlighter,
28 29
    MarkdownStyle markdownStyle,
    MarkdownLinkCallback onTapLink
30 31 32
  }) : super(
    data: data,
    syntaxHighlighter: syntaxHighlighter,
33 34
    markdownStyle: markdownStyle,
    onTapLink: onTapLink
35 36
  );

37
  @override
38 39 40
  MarkdownBody createMarkdownBody({
    String data,
    MarkdownStyle markdownStyle,
41 42
    SyntaxHighlighter syntaxHighlighter,
    MarkdownLinkCallback onTapLink
43 44 45 46
  }) {
    return new MarkdownBody(
      data: data,
      markdownStyle: markdownStyle,
47 48
      syntaxHighlighter: syntaxHighlighter,
      onTapLink: onTapLink
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    );
  }
}

/// A [Widget] that renders markdown formatted text. It supports all standard
/// markdowns from the original markdown specification found here:
/// https://daringfireball.net/projects/markdown/ This class doesn't implement
/// any scrolling behavior, if you want scrolling either wrap the widget in
/// a [ScrollableViewport] or use the [Markdown] widget.
class MarkdownBody extends MarkdownBodyRaw {

  /// Creates a new Markdown [Widget] that renders the markdown formatted string
  /// passed in as [data]. By default the markdown will be rendered using the
  /// styles from the current theme, but you can optionally pass in a custom
  /// [markdownStyle] that specifies colors and fonts to use. Code blocks are
  /// by default not using syntax highlighting, but it's possible to pass in
  /// a custom [syntaxHighlighter].
  ///
  /// Typically, you may want to wrap the [MarkdownBody] widget in a [Padding] and
  /// a [ScrollableViewport], or use the [Markdown] class
  ///
  ///     new ScrollableViewport(
  ///       child: new Padding(
72
  ///         padding: new EdgeInsets.all(16.0),
73 74 75 76 77 78
  ///         child: new Markdown(data: markdownSource)
  ///       )
  ///     )
  MarkdownBody({
    String data,
    SyntaxHighlighter syntaxHighlighter,
79 80
    MarkdownStyle markdownStyle,
    MarkdownLinkCallback onTapLink
81 82 83
  }) : super(
    data: data,
    syntaxHighlighter: syntaxHighlighter,
84 85
    markdownStyle: markdownStyle,
    onTapLink: onTapLink
86 87
  );

88
  @override
89 90 91 92
  MarkdownStyle createDefaultStyle(BuildContext context) {
    return new MarkdownStyle.defaultFromTheme(Theme.of(context));
  }
}