Giter VIP home page Giter VIP logo

Comments (6)

darshankawar avatar darshankawar commented on June 25, 2024

Thanks for the report @YashExpert10
Can you narrow down code sample only to minimal and filter out unnecessary implementation to make it reduced code sample to work with ?
Also, is there a way for you to check the native Android behavior if it works as expected ? This is just to narrow down if the behavior is specific to Flutter or occurs in native as well.

from flutter.

YashExpert10 avatar YashExpert10 commented on June 25, 2024

Thanks for response @darshankawar
//I am Tested this code on my real Android device, I haven't tested on iOS

//Here is minimal code

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

void main() {
  runApp(const MaterialApp(home: TabsView()));
}

class TabsView extends StatefulWidget {
  const TabsView({super.key});

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

class _TabsViewState extends State<TabsView> {
  int _currentIndex = 0;
  int tabIndex = 0;

  void onTabTapped(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        ///  Added Stack Widget
        children: [
          Offstage(
            offstage: _currentIndex != 0,
            child: const First(),
          ),
          Offstage(
            offstage: _currentIndex != 1,
            child: const Second(),
          ),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        selectedItemColor: Colors.deepOrange[800],
        backgroundColor: Colors.grey[300],
        unselectedItemColor: Colors.grey[800],
        currentIndex: _currentIndex,
        onTap: onTabTapped,
        items: const [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: 'Webview 1',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.web),
            label: 'Webview 2',
          ),
        ],
      ),
    );
  }
}

class First extends StatefulWidget {
  const First({super.key});

  @override
  State<First> createState() => _FirstState();
}

class _FirstState extends State<First> {
  late final WebViewController _controller1;

  @override
  void initState() {
    super.initState();

    _controller1 = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..loadRequest(Uri.parse('https://stackoverflow.com'));
  }

  Future<bool> _goBackCall1(BuildContext context) async {
    if (await _controller1.canGoBack()) {
      _controller1.goBack();
      return Future.value(false);
    } else {
      // SystemNavigator.pop();
      return Future.value(true);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Flutter WebView1"),
      ),
      body: PopScope(
        canPop: false,
        onPopInvoked: (bool didPop) {
          if (didPop) {
            print('didPop call: $didPop');
            return;
          }
          _goBackCall1(context);
        },
        child: SafeArea(
          child: Stack(
            children: [
              WebViewWidget(controller: _controller1),
            ],
          ),
        ),
      ),
    );
  }
}

class Second extends StatefulWidget {
  const Second({super.key});

  @override
  State<Second> createState() => _SecondState();
}

class _SecondState extends State<Second> {
  late final WebViewController _controller2;

  @override
  void initState() {
    super.initState();

    _controller2 = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..loadRequest(Uri.parse('https://google.com'));
  }

  Future<bool> _goBackCall2(BuildContext context) async {
    if (await _controller2.canGoBack()) {
      _controller2.goBack();
      return Future.value(false);
    } else {
      // SystemNavigator.pop();
      return Future.value(true);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Flutter WebView2"),
      ),      
      body: PopScope(
        canPop: false,
        onPopInvoked: (bool didPop) {
          if (didPop) {
            print('didPop call: $didPop');
            return;
          }
          _goBackCall2(context);
        },
        child: SafeArea(
          child: Stack(
            children: [
              WebViewWidget(controller: _controller2),
            ],
          ),
        ),
      ),
    );
  }
}

from flutter.

stuartmorgan avatar stuartmorgan commented on June 25, 2024

Controller should be different for different window.
[...]
Controllers and runJavaScript and other method should not be conflict for different Class.

The issue description is describing this as being a problem where calling goBack on one controller is affecting two web views, but that not what's happening. Both onPopInvoked methods are being called when back is pressed, meaning this code is calling goBack on each webview individually. webview_flutter is therefore behavior correctly here, and the issue is with the code calling it, not the plugin.

from flutter.

YashExpert10 avatar YashExpert10 commented on June 25, 2024

Controller should be different for different window.
[...]
Controllers and runJavaScript and other method should not be conflict for different Class.

The issue description is describing this as being a problem where calling goBack on one controller is affecting two web views, but that not what's happening. Both onPopInvoked methods are being called when back is pressed, meaning this code is calling goBack on each webview individually. webview_flutter is therefore behavior correctly here, and the issue is with the code calling it, not the plugin.

Thanks Sir for your Guidance @stuartmorgan

Can u please give me full example code Or Edit my example code so i understand where i am doing wrong ?

from flutter.

darshankawar avatar darshankawar commented on June 25, 2024

@YashExpert10
Would be best to raise this in support channels like StackOverflow or https://www.reddit.com/r/flutterhelp/ to get code assistance for your case. I'll close this based on Stuart's comment.

from flutter.

github-actions avatar github-actions commented on June 25, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

from flutter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.