can't getting stacked list of widgets after pushing them

i created simple app to push and pop random widgets which i listed on variable:

List<Widget> _widgets = [TextWidget(), ImageWidget(), ButtonWidget()];

in screen i have to button as

  • 1) attach random widget
  • 2) previous widget

after click on attach random widget i store that in

final List<int> _widgetStacks = [0];

app work fine with this functions to store them

void attachWidget(int index) {
  print(_widgetStacks[index]);
  _widgetStacks.add(index);
  setState(() {
    _currentWidget = index;
  });
}

now i want to go back in _widgetStacks with clicking on previous widget by this functions:

getPreviousWidget() {
  if (_widgetStacks.length - 1 > 0) {
    attachWidget(_widgetStacks[_widgetStacks.length - 1]);
  } else {
    _widgetStacks.removeAt(_widgetStacks.length - 1);
    Navigator.pop(context);
  }
}

void getPrevWidget(int index) {
  setState(() {
    _currentWidget = index;
  });
}

this functions by multiple click on previous widget don't work

my implementation code:

void main() {
  runApp(MaterialApp(
    title: 'AndroidMonks',
    home: Scaffold(
      appBar: AppBar(
        title: Text('Androidmonks'),
        backgroundColor: Colors.orangeAccent,
      ),
      body: Home(),
    ),
  ));
}

class Home extends StatefulWidget {
  Home({
    Key key,
  }) : super(key: key);

  @override
  State<Home> createState() => _Home();
}

class _Home extends State<Home> {
  String title = "Title";
  int _currentWidget = 0;
  final List<int> _widgetStacks = [0];

  @override
  Widget build(BuildContext context) {
    attachWidget(_currentWidget);
    List<Widget> _widgets = [TextWidget(), ImageWidget(), ButtonWidget()];

    //TextWidget
    return WillPopScope(
      onWillPop: () {
        getPreviousWidget();
      },
      child: Scaffold(
        body: Column(
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                RaisedButton(
                  child: Text('Attach RANDOM widget'.toUpperCase()),
                  onPressed: () {
                    var rnd = Random();
                    _currentWidget = rnd.nextInt(3);
                    attachWidget(_currentWidget);
                  },
                ),
                RaisedButton(
                  child: Text('PREVIOUS widget'.toUpperCase()),
                  onPressed: () {
                    getPreviousWidget();
                  },
                ),
              ],
            ),
            Expanded(
              child: _widgets[_currentWidget],
            ),
          ],
        ),
      ),
    );
  }

  void attachWidget(int index) {
    print(_widgetStacks[index]);
    _widgetStacks.add(index);
    setState(() {
      _currentWidget = index;
    });
  }

  void getPrevWidget(int index) {
    setState(() {
      _currentWidget = index;
    });
  }

  getPreviousWidget() {
    if (_widgetStacks.length - 1 > 0) {
      attachWidget(_widgetStacks[_widgetStacks.length - 1]);
    } else {
      _widgetStacks.removeAt(_widgetStacks.length - 1);
      Navigator.pop(context);
    }
  }
}

class ImageWidget extends StatefulWidget {
  @override
  State<ImageWidget> createState() => _ImageWidget();
}

class _ImageWidget extends State<ImageWidget> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text("ImageWidget"),
    );
  }
}

class TextWidget extends StatefulWidget {
  @override
  State<TextWidget> createState() => _TextWidget();
}

class _TextWidget extends State<TextWidget> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text("TextWidget"),
    );
  }
}

class ButtonWidget extends StatefulWidget {
  @override
  State<ButtonWidget> createState() => _ButtonWidget();
}

class _ButtonWidget extends State<ButtonWidget> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text("ButtonWidget"),
    );
  }
}

1 answer

  • answered 2019-05-22 08:37 Sibin

    check the changes :

    In method attachWidget every time adding the widget to the _widgetStacks and not removing from _widgetStacks from getPreviousWidget

       void main() {
      runApp(MaterialApp(
        title: 'AndroidMonks',
        home: Scaffold(
          appBar: AppBar(
            title: Text('Androidmonks'),
            backgroundColor: Colors.orangeAccent,
          ),
          body: Home(),
        ),
      ));
    }
    
    class Home extends StatefulWidget {
      Home({
        Key key,
      }) : super(key: key);
    
      @override
      State<Home> createState() => _Home();
    }
    
    class _Home extends State<Home> {
      String title = "Title";
      int _currentWidget = 0;
      final List<int> _widgetStacks = [0];
    
      @override
      Widget build(BuildContext context) {
        attachWidget(_currentWidget);
        List<Widget> _widgets = [TextWidget(), ImageWidget(), ButtonWidget()];
    
        //TextWidget
        return WillPopScope(
          onWillPop: () {
            getPreviousWidget();
          },
          child: Scaffold(
            body: Column(
              children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    RaisedButton(
                      child: Text('Attach widget'.toUpperCase()),
                      onPressed: () {
                        var rnd = Random();
                        _currentWidget = rnd.nextInt(3);
                        _widgetStacks.add(_currentWidget);
                        attachWidget(_currentWidget);
                      },
                    ),
                    RaisedButton(
                      child: Text('PREVIOUS widget'.toUpperCase()),
                      onPressed: () {
                        getPreviousWidget();
                      },
                    ),
                  ],
                ),
                Expanded(
                  child: _widgets[_currentWidget],
                ),
              ],
            ),
          ),
        );
      }
    
      void attachWidget(int index) {
         setState(() {
          _currentWidget = index;
        });
      }
    
    
      getPreviousWidget() {
        if (_widgetStacks.length - 1 > 0) {
          _widgetStacks.removeAt(_widgetStacks.length - 1);
          attachWidget(_widgetStacks[_widgetStacks.length - 1]);
        } else {
          Navigator.pop(context);
        }
      }
    }
    
    class ImageWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Center(
          child: Text("ImageWidget"),
        );
      }
    }
    
    class TextWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Center(
          child: Text("TextWidget"),
        );
      }
    }
    
    class ButtonWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Center(
          child: Text("ButtonWidget"),
        );
      }
    }