flutter - 我在颤振上使用 webview。页面有收藏链接功能。我如何在个人资料页面中调用和列出收藏的链接?
问题描述
我在这个插件上使用 webview 和最喜欢的功能。它工作正常,但我怎样才能在个人资料页面>收藏夹上显示收藏链接?
import 'dart:async';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class NewsScreen extends StatefulWidget {
@override
_NewsScreenState createState() => _NewsScreenState();
}
class _NewsScreenState extends State<NewsScreen> {
final Completer<WebViewController> _controller =
Completer<WebViewController>();
@override
void initState() {
super.initState();
if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(60.0),
child: AppBar(
backgroundColor: Colors.white,
automaticallyImplyLeading: true,
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Haberler',
style: TextStyle(color: Colors.red, fontSize: 25.0),
)
],
),
// This drop down menu demonstrates that Flutter widgets can be shown over the web view.
actions: <Widget>[
NavigationControls(
_controller.future,
),
],
),
),
body: Builder(builder: (BuildContext context) {
return WebView(
initialUrl: 'https://www.google.com',
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
},
onProgress: (int progress) {
print("WebView is loading (progress : $progress%)");
},
javascriptChannels: <JavascriptChannel>{
_toasterJavascriptChannel(context),
},
navigationDelegate: (NavigationRequest request) {
if (request.url.startsWith('https://www.youtube.com/')) {
print('blocking navigation to $request}');
return NavigationDecision.prevent;
}
print('allowing navigation to $request');
return NavigationDecision.navigate;
},
onPageStarted: (String url) {
print('Page started loading: $url');
},
onPageFinished: (String url) {
print('Page finished loading: $url');
},
gestureNavigationEnabled: true,
);
}),
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
floatingActionButton: favoriteButton(),
);
}
JavascriptChannel _toasterJavascriptChannel(BuildContext context) {
return JavascriptChannel(
name: 'Toaster',
onMessageReceived: (JavascriptMessage message) {
// ignore: deprecated_member_use
Scaffold.of(context).showSnackBar(
SnackBar(content: Text(message.message)),
);
});
}
Widget favoriteButton() {
return FutureBuilder<WebViewController>(
future: _controller.future,
builder: (BuildContext context,
AsyncSnapshot<WebViewController> controller) {
if (controller.hasData) {
return FloatingActionButton(
onPressed: () async {
final String url = (await controller.data!.currentUrl())!;
// ignore: deprecated_member_use
Scaffold.of(context).showSnackBar(
SnackBar(content: Text('Favorited $url')),
);
},
child: const Icon(
Icons.favorite,
color: Colors.white,
),
);
}
return Container();
});
}
}
class NavigationControls extends StatelessWidget {
const NavigationControls(this._webViewControllerFuture)
: assert(_webViewControllerFuture != null);
final Future<WebViewController> _webViewControllerFuture;
@override
Widget build(BuildContext context) {
return FutureBuilder<WebViewController>(
future: _webViewControllerFuture,
builder:
(BuildContext context, AsyncSnapshot<WebViewController> snapshot) {
final bool webViewReady =
snapshot.connectionState == ConnectionState.done;
final WebViewController controller = snapshot.data!;
return Row(
children: <Widget>[
IconButton(
icon: const Icon(
Icons.arrow_back_ios,
color: Colors.red,
),
onPressed: !webViewReady
? null
: () async {
if (await controller.canGoBack()) {
await controller.goBack();
} else {
// ignore: deprecated_member_use
Scaffold.of(context).showSnackBar(
const SnackBar(content: Text("Geçmiş Bulunamadı")),
);
return;
}
},
),
IconButton(
icon: const Icon(
Icons.arrow_forward_ios,
color: Colors.red,
),
onPressed: !webViewReady
? null
: () async {
if (await controller.canGoForward()) {
await controller.goForward();
} else {
// ignore: deprecated_member_use
Scaffold.of(context).showSnackBar(
const SnackBar(content: Text("Geçmiş Bulunamadı")),
);
return;
}
},
),
],
);
},
);
}
}
解决方案
推荐阅读
- javascript - 仅适用于三个元素中的两个
- c++ - 如何定义模板类模板友元函数,其参数之一是模板类主体之外的依赖名称?
- .net-core - 添加 NuGet 源时,Gitlab 管道遇到错误
- bash - 自动响应 npm/npx 命令行安装中的提示
- python - tkinter 居中两个或多个小部件
- javascript - 向下滚动网站后显示 div
- php - 如何在 bash / php 脚本中执行命令?
- angular - How to create a mat-tree from firebase data in angular
- python - 如何在 Windows 10 中使用 python3.9 检查文件是否在我的 python 脚本之外的另一个进程中主动打开?
- php - 强制显示购物车