0

Всем привет, я только начал изучать webview flutter и хочу добавить кнопку на конкретной страничкe в webview. Каким образом можно это реализовать?

Например у меня есть код, webview отображает youtube:

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Welcome to Flutter'),
        ),
        body: const WebView(
          initialUrl: 'https://www.youtube.com/',
          javascriptMode: JavascriptMode.unrestricted,
        ),
      )
    );
  }
}

Как можно реализовать кнопку только на странице https://www.youtube.com/feed/library. Что бы кнопка отображалась только на данной странице, а на других её не было. Помогите реализовать данную кнопку!

1 ответ 1

0

Как то так дальше думаю разберешься. В body поменяй на WebViewWidget(url: 'https://www.youtube.com/')

class WebViewWidget extends StatefulWidget {
  final String url;
  const WebViewWidget({Key? key, required this.url}) : super(key: key);

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

class _WebViewWidgetState extends State<WebViewWidget> {
  bool showButton = false;
  bool isLoading = true;
  late WebViewController _webView;

  final Completer<WebViewController> _controllerCompleter =
      Completer<WebViewController>();
  //Make sure this function return Future<bool> otherwise you will get an error
  Future<bool> _onWillPop(BuildContext context) async {
    if (await _webView.canGoBack()) {
      _webView.goBack();
      return Future.value(false);
    } else {
      return Future.value(true);
    }
  }

  @override
  void initState() {
    super.initState();
    if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        constraints: BoxConstraints(maxWidth: UIUtil.newsCardWidth(context)),
        child: WillPopScope(
          onWillPop: () => _onWillPop(context),
          child: WebView(
            initialUrl: widget.url',
            gestureNavigationEnabled: true,
            javascriptMode: JavascriptMode.unrestricted,
            onWebViewCreated: (WebViewController webViewController) {
              _controllerCompleter.future.then((value) => _webView = value);
              _controllerCompleter.complete(webViewController);
            },
            onPageFinished: (String s) {
              setState(() {
                isLoading = false;
              });
            },
            navigationDelegate: (NavigationRequest request) {
              if (request.url.startsWith('https://www.youtube.com/feed/library')) {
                setState(() {
                  showButton = true;
                });
              }
              return NavigationDecision.navigate;
            },
          ),
        ),
      ),
    );
  }
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.