Pannam T
Pannam T

Reputation: 479

How do I use the offStage widget?

My first widget has a FlutterMap, As I navigate to another screen and come back for some reason the map will reload, so I want to hide it (maintain state), I tried using Visibility but it doesn't work and after some recommendation, I was told to use the OffStage widget but I have no clue on how to implement it. Here is the logic, The first screen has a boolean check isVisible if it's true then the map will show on the screen, As I navigate away from the main screen then the boolean is set to false therefore the visibility is hidden. Again as I come back from the second screen the visibility is re set back to true hence showing the map. If I were to do the same thing using Offstage in place of Visibility how would I do it ?

class _MainScreenState extends State<MainScreen> {
  bool isVisible= true;

Future testFunction(bool checkValue) async {
    setState(() {
      isVisible= checkValue;
    });

 @override
  Widget build(BuildContext context) {
.....

Scaffold(
        body: Container(
            //change the margin
            margin: EdgeInsets.fromLTRB(0, 0, 0, 300),
            child: 
            Visibility(
                visible: isVisible,
                maintainAnimation: true,
                maintainState: true,
                child: (FlutterMap()
            ))

 .........
GestureDetector(
        onTap: () {
           
            setState(() {
                isVisible= !isVisible;
            });
            
            Navigator.push(
                //send to search screen
                context,
                MaterialPageRoute(
                    builder: (context) => (SearchScreen(
                        testFunction: testFunction))));

The second page

class SearchScreen extends StatefulWidget {
  final Function testFunction;

  const SearchScreen({this.testFunction});
GestureDetector(
    onTap: () {
        Navigator.pop(
            //send back data
            context,
            widget.testFunction(true));

    },
    child: Icon(Icons.arrow_back)),

Upvotes: 0

Views: 1315

Answers (1)

Rohan Thacker
Rohan Thacker

Reputation: 6357

If I were to do the same thing using Offstage in place of Visibility how would I do it?

Offstage works similar to Visibility, in the sense that they both have a boolean flag that you can use to toggle the action for each widget.

So to use an Offstage in place of a Visibility you can use:

Scaffold(
  body: Container(
    margin: EdgeInsets.fromLTRB(0, 0, 0, 300),
    child: Offstage(
       offstage: isVisible,
       child: FlutterMap()
)

Upvotes: 3

Related Questions