8

i need to show desktop version of website in my app. for me it shows the mobile version of app:

code:

import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import './landing_page.dart';

class ComicsPage extends StatefulWidget {

@override
_ComicsPageState createState() => _ComicsPageState();

 }
class _ComicsPageState extends State<ComicsPage> {
TextEditingController controller = TextEditingController();
FlutterWebviewPlugin flutterWebviewPlugin = FlutterWebviewPlugin();
var urlString = "https://avengers.marvelhq.com/comics";

 launchUrl() {
 setState(() {
  urlString = controller.text;
  flutterWebviewPlugin.reloadUrl(urlString);
 });
}

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

flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged wvs) {
  print(wvs.type);
 });
 }

@override
Widget build(BuildContext context) {
 String newUA= "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 
 Firefox/40.1";
 return WebviewScaffold(
  appBar: AppBar(
    actions: <Widget>[
      IconButton(
        icon: Icon(Icons.cancel,size: 45.0),
        onPressed: () => Navigator.of(context).pushAndRemoveUntil(new 
    MaterialPageRoute(builder: (BuildContext context) => new LandingPage()), 
   (Route route) => route == null),
      )
    ],
    title: const Text('Marvel Comics'),
  ),
  url: urlString,
  withZoom: true,
  withJavascript: true,
  withLocalStorage: true,
  scrollBar: true,
  enableAppScheme: true,

  userAgent: newUA,
  clearCookies: false,
  clearCache: false,

   );
 }
}

i need to view like this sample image

especially for this site: click here

i tried to change useragent to desktop version(Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1). it not works.. give me solution.

PrakashKing
  • 635
  • 2
  • 11
  • 24

3 Answers3

2

i know this late answer but maybe someone will need it

BTW i also face this problem before in flutter

after do this trick it was work for me

just try eval this code JavaScript after your page is Loaded

like this

//to load desktop mode
  String js =
      "document.querySelector('meta[name=\"viewport\"]').setAttribute('content', 'width=1024px, initial-scale=' + (document.documentElement.clientWidth / 1024));";

  @override
  Widget build(BuildContext context) {
    final flutterWebViewPlugin = new FlutterWebviewPlugin();

    flutterWebViewPlugin.onProgressChanged.listen((event) {
      debugPrint("Progress $event");

        //this will make show in desktop mode 
        flutterWebViewPlugin.evalJavascript(js);

    });

    return WebviewScaffold(
      appBar: AppBar(
        title: Text("Desktop Mode"),
      ),
      url: "My Url",
      withJavascript: true,
      useWideViewPort: true,
      displayZoomControls: false,
      scrollBar: true,
      withZoom: true,
    );
  }  

Link Of WebView Plugin here

Mahmoud Abu Alheja
  • 3,343
  • 2
  • 24
  • 41
1

If you need to load a web page in desktop mode from a Flutter Mobile app, you can use the "flutter_inappwebview" plugin. This plugin allows you to pass a "userAgent" to the server, which can be used to set the user agent string of the web browser.

To load the page in desktop mode, you can set the user agent to a desktop browser's user agent string. This can be achieved using the "UserAgent" class provided by the "flutter_inappwebview" plugin.

Here's an example code snippet:

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

/// desktop User Agent
const desktopUserAgent =
    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36";

/// Web link
const webWhatsappUrl = "https://web.whatsapp.com//en/";

class DesktopWeb extends StatefulWidget {
   const DesktopWeb({Key? key}) : super(key: key);

   @override
  State<DesktopWeb> createState() => _DesktopWebState();
}

class _DesktopWebState extends State<DesktopWeb> {
  InAppWebViewController? _webViewController;

 @override
 Widget build(BuildContext context) {

 final settings = InAppWebViewGroupOptions(
    crossPlatform: InAppWebViewOptions(
  userAgent: desktopUserAgent,/// pass your user agent
  allowFileAccessFromFileURLs: true,
  allowUniversalAccessFromFileURLs: true,
  useOnDownloadStart: true,
  mediaPlaybackRequiresUserGesture: true,
));

final contextMenu = ContextMenu(
  options: ContextMenuOptions(hideDefaultSystemContextMenuItems: false),
);

return Scaffold(
  body: SafeArea(

    child: InAppWebView(
      initialUrlRequest: URLRequest(url: Uri.parse(webWhatsappUrl)),
      initialOptions: settings,
      contextMenu: contextMenu,
      onWebViewCreated: (InAppWebViewController controller) {
        _webViewController = controller;
      },
   
    ),
   ),
  );
 }
} 
-2

Change the user agent parameter from null to the value mentioned below

WebView(
              userAgent: "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/20100101 Firefox/4.0",
              initialUrl: html,
              javascriptMode: JavascriptMode.unrestricted,
              onWebViewCreated: (WebViewController webViewController) {
                _controller.complete(webViewController);
              },