Can you tell me how can I go back to previous pages in my webview application?
I am using the following code which works for android but not swipe on iphone –
late WebViewController? webViewController;
Future<bool> _onWillPop(BuildContext context) async {
if (await webViewController!.canGoBack()) {
webViewController!.goBack();
return Future value(false);
} else {
return Future value(true);
}
}
and here is my complete widget code –
return WillPopScope(
child: Scaffold(
bottomNavigationBar: BottomNavigationBar(
selectedItemColor: Colors.orange,
backgroundColor: Colors.white,
type: BottomNavigationBarType.fixed,
currentIndex: selectedIndex,
unselectedLabelStyle: TextStyle(color: Color.fromARGB(153, 162, 173, 1)),
selectedLabelStyle: TextStyle(fontSize: 11.9),
items: [
BottomNavigationBarItem(
icon: Container(margin: EdgeInsets.only(bottom: 5), height: 32, width: 35, child: Image.asset('assets/images/Home.png', fit: BoxFit.cover,),),
activeIcon: Container(margin: EdgeInsets.only(bottom: 5),height: 32, width: 35, child: Image.asset('assets/images/Home_color.png', fit: BoxFit.cover,),),
label: "Главная"
),
BottomNavigationBarItem(
icon: Container(margin: EdgeInsets.only(bottom: 5),height: 32, width: 32, child: Image.asset('assets/images/Catalog.png', fit: BoxFit.cover,)),
activeIcon: Container(margin: EdgeInsets.only(bottom: 5),height: 32, width: 32, child: Image.asset('assets/images/Catalog_color.png', fit: BoxFit.cover,),),
label: "Каталог",
),
],
onTap: (i) async {
webViewController?.loadUrl(linkNavbar[i]);
setState(() => selectedIndex = i);
},
),
body: SafeArea(
child: Stack(
children: [
WebView(
key: _key,
javascriptMode: JavascriptMode.unrestricted,
initialUrl: "https://caravan-store.shop/",
onWebViewCreated: (controller) {
setState(() {
webViewController = controller;
});
},
onWebResourceError: (WebResourceError error) {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => MyError(),
),
);
},
),
]
),
)
),
onWillPop: () => _onWillPop(context)
);
Also, I added this code to my main.dart –
theme: ThemeData(
pageTransitionsTheme: PageTransitionsTheme(
builders: {
TargetPlatform.android: CupertinoPageTransitionsBuilder(),
TargetPlatform.iOS: CupertinoPageTransitionsBuilder(),
}
)
On the Internet, I could not find any solution for such a problem. What I’m doing is trying to make a swipe to the right to go to the previous page. Unfortunately, it doesn’t work on iPhone, but it works on Android.
Tell me what do I need to do?
2
Answers
To be honest, I don't know why it worked, but by adding the following code, everything worked on the iPhone.
Its work android and ios both right now