Home > Enterprise >  flutter : use index in position , stack parent
flutter : use index in position , stack parent

Time:06-22

i have to use index in position , but when I use listView.builder for I , I got this error :

Incorrect use of ParentDataWidget.

The ParentDataWidget Positioned(left: 157.2) wants to apply ParentData of type StackParentData to a RenderObject, which has been set up to accept ParentData of incompatible type FlexParentData.

Usually, this means that the Positioned widget has the wrong ancestor RenderObjectWidget. Typically, Positioned widgets are placed directly inside Stack widgets. The offending Positioned is currently placed inside a Row widget.

and app crashed.

i use stack -> listview.bulder(for index) -> position

but correct is stack -> position

I have to use index in position. so can anyone help me how can I do that ?

for example myList are :

myList = [{"data" : 100.0},{"data" : 200.0 },{"data" : 300.0 }, {"data" :400.0 },{"data" : 500.0} ]

here is my code :

Stack(
                  alignment: AlignmentDirectional.topStart,
                  children: [
                    Positioned(
                          left: (myList[index]["data"]),
                          child: Directionality(
                            textDirection: TextDirection.ltr,
                            child: MySimpleIcons(
                              iconName: MyIcons.bookProfile,
                              iconColor: theme.darkGrey,
                              iconSize: 24,
                            ),
                          ),
                 // another widgets
                 ) ],

CodePudding user response:

You can use .map & spread operator to fulfill this case. You can try the following code:

Stack(
      alignment: AlignmentDirectional.topStart,
      children: [
        ...myList.map((position) => Positioned(
                  left: position,
                  child: Directionality(
                    textDirection: TextDirection.ltr,
                    child: MySimpleIcons(
                      iconName: MyIcons.bookProfile,
                      iconColor: theme.darkGrey,
                      iconSize: 24,
                    ),
                  ),
                )
            // another widgets
            )
      ],
    );
  • Related