On my flutter app I’ve a web page that hundreds a web site utilizing WebView. The Url for the WebView is being obtained by an API which is saved inside a variable referred to as pageUrl
. When a consumer clicks on the BottomNavBar
to go to the stated display screen, the WebView will reload (or reinitialize, unsure about the best terminology right here). It’s working superb.
Nevertheless, the difficulty I’m having is that, when the consumer goes to that web page, they should click on the button once more for the web page to reinitialize. I would like it to be such that each time the consumer clicks on the button for the web page, it simply reinitializes the web page. I’ve hooked up my code under.
navbar_widget.dart
class BottomNavbar extends StatelessWidget {
const BottomNavbar({tremendous.key});
@override
Widget construct(BuildContext context) {
return GetBuilder(
builder: (controller) => WillPopScope(
onWillPop: () async {
if (controller.tabIndex != 0) {
controller.updateIndex(0);
return false;
} else {
bool closeApp = await showDialog(
context: context,
builder: (context) => AlertDialog(
// Alert Dialog
],
),
);
return closeApp;
}
},
little one: Scaffold(
extendBody: true,
appBar: AppbarWidget(tabIndex: controller.tabIndex),
bottomNavigationBar: CurvedNavigationBar(
index: controller.tabIndex,
onTap: (val) {
controller.updateIndex(val);
},
gadgets: const [
Icon(Icons.home_outlined, color: Colors.white),
Icon(Icons.maps_home_work_outlined, color: Colors.white),
Icon(Icons.access_time, color: Colors.white),
],
),
physique: Padding(
padding: EdgeInsets.solely(
high: MediaQuery.of(context).dimension.peak * 0.018),
little one: SafeArea(
backside: false,
little one: IndexedStack(
index: controller.tabIndex,
kids: const [
PageOne(),
PageTwo(),
PageThree(),
],
),
),
),
),
),
);
}
}
page2_screen.dart
class PageTwo extends StatefulWidget {
const PageTwo({tremendous.key});
@override
State createState() => _PageTwoState();
}
class _PageTwoState extends State {
late WebViewController webViewController;
String pageUrl="";
@override
void initState() {
tremendous.initState();
initializeWebView();
fetchData();
}
void initializeWebView() {
webViewController = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(const Coloration(0x00000000))
..setNavigationDelegate(
NavigationDelegate(
onPageFinished: (url) => _injectJavaScript(),
),
);
}
Future fetchData() async {
// Getting URL for the webview
}
void _injectJavaScript() {
String jsCode = """
// Injecting JS
""";
webViewController.runJavaScript(jsCode);
}
@override
Widget construct(BuildContext context) {
return GetBuilder(
builder: (bottomNavbarController) {
if (bottomNavbarController.refreshMenuScreen) {
initializeWebView();
fetchData();
bottomNavbarController.refreshMenuScreen = false;
}
return Scaffold(
backgroundColor: const Coloration(0xFF000000),
physique: Padding(
padding: EdgeInsets.solely(
backside: MediaQuery.of(context).dimension.peak * 0.01),
little one: SafeArea(
little one: WebViewWidget(
controller: webViewController,
),
),
),
);
},
);
}
}