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
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:
Use webview controller to load new url in webview. As the property says its for initial url to load new url use controller.