StartActivityForResult in Flutter

The startActivityForResult function, which enables you to launch a new activity and receive a result back in the original activity, is one of the helpful tools in Android programming.

Although there isn’t a direct Flutter equivalent to this technique, we can accomplish a comparable feature by utilising callbacks and navigation in Flutter. We’ll look at how to use Flutter to construct a similar pattern in this article.

We can using callback functionality in Flutter applications.

Create one file StartActivityResultDetails.dart. Put below code of that.

class StartActivityResultDetails extends StatefulWidget {
  @override
  StartActivityDisplayState createState() => StartActivityDisplayState();
}

class StartActivityDisplayState extends State<StartActivityResultDetails> {
  Map str_Callback = {"Title": "Welcome India"};

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('First Page'),
          automaticallyImplyLeading: false,
        ),
        body: Center(
            child: Column(
          children: [
            Container(
              padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
              margin: EdgeInsets.fromLTRB(5, 5, 5, 0),
              child: TextButton(
                style: TextButton.styleFrom(
                  shadowColor: Colors.black,
                  textStyle: TextStyle(fontSize: 20),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10.0),
                  ),
                  backgroundColor: Colors.black45,
                ),
                onPressed: () async {
                  Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) => CallBackResultDetails()))
                      .then((detailsCallback) {
                    setState(() {
                      str_Callback = detailsCallback;
                    });
                  });
                },
                child: Text(
                  "Click Here For Next Screen",
                  style: TextStyle(color: Colors.white, fontSize: 12),
                ),
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
              margin: EdgeInsets.fromLTRB(5, 5, 5, 0),
              child: Text(
                "Result of Next Screen Display Here -- " +
                    str_Callback['Title'],
                style: TextStyle(color: Colors.black, fontSize: 12),
              ),
            ),
          ],
        )));
  }
}

Now Create second part of that code.

Create CallBackResultDetails.dart File for callback function.

class CallBackResultDetails extends StatefulWidget {
  @override
  CallBackDisplayState createState() => CallBackDisplayState();
}

class CallBackDisplayState extends State<CallBackResultDetails> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('CallBack Functions'),
          automaticallyImplyLeading: false,
        ),
        body: Center(
            child: Column(
          children: [
            Container(
              padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
              margin: EdgeInsets.fromLTRB(5, 5, 5, 0),
              child: TextButton(
                style: TextButton.styleFrom(
                  shadowColor: Colors.black,
                  textStyle: TextStyle(fontSize: 20),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10.0),
                  ),
                  backgroundColor: Colors.black45,
                ),
                onPressed: () async {
                  Navigator.of(context).pop({"Title": "Great Nice from you."}); // When you want to send JSON Then this will be help
                  // Navigator.of(context).pop(true); // When you want to send Boolean Then this will be help
                },
                child: Text(
                  "Click Here For Next Screen",
                  style: TextStyle(color: Colors.white, fontSize: 12),
                ),
              ),
            ),
          ],
        )));
  }
}

If you want to send JSON part then this will be help

Navigator.of(context).pop({"Title": "Great Nice from you."}); // When you want to send JSON Then this will be help

If you want to send boolean value then this will be help.

Navigator.of(context).pop(true); // When you want to send Boolean Then this will be help

Despite the lack of a direct equivalent to startActivityForResult in Flutter, we can nevertheless achieve a similar level of functionality by combining callbacks and navigation.

By following the instructions in this article, you may use Flutter to move to a different screen and have the result returned to the original widget.

Your Flutter applications can communicate between screens thanks to this pattern’s increased flexibility.

With this method, you can easily manage the results and construct your own startActivityForResult in Flutter. Coding is fun!

Related Posts

Custom Tabbar in Flutter

Bottom Tab Bar in Flutter

Tabbar with image in flutter

Simple Tabbar in Flutter

Progress Bars Example in Flutter

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply