【问题标题】:When I make Widget build in flutter, can I make it without Scaffold?当我在 Flutter 中构建 Widget 时,我可以在没有 Scaffold 的情况下制作它吗?
【发布时间】:2021-10-12 00:30:21
【问题描述】:

我正在尝试制作一个只有 webview 的应用程序。

1)。当我构建 Widget 时,我可以像下面的代码那样在没有 Scaffold 的情况下构建它吗?

2)。我什至想减少一帧。 如果我在没有脚手架的情况下进行构建,是否有助于减少框架?

还有 3)。我只制作网页视图,我也可以删除 SafeArea 吗?

return Container(

    child: SafeArea(

         child: WebView(
            ..
         ),
    )
);

}

【问题讨论】:

  • 如果我理解你的问题,你想在 Flutter 上创建一个 webview,你问的是 Scaffold 是否会影响性能?
  • 是的,没错。我担心不使用脚手架时性能下降

标签: flutter webview scaffold


【解决方案1】:

尽快回答您的问题:

  1. 如果您的小部件是需要Material 父小部件的小部件(例如TextField),则需要ScaffoldMaterial 小部件。如果您的小部件不是,则否。

    如果您省略 Material 父小部件,则会发生错误(有关详细信息,请参阅帖子末尾)。

  2. 在这种情况下,由于您的小部件是 WebView(我假设它来自此包 webview_flutter),因此您不需要 Scaffold

  3. 你可以像这样简单地使用它:

import 'dart:io';
import 'package:webview_flutter/webview_flutter.dart';

class WebViewExample extends StatefulWidget {
  @override
  WebViewExampleState createState() => WebViewExampleState();
}

class WebViewExampleState extends State<WebViewExample> {
  @override
  Widget build(BuildContext context) {
    return WebView(
      initialUrl: 'https://flutter.dev',
    );
  }
}

P/s:省略 Material 父级时的错误如下:

I/flutter ( 5187): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 5187): The following assertion was thrown building InputDecorator(decoration: InputDecoration(hintText:
I/flutter ( 5187): "Type something"); baseStyle: null; isFocused: false; isEmpty: true; dirty):
I/flutter ( 5187): No Material widget found.
I/flutter ( 5187): InputDecorator widgets require a Material widget ancestor.
I/flutter ( 5187): In material design, most widgets are conceptually "printed" on a sheet of material. In Flutter's
I/flutter ( 5187): material library, that material is represented by the Material widget. It is the Material widget
I/flutter ( 5187): that renders ink splashes, for instance. Because of this, many material library widgets require that
I/flutter ( 5187): there be a Material widget in the tree above them.
I/flutter ( 5187): To introduce a Material widget, you can either directly include one, or use a widget that contains
I/flutter ( 5187): Material itself, such as a Card, Dialog, Drawer, or Scaffold.

【讨论】:

  • 我当然使用 Material 。我想像你写的代码一样使用它。但是我担心不使用脚手架时性能会下降
  • @아이엠초코 不会影响性能。当您的应用不必渲染 Scaffold 并直接转到 WebView 时,它可能会节省一些时间
猜你喜欢
  • 2021-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
  • 2017-04-29
  • 1970-01-01
  • 2015-08-27
  • 2015-08-29
相关资源
最近更新 更多