newbiras
newbiras

Reputation: 25

How to fix showDialog closing screen instead an Dialog window?

I have a problem, when I try to load data from api...

I have cubit when I call emit(Waiting) => this one is to show me showDialog with black full screen and loading indicator in the center, and when api response is 200 then emit(Success) that close that showDialog() => Navigator.of(context, rootNavigator: true).pop();

And this is the problem, when I login to app and go to main widget, then I click on the tab(TabBar) inside of the main widget and call BlocProvider.of(context).callApi() inside initState() then when response is 200 I return to login page, show dialog disappears and my main widget too. How to fix that?

This is main widget code:

class _MainWidgetState extends State<MainWidget> with WindowListener {
  onTap: (index) { // This problem happens too if I add this, I return to login screen when go to HomeWidget
       if(index == 0) {
         BlocProvider.of<StatisticsCubit>(context).callApi();
       }
  }
  return TabBar(
    tabs: [
      Tab(
        child: "Home",
      ),
      Tab(
        child: "Test",
      ),
      Tab(
        child: "Statistics",
      )
    ]
  )
}

And when I press Home tab I go to HomeWidget, this is the home widget:

class _HomeWidgetState extends State<HomeWidget> with WindowListener {
  @override
  void initState() {

    BlocProvider.of<StatisticsCubit>(context).callApi();

    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    return BlocConsumer<StatisticsCubit, StatisticsState>(
      listener: (context,state) {
        if (state is WaitingNfb) {
          showDialog(
            context: context,
            builder: (context) => Container(
              width: MediaQuery.of(context).size.width,
              height: MediaQuery.of(context).size.height,
              color: Colors.black.withOpacity(0.7),
              child: const Center(
                child: CircularProgressIndicator(
                  strokeWidth: 1,
                  color: Colors.black,
                  backgroundColor: Colors.white,
                ),
              ),
            )
          );
        }
        if (state is Success) {
          Navigator.of(context, rootNavigator: true).pop(); // And here I return to login screen, not showing content of HomeWidget
        }
      },
      builder: (context, state) {
        return Container(
          child: ,
        )
      }),
    )
  }
}

How to close only showDialog, not all screen?..

Upvotes: 0

Views: 202

Answers (1)

cyberail
cyberail

Reputation: 1284

Try making listener as async and use await in front of the showDialog.

listener: () async {
   if (someState) {
      await showDialog()
   }
}

Upvotes: 0

Related Questions