I'm using Flutter with GetX for state management. I have a WebView inside a Stack, and I want to display a CircularProgressIndicator while the WebView is loading. However, the Obx widget does not seem to react to updates in the loading state.
Here is the relevant part of my code:
class WebViewPage extends StatefulWidget {
@override
_WebViewPageState createState() => _WebViewPageState();
}
class _WebViewPageState extends State<WebViewPage> {
late InAppWebViewController webViewController;
final WebController _controller = Get.put(WebController());
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBarWidget(),
endDrawer: Drawer(),
body: _buildMobileWebView(),
);
}
Widget _buildMobileWebView() {
return Stack(
children: [
VerticalSpacing(10),
InAppWebView(
initialUrlRequest: URLRequest(
url: WebUri(_controller.currentUrl.string)), // Initial URL
initialSettings: InAppWebViewSettings(
javaScriptEnabled: true,
useShouldOverrideUrlLoading: true,
mediaPlaybackRequiresUserGesture: false,
),
onWebViewCreated: (InAppWebViewController controller) {
webViewController = controller;
},
onLoadStart: (controller, url) {
_controller.updateLoading(true); // Start loading
_controller.updateUrl(url.toString());
// Update URL
},
onLoadStop: (controller, url) async {
_controller.updateLoading(false); // Stop loading
_controller.updateUrl(url?.toString() ?? ''); // Update URL
},
onProgressChanged: (controller, progress) {
if (progress < 100) {
_controller
.updateLoading(true); // Show loading until it's complete
} else {
_controller.updateLoading(false); // Stop loading at 100%
}
},
),
Obx(() {
return _controller.isLoading.value
? Center(child: CircularProgressIndicator())
: SizedBox.shrink();
}),
],
);
}
}
class WebController extends GetxController {
var isLoading = true.obs;
var currentUrl = 'https://mpfsts.mp.gov.in/'.obs;
void updateLoading(bool loading) {
print('Loading state updated: $loading');
isLoading.value = loading;
}
void updateUrl(String url) {
currentUrl.value = url;
print("Susheel: ${currentUrl.value}");
}
}
Problem: The Obx widget doesn't seem to react to changes in the isLoading observable when calling _controller.updateLoading(true/false) during WebView load events. The CircularProgressIndicator is not showing up as expected.
What I've Tried: Checked if the isLoading state is being updated correctly using print statements. Ensured that Get.put(WebController()) is only initialized once. Verified that Obx correctly listens to isLoading.value. Question: What am I missing here? Why is the Obx widget not reflecting the changes in the loading state when updating via updateLoading()?
Obxto see if a rebuild is triggered?