I have tried to use the willpopscope functionality of flutter to enable back button functionality. However after I have inputted any text in the webview, it does not work until I tap somewhere else on the mobile screen. Here is the code,
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:keyboard_visibility/keyboard_visibility.dart';
import 'package:rxdart/rxdart.dart';
void main() {
runApp(MyApp());}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: WebViewWebPage(),
);
}
}
class WebViewWebPage extends StatefulWidget {
@override
_WebViewWebPageState createState() => _WebViewWebPageState();
}
class _WebViewWebPageState extends State<WebViewWebPage> {
Future<bool> _onBack() async {
bool goBack;
print('AREARE');
var value = await webView.canGoBack(); // check webview can go back
if (value) {
webView.goBack(); // perform webview back operation
return false;
} else {
await showDialog(
context: context,
builder: (context) => new AlertDialog(
title: new Text('Confirmation ', style: TextStyle(color: Colors.purple)),
// Are you sure?
content: new Text('Do you want exit app ? '),
// Do you want to go back?
actions: <Widget>[
new FlatButton(
onPressed: () {
Navigator.of(context).pop(false);
setState(() {
goBack = false;
});
},
child: new Text('No'), // No
),
new FlatButton(
onPressed: () {
Navigator.of(context).pop();
setState(() {
goBack = true;
});
},
child: new Text('Yes'), // Yes
),
],
),
);
if (goBack) Navigator.pop(context); // If user press Yes pop the page
return goBack;
}
}
// URL to load
var URL = "https://www.google.com";
// Webview progress
double progress = 0;
InAppWebViewController webView;
int counter=0;
@override
void initState() {
super.initState();
}
void yo(){
setState(() {
counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.transparent,
appBar: AppBar(
title: Text("JAM App"),
),
body: Container(
child: Column(
children: <Widget>[
(progress != 1.0)
? LinearProgressIndicator(
value: progress,
backgroundColor: Colors.grey[200],
valueColor: AlwaysStoppedAnimation<Color>(Colors.lightBlue[900]))
: null, // Should be removed while showing
Expanded(
child: Container(
child: WillPopScope(
onWillPop: _onBack,
child: InAppWebView(
initialUrl: URL,
initialHeaders: {},
//initialOptions: {},
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
print('
WOOOOOOOOO
');
final _blurNode = FocusNode();
final FocusNode roomIdInputFocusNode = FocusNode();
final roomIdHasFocus = BehaviorSubject<bool>();
roomIdHasFocus.sink.add(false);
roomIdInputFocusNode.addListener(() {
roomIdHasFocus.sink.add(roomIdInputFocusNode.hasFocus);
});
KeyboardVisibilityNotification().addNewListener(
onChange: (bool visible) {
if (!visible) {
// roomIdHasFocus.sink.add(false);
//FocusScope.of(context).unfocus();
//FocusScopeNode currentFocus = FocusScope.of(context);
/*while (currentFocus.canRequestFocus) {
FocusScope.of(context).requestFocus(new FocusNode());
}*/
//currentFocus.dispose();
print('Salame');
yo();
/*bool myInterceptor(bool stopDefaultButtonEvent, RouteInfo info) {
_onBack(); // Do some stuff.
print('yyy');
return true;
}*/
//BackButtonInterceptor.add(myInterceptor);
// BuildContext context;
// final renderObj = context.findRenderObject();
// if (renderObj is RenderBox) {
// final hitTestResult = HitTestResult();
// if (renderObj.hitTest(hitTestResult, position: Offset(0,0))) {
// // a descendant of `renderObj` got tapped
// print('yikes');
// print(hitTestResult.path);
// }
// }
} else if (visible && roomIdInputFocusNode.hasFocus) {
roomIdHasFocus.sink.add(true);
print('Namaste');
}
},
);
},
/*onLoadStart: (InAppWebViewController controller, String url) {
// Listen Url change
/*if(URL == LISTENINGURL){
Navigator.of(context, rootNavigator: true)
.push(MaterialPageRoute(
builder: (context) => new SecondPage()));
}*/
},*/
onProgressChanged:
(InAppWebViewController controller, int progress) {
setState(() {
this.progress = progress / 100;
});
},
),
),
),
)
].where((Object o) => o != null).toList())),
); //Remove null widgets
}
}
I have tried to intercept the event of Keyboard popup and unfocus the text field after the keyboard is closed, but that also didn't restore the back button.
EDIT: For some reason when I remove scaffold it works as I require it to. However, I really want to include scaffold otherwise I can't use any of the features of material app.
question from:
https://stackoverflow.com/questions/65517075/willpopscope-not-working-after-adding-input-in-webview