我有一个webView页面的代码,我想创建一个代码,其中我在appBar的输入中输入一些东西,当我单击搜索图标时,它会重定向到该URL,但由于某种原因,loadRequest方法不起作用,它没有显示错误,这是我的代码
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import '../components/bottom_navbar.dart';
class WebViewContainer extends StatelessWidget {
final TextEditingController _searchController = TextEditingController();
get controller => WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(const Color(0x00000000))
..setNavigationDelegate(
NavigationDelegate(
onProgress: (int progress) {
// Update loading bar.
},
onPageStarted: (String url) {},
onPageFinished: (String url) {},
onWebResourceError: (WebResourceError error) {},
onNavigationRequest: (NavigationRequest request) {
return NavigationDecision.navigate;
},
),
)
..loadRequest(Uri.parse('https://www.google.com'));
onSearch() {
// Get the text from the search input.
String url = _searchController.text;
// Check if the URL has a scheme, and if not, prepend "https://".
if (!url.startsWith('http://') && !url.startsWith('https://')) {
url = 'https://' + url;
}
WebViewController().loadRequest(Uri.parse(url));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: const Color(0xFF0f3443),
actions: [
PopupMenuButton(
itemBuilder: (context) {
return [
const PopupMenuItem(child: Text("Settings")),
const PopupMenuItem(child: Text("profile")),
const PopupMenuItem(child: Text('Log Out')),
];
},
)
],
automaticallyImplyLeading: false,
// The search area here
title: Container(
width: double.infinity,
height: 40,
decoration: BoxDecoration(
color: Colors.white, borderRadius: BorderRadius.circular(5)),
child: Center(
child: TextField(
controller: _searchController,
decoration: InputDecoration(
prefixIcon: IconButton(
icon: const Icon(Icons.search),
onPressed: onSearch,
),
suffixIcon: IconButton(
icon: const Icon(Icons.clear),
onPressed: () {
/* Clear the search field */
},
),
hintText: 'Search...',
border: InputBorder.none,
contentPadding: const EdgeInsets.symmetric(vertical: 13.5)),
),
),
)),
body: WebViewWidget(controller: controller),
bottomNavigationBar: const BottomNavigationBarExample(),
);
}
}
字符串
我期待着我的webview带我到谷歌,如果我键入谷歌i的搜索输入,例如
1条答案
按热度按时间4jb9z9bj1#
每次尝试访问或修改WebView时,您都在创建一个新的WebViewController。您需要引用附加到WebView小部件的同一个WebViewController示例。
修改
onSearch
以使用WebViewController instance
字符串