Do you know how to close a web view from different screen using bloc in flutter?
Loading
Do you know how to close a web view from different screen using bloc in flutter?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Deepak RawatRank #646 · 3.7K reputationPosted Jun 21, 2023, 5:39 AM EDT
To close a web view from a different screen using the BLoC pattern in Flutter, you can follow these steps:
// WebViewBlocState.dart abstract class WebViewBlocState {} class WebViewOpenState extends WebViewBlocState {} class WebViewClosedState extends WebViewBlocState {}// WebViewBloc.dart import 'dart:async'; class WebViewBloc { final _eventStreamController = StreamController(); final _stateStreamController = StreamController(); StreamSink get eventSink => _eventStreamController.sink; Stream get stateStream => _stateStreamController.stream; WebViewBloc() { _eventStreamController.stream.listen(_handleEvent); } void _handleEvent(WebViewBlocEvent event) { if (event is CloseWebViewEvent) { _closeWebView(); } } void _closeWebView() { _stateStreamController.sink.add(WebViewClosedState()); } void dispose() { _eventStreamController.close(); _stateStreamController.close(); } }// CloseWebViewScreen.dart import 'package:flutter/material.dart'; import 'WebViewBloc.dart'; class CloseWebViewScreen extends StatelessWidget { final WebViewBloc webViewBloc; CloseWebViewScreen({required this.webViewBloc}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Close WebView'), ), body: Center( child: ElevatedButton( child: Text('Close WebView'), onPressed: () { webViewBloc.eventSink.add(CloseWebViewEvent()); Navigator.pop(context); // Close the current screen }, ), ), ); } }// WebViewScreen.dart import 'package:flutter/material.dart'; import 'WebViewBloc.dart'; class WebViewScreen extends StatefulWidget { final WebViewBloc webViewBloc; WebViewScreen({required this.webViewBloc}); @override _WebViewScreenState createState() => _WebViewScreenState(); } class _WebViewScreenState extends State { @override void initState() { super.initState(); widget.webViewBloc.stateStream.listen(_handleStateChange); } void _handleStateChange(WebViewBlocState state) { if (state is WebViewClosedState) { Navigator.pop(context); // Close the web view screen } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Web View Screen'), ), body: Container( // Build your web view widget here ), ); } @override void dispose() { widget.webViewBloc.dispose(); super.dispose(); } }WebViewBlocand pass it to both theCloseWebViewScreenandWebViewScreen:// main.dart import 'package:flutter/material.dart'; import 'WebViewBloc.dart'; import 'CloseWebViewScreen.dart'; import 'WebViewScreen.dart'; void main() { final webViewBloc = WebViewBloc(); runApp(MyApp(webViewBloc: webViewBloc)); } class MyApp extends StatelessWidget { final WebViewBloc webViewBloc; MyApp({required this.webViewBloc}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', home: CloseWebViewScreen(webViewBloc: webViewBloc), routes: { '/webView': (context) => WebViewScreen(webViewBloc: webViewBloc), }, ); } }Now, when you navigate to the
CloseWebViewScreenand press the "Close WebView" button, it will trigger the closing event, close the web view screen, and return to the previous screen.