如何在InAppWebView中从String加载HTML?

问题描述 投票:0回答:1

我有时会收到 HTML 作为网络请求的响应。我遇到过需要在 InAppWebView 中显示 HTML 内容的场景。有没有什么捷径可以做到这一点。

我试图在互联网上寻找答案,但找不到。

flutter flutter-inappwebview
1个回答
0
投票

要显示 HTML 内容,您可以使用 Html Widget 或依赖较少的 Core Widget。 Html 小部件核心

这里是来自 pub.dev 网站的使用示例:

HtmlWidget(
  // the first parameter (`html`) is required
  '''
  <h3>Heading</h3>
  <p>
    A paragraph with <strong>strong</strong>, <em>emphasized</em>
    and <span style="color: red">colored</span> text.
  </p>
  ''',

  // all other parameters are optional, a few notable params:

  // specify custom styling for an element
  // see supported inline styling below
  customStylesBuilder: (element) {
    if (element.classes.contains('foo')) {
      return {'color': 'red'};
    }

    return null;
  },

  customWidgetBuilder: (element) {
    if (element.attributes['foo'] == 'bar') {
      // render a custom widget that takes the full width
      return FooBarWidget();
    }

    if (element.attributes['fizz'] == 'buzz') {
      // render a custom widget that inlines with surrounding text
      return InlineCustomWidget(
        child: FizzBuzzWidget(),
      )
    }

    return null;
  },

  // this callback will be triggered when user taps a link
  onTapUrl: (url) => print('tapped $url'),

  // select the render mode for HTML body
  // by default, a simple `Column` is rendered
  // consider using `ListView` or `SliverList` for better performance
  renderMode: RenderMode.column,

  // set the default styling for text
  textStyle: TextStyle(fontSize: 14),
),
© www.soinside.com 2019 - 2024. All rights reserved.