0

I have an list of widgets. The widgets are background images for a website. The last widget(image) in this list is displayed onscreen. As nav functions push/pop, the last element is removed/added.

One of the fields in the Widget(BackdropImage) is a bool called 'isForeground'. I need access to it.

Why? Some background images are foreground and are to be rendered above the site's semi-transparent background texture, some are to be rendered behind it.

If I have a List(BackDropImage) and the BackDropImage contains: Path, BoxFit, Opacity, isForeground(bool) etc etc. How can I get access to the last BackDropImage in BackDropImage list and access its isForground field?

//PSUDO WIDGET TREE:

if(backdropImageList.last's 'isForground' field is FALSE) BackDropImage.last,//render here below the texture BackgroundTextureWidget, if(backdropImageList.last's isForground field is TRUE) BackDropImage.last //render here above the texture HeadingWidget, OtherWidgets

I'd appreciate help if possible (or alternative approaches). The alterative is to flip a bool programmatically every time a button is pressed/popped and keeping track of what images are displayed where. Knowing which images are fore/background in the first place and controlling from the object itself is much neater IMO. Thanks folks.

2 Answers2

0
 list.firstWhere((x) => x['isForground'] == true);
Eric
  • 352
  • 3
  • 11
  • Not quite. That finds the first widget in a list that has a true value in that field. I want to take the last actual widget in the list and determine if that specific widgets isForground field is true or false. – Mark Bowler Jun 13 '22 at 07:40
  • Widget widget = list.last; if(widget.isForeground == true) {....} https://stackoverflow.com/questions/50287995/passing-data-to-statefulwidget-and-accessing-it-in-its-state-in-flutter – Eric Jun 13 '22 at 09:02
  • That example is for passing down constructor values to the state class in a way that somehow duplicates the fields instantiated originally but can only be accessed within the Widget build method. Widget object is an abstract class that doesn't usually have access to isForground except when somehow passed to the build method in a widget state, which I'm not using as the data is in a controller. Basically I cant use this Widget bla = Text('aaa', semanticsLabel = 'label') because you cant go bla.semanticsLabel. You can only do this if you instantiate it as Text bla = Text(...) – Mark Bowler Jun 13 '22 at 21:36
0

Widget is an abstract class. When working with Widgets like Container etc that have member variables you need to access programmatically, it can be done like this:

Widget testContainer = Container (width:100)
print(testContainer.width) // WON'T WORK
print((testContainer as Container).width) // WILL WORK 

To access the member variables of individual Widgets in a list of Widgets:

List<Widget> listOfWidgets = [Container(color:Colors.blue), Container(color:Colors.red), Container(color:Colors.green)]

void printColorsOfAllContainers () {

for (var element in listOfWidgets) {
    if (element is Container) {
      print(element.color);
    }
  }
}

Alternatively, you can also do things like this:

void printColorsOfAllContainers() {
  final List<Color> listOfContainerColours = [];
  for (var element in listOfWidgets) {
    element as Container;
    listOfContainerColours.add(element.color!);
  }
}