0

Suppose I have two Widget A and B. A listens for event changes. And Event type is Connectivity status.

A --> One Widget (separate screen)
B --> Another Widget (separate screen)

Event Type
 |
 --> INTERNET_CONNECTED 
 --> INTERNET_NOT_CONNECTED

I want to push and pop B widget depending on the event triggered inside A. If event is INTERNET_NOT_CONNECTED then push B and if INTERNET_CONNECTED then pop B. Can someone suggest me how can i achieve it. Thanks...

Purushotam Kumar
  • 1,002
  • 2
  • 13
  • 23
  • How about using the Visibility widget to show/hide B depending on the status? – uan Aug 28 '20 at 09:20
  • Widget B is a separate screen, it is not a part of Screen A. How can i use visibility in this scenario – Purushotam Kumar Aug 28 '20 at 09:22
  • So you want to transition from one screen to another based on some event? How about pushing a new route? – uan Aug 28 '20 at 09:26
  • yes, transition. Pushing a route is working fine. I am able to push it. But when Screen B comes on the top of A, i am not able to pop B when event changes. – Purushotam Kumar Aug 28 '20 at 09:28
  • Are you using a provider for state management in Screen A? (If you are using a provider, have B listen to the state, and pop as needed.) Just a suggestion. – uan Aug 28 '20 at 09:33
  • i am using flutter bloc for state management. So you are saying i need to listen for the events inside B as well. and if event changes pop it. rit? – Purushotam Kumar Aug 28 '20 at 09:41
  • That is one answer. There might be others as well. Let us know when you find out. – uan Aug 28 '20 at 09:47
  • Actually, i have tried this step, but sometime it was showing some weird behavior. B was listening for events, and what was happening it was creating its own copy when internet_not_connected was getting called. – Purushotam Kumar Aug 28 '20 at 09:54
  • Would it be possible for both the screens to listen to the "connected state" and when that changes, show which screen you need? – uan Aug 28 '20 at 10:14

2 Answers2

0

You can use the Visibility() widget inside a Stack() to show widgets depending on variables.

Stack(
  children: [
    A(),
    Visibility(
      visible: _connected, //assuming _connected is a bool that returns true if INTERNET_NOT_CONNECTED
      child: B()
    ),
  ],
),
MindStudio
  • 706
  • 1
  • 4
  • 13
  • In this scenario. both A and B is going to be a part of one screen. Here i am stating, I want to push and pop screen B depending upon the event in screen A. – Purushotam Kumar Aug 28 '20 at 09:26
  • If B() is visible and a full screen widget, it would cover up A(). I made a whole app using this method and it worked like switching between sites. – MindStudio Aug 28 '20 at 09:33
  • yes i understand your approach, but i wanted to know if navigation is possible or not. – Purushotam Kumar Aug 28 '20 at 09:45
  • navigation via...? You can always have buttons or any form of Interface element on top that can manipulate the value that is used to decide wether B() is visible or not. For example: You could put a button in B() "return to A()" that sets the value used in the "visible" argument to false. But make sure to use setState((){}); to update your state. – MindStudio Aug 28 '20 at 09:53
0

Do it like this,

Make A StateFulWidget.


import 'package:connectivity/connectivity.dart';//https://pub.dev/packages/connectivity

@override
initState() {
  super.initState();
  getcon();
}

getcon() async{

var connectivityResult = await (Connectivity().checkConnectivity()).then((value) {
if (value== ConnectivityResult.mobile||value== ConnectivityResult.wifi) {
  Navigator.pop(context)//popping the screen A
Navigator.push()//write code here to push to screen B


}
                  ;

}
Madhavam Shahi
  • 1,166
  • 6
  • 17