在 InAppWebView 上堆叠小部件

Stacking Widgets Over InAppWebView

我有一个显示 InAppWebView 的页面。我想在它上面堆叠一个小部件。当我打开页面时,它会瞬间显示堆叠的小部件,但一旦加载 Web 视图,堆叠的小部件就会消失。我打开了 flutter inspector,页面上确实存在这个小部件,它看起来就像隐藏在 web 视图下面。在 InAppWebView 顶部堆叠小部件时,我需要在这里采取不同的方法吗?

class UnityFormViewPage extends StatefulWidget {
  final UnityForm _selectedForm;

  UnityFormViewPage(this._selectedForm);

  @override
  _UnityFormViewPageState createState() => _UnityFormViewPageState();
}

class _UnityFormViewPageState extends State<UnityFormViewPage> {
  String currentUrl = '';

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Scaffold(
          appBar: AppBar(
            title: Text(widget._selectedForm.title),
            leading: IconButton(
                icon: Icon(Icons.arrow_back),
                onPressed: () {
                  Navigator.pop(context);
                }),
          ),
          body: Stack(
            children: [
              OfflineBar(),
              InAppWebView(
                initialUrl: widget._selectedForm.formUrl,
                onLoadStart: (InAppWebViewController controller, String url) {
                  setState(() {
                    this.currentUrl = url;
                  });
                },
              ),
            ],
          )),
    );
  }
}

您可以复制粘贴 运行 下面的完整代码
您可以更改顺序并使用 Positioned 定位 OfflineBar
代码片段

Stack(
    children: [
      InAppWebView(
        initialUrl: widget._selectedForm.formUrl,
        onLoadStart: (InAppWebViewController controller, String url) {
          setState(() {
            this.currentUrl = url;
          });
        },
      ),
      Positioned(left: 0, right: 0, top: 20, child: OfflineBar()),
    ],
  )

工作演示

完整代码

import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

class UnityForm {
  String title;
  String formUrl;

  UnityForm({this.title, this.formUrl});
}

class UnityFormViewPage extends StatefulWidget {
  final UnityForm _selectedForm;

  UnityFormViewPage(this._selectedForm);

  @override
  _UnityFormViewPageState createState() => _UnityFormViewPageState();
}

class _UnityFormViewPageState extends State<UnityFormViewPage> {
  String currentUrl = '';

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Scaffold(
          appBar: AppBar(
            title: Text(widget._selectedForm.title),
            leading: IconButton(
                icon: Icon(Icons.arrow_back),
                onPressed: () {
                  Navigator.pop(context);
                }),
          ),
          body: Stack(
            children: [
              InAppWebView(
                initialUrl: widget._selectedForm.formUrl,
                onLoadStart: (InAppWebViewController controller, String url) {
                  setState(() {
                    this.currentUrl = url;
                  });
                },
              ),
              Positioned(left: 0, right: 0, top: 20, child: OfflineBar()),
            ],
          )),
    );
  }
}

class OfflineBar extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(color: Colors.purple, child: Text("OfflineBar"));
  }
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: UnityFormViewPage(
          UnityForm(title: "test", formUrl: "https://flutter.dev/")),
    );
  }
}