Flutter WebView loadRequest

mhd8tkvw  于 2023-11-21  发布在  Flutter
关注(0)|答案(1)|浏览(176)

我有一个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的搜索输入,例如

4jb9z9bj

4jb9z9bj1#

每次尝试访问或修改WebView时,您都在创建一个新的WebViewController。您需要引用附加到WebView小部件的同一个WebViewController示例。
修改onSearch以使用WebViewController instance

void _onSearch() {
  String url = _searchController.text;
  
  if (!url.startsWith('http://') && !url.startsWith('https://')) {
    url = 'https://' + url;
  }
  
  controller.loadRequest(Uri.parse(url));
}

字符串

相关问题