flutter calling bottom sheet and popup from bottom sheet and refresh first screen

flutter calling bottom sheet and popup from bottom sheet and refresh first screen

Problem Description:

I used the bottom sheet and I’m using navigator.pop on the button inside the bottom sheet but want to refresh the first screen when calling popup…

code

showModalBottomSheet(
    context: context,
    builder: (BuildContext bc) {
      return Container(
        child: Wrap(
          children: <Widget>[
          
          ListTile(
              leading: Icon(Icons.delete),
              title: Text('delete'),
              onTap: () async {
                

                try {
                  final file = await File(path);
                  print(path);
                  await file.delete();


                  print(file);
                } catch (e) {
                  print(e.toString());

                }
                Navigator.pop(context);
                setState(() {
                  print('delete');
                });

Solution – 1

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String homeScreenText = "Bottom Sheet not opened";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(homeScreenText),
            const SizedBox(height: 50),
            ElevatedButton(
              onPressed: () {
                showBS();
              },
              child: const Text("open bottom sheet"),
            ),
          ],
        ),
      ),
    );
  }

  showBS() {
    showModalBottomSheet(
      context: context,
      builder: (context) {
        return SizedBox(
          height: 300,
          width: double.infinity,
          child: Center(
            child: ElevatedButton(
                onPressed: () {
                // Delete item from the list
                  // then call these
                  Navigator.pop(context);
                  setState(() {});
                },
                child: const Text("Close")),
          ),
        );
      },
    );
  }
}

Solution – 2

You just need to pass class name beside the Navigator.

Like this:-

Navigator.pop(context,classname());

Solution – 3

This perfectly work’s for me and hoping that also work’s for you.`

Note:- If you note get data then call that method many times.

If You try to get data from any method then call that method after `

Navigator.pop(context)
method name();
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject