推入后无法获取堆叠的小部件列表

时间:2019-05-21 18:14:30

标签: dart flutter

我创建了一个简单的应用程序来推送和弹出变量上列出的随机窗口小部件:

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

在屏幕上,我必须按

  • 1)附加随机小部件
  • 2)上一个窗口小部件

点击附加随机小部件后,我将其存储在

final List<int> _widgetStacks = [0];

应用程序可以很好地使用此功能来存储它们

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

现在我想通过此功能单击上一个窗口小部件返回_widgetStacks

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;
  });
}

通过多次单击上一个窗口小部件不起作用

,此功能将起作用

我的实现代码:

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 个答案:

答案 0 :(得分:0)

检查更改:

在方法attachWidget中,每次将小部件添加到_widgetStacks而不是从_widgetStacks中将其从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"),
    );
  }
}