skip to Main Content

so i have been trying to get my app to work. i wanted to create an app for one of my websites using flutter and so far so good. the only problem is when i click on the button labeled as "status" the WebView does not change to the page i want it to go to.

my code:

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

void main() {
  runApp(const MyApp());
}

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

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

class _MyAppState extends State<MyApp> {
  int _selectedIndex = 0;
  final List<String> _urls = [
    "https://minecraft.xandersalarie.com/",
    "https://minecraft.xandersalarie.com/status",
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: SafeArea(
          child: WebView(
            initialUrl: _urls[_selectedIndex],
            javascriptMode: JavascriptMode.unrestricted,
          ),
        ),
        bottomNavigationBar: BottomNavigationBar(
          items: const <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Icon(Icons.home),
              label: 'Home',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.stacked_bar_chart_outlined),
              label: 'Status',
            ),
          ],
          currentIndex: _selectedIndex,
          selectedItemColor: Colors.amber[800],
          onTap: _onItemTapped,
        ),
      ),
    );
  }
}

i tried what i’ve read so far on the internet. i have read that so far you cannot use initialUrl twice so i made sure to use final List<String>_urls as this was the only option i found. what i was expecting to happen is that,when it is on "home". it starts at the login page and when i tap "status" it goes to the other url within the list. this does not happen for me. what i would also like is if you tap home it would refresh the WebView but it does not do this either

2

Answers


  1. You can use the webview controller and webviewwidget to have a controlled navigation to pages.

    You can use the official flutter.dev webview package: https://pub.dev/packages/webview_flutter

    Something like this:

    class MyApp extends StatefulWidget {
      const MyApp({super.key});
    
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      int _selectedIndex = 0;
      final List<String> _urls = [
        "https://minecraft.xandersalarie.com/",
        "https://minecraft.xandersalarie.com/status",
      ];
    
      late final WebViewController controller;
    
      @override
      void initState() {
        controller = WebViewController()
          ..setJavaScriptMode(JavaScriptMode.unrestricted)
          ..setNavigationDelegate(
            NavigationDelegate(
              onNavigationRequest: (request) async {
                if (_urls.contains(request.url)) {
                  return NavigationDecision.navigate;
                }
    
                return NavigationDecision.prevent;
              },
            ),
          )
          ..loadRequest(Uri.parse(_urls[_selectedIndex])).onError((error, stackTrace) => log(
            'error',
            error: error,
            stackTrace: stackTrace,
          ));
        super.initState();
      }
    
      void _onItemTapped(int index) {
        setState(() {
          _selectedIndex = index;
        });
        // This will load new url to the webview
        controller.loadRequest(Uri.parse(_urls[_selectedIndex]));
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            body: SafeArea(
              child: WebViewWidget(
                controller: controller,
              ),
            ),
            bottomNavigationBar: BottomNavigationBar(
              items: const <BottomNavigationBarItem>[
                BottomNavigationBarItem(
                  icon: Icon(Icons.home),
                  label: 'Home',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.stacked_bar_chart_outlined),
                  label: 'Status',
                ),
              ],
              currentIndex: _selectedIndex,
              selectedItemColor: Colors.amber[800],
              onTap: _onItemTapped,
            ),
          ),
        );
      }
    }
    
    Login or Signup to reply.
  2. Use webview controller to load new url in webview. As the property says its for initial url to load new url use controller.

    import 'package:flutter/material.dart';
    import 'package:webview_flutter/webview_flutter.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatefulWidget {
      const MyApp({Key? key}) : super(key: key);
    
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      int _selectedIndex = 0;
      final List<String> _urls = [
        "https://minecraft.xandersalarie.com/",
        "https://minecraft.xandersalarie.com/status",
      ];
      late WebViewController _controller;
    
      @override
      void initState() {
        super.initState();
      }
    
      void _onItemTapped(int index) {
        setState(() {
          _selectedIndex = index;
        });
        _controller.loadUrl(_urls[index]);
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            body: SafeArea(
              child: WebView(
                initialUrl: _urls[_selectedIndex],
                javascriptMode: JavascriptMode.unrestricted,
                onWebViewCreated: (WebViewController webViewController) {
                  _controller = webViewController;
                },
              ),
            ),
            bottomNavigationBar: BottomNavigationBar(
              items: const <BottomNavigationBarItem>[
                BottomNavigationBarItem(
                  icon: Icon(Icons.home),
                  label: 'Home',
                ),
                BottomNavigationBarItem(
                  icon: Icon(Icons.stacked_bar_chart_outlined),
                  label: 'Status',
                ),
              ],
              currentIndex: _selectedIndex,
              selectedItemColor: Colors.amber[800],
              onTap: _onItemTapped,
            ),
          ),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search