简体   繁体   中英

How to programmatically call multiple widget under a stack in flutter

I've a screen with several buttons and I would like to show a sliding panel for each button (passing some information to show in the panel).

I've used this widget: https://pub.dev/packages/sliding_up_panel

I would like to understand how I can show a widget that is under a Stack passing programmatically some information once I click on a specific button. It is possible?

Following a brief snipped of code in which we can see the Stack that contains the SlidingPanel. I would like to be able to show the Sliding panel clicking on a specific button and passing an information to it programmatically.

The only solution that I've found up to now is adding all the possible sliding panel to the Stack and then based on the click on the button showing only the one I need, but I hope there is a better way to do this.

Thank you

return Stack(
  children: [
    Scaffold(
    .....
    ),
    SlidingUpPanel(
      panelController: panelController,
      nameTEController: nameTEPanel,
      platform: 'Instagram',
    )
    ]

Just populate your list of Widgets inside a List. If you are using a StatefullWidget, then just call setState(() => )

You could do something like this:

List<Widget> _myWidgetList = [];
[...]
return Stack(
  children: _myWidgetList);

List<Widget> _buildMyWidgetList() {
  final List<Widget> tmpList = [];
  tmpList..add(Widget1)
    ..add(Widget2);
  setState(() => _myWidgetList = tmp);
}

Then just call your buildWidgets functions on the Buttons.

Another way would be to use Bloc: https://pub.dev/packages/flutter_bloc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM