Home > Enterprise >  Flutter reverse order of Children inside Column
Flutter reverse order of Children inside Column

Time:08-26

Is there a way to reverse the order of Children inside Column? Because I'm using a custom Widget and the widget appears two times: first time the order is good, the second time order is reversed.

Need to reverse the order of Text elements in Column:

Column(
          verticalDirection: VerticalDirection.up,
          children: [
            Text(
              dTDInfoValue,
              style: const TextStyle(
                  fontSize: AppFontSizes.aboutMountainDTDInfoText,
                  color: AppColors.aboutMountainDTDInfoColor,
                  fontWeight: FontWeight.w400),
            ),
            const SizedBox(height: AppConstants.aboutMountainDTDSizedBoxHeight),
            Text(
              dTDInfoName,
              style: const TextStyle(
                  fontSize: AppFontSizes.aboutMountainDTDNameText,
                  color: AppColors.aboutMountainDTDInfoNameColor),
            )
          ],
        )

CodePudding user response:

Use the List<Widget> to store the Widget you need.

List<Widget> _widgetList = [/* your Widgets */];

Column(
          verticalDirection: VerticalDirection.up,
          children: [
            for(int i=0; i<_widgetList.length; i  )
              _widgetList[i];
          ],
        )

Use the reversed method of List to reverse the order.

_widgetList.reversed;

CodePudding user response:

You are trying to change the order of item, It will be easier to make a list and then reverse if needed.

You can do something like

body: Column(
  // verticalDirection: VerticalDirection.up,
  children: [
    ...[
      Text(
        "XdTDInfoValue",
        style: const TextStyle(fontWeight: FontWeight.w400),
      ),
      const SizedBox(height: 100),
      Text(
        "dTDInfoName",
      ),
    ].reversed.map((e) => e).toList()
  ],
),

CodePudding user response:

Create a state variable of type List say

List<Widget>myList=[Widget1, Widget2, Widget3...],

and to reverse use method myList.reversed.

Assign this variable to Column children and use myList.reversed within setState whenever you need to reverse the list.

CodePudding user response:

List<String> randomWords = ['abc', 'xyz', '123'];

List<String> reversedRandomWords = randomWords.reversed.toList();

print(reversedRandomWords); // [ '123','xyz', 'abc'];
  • Related