I want to display a list of buttons on a single screen, with the icon displayed at the leading of each button.
I want to align the buttons on the screen based on the button with the longest content. Icons and titles line up.
Here is an illustration
2
You can create a list of your title and sort that list according to the length of each title.
Largest at first and smallest at last.
Now using this list create a listView builder
now in each button use Row as its child
and Row children Icon and Text
Now use mainAxisAlignment as MainAxisAlignment.start
and wrap text to expand and align text to center
In this way I think you can achieve what you want.
You can make a model which will have the title and icon and colour for it. make a list of the this model:
List<buttonDetails> list = [ buttonDetails('Medium Title', Icons.density_medium, Colors.blueAccent), buttonDetails('Small Title', Icons.density_small, Colors.greenAccent), buttonDetails('VeryLarge Title', Icons.density_large, Colors.orangeAccent), ];
Sort it according to the length of the title’s letter length, you can do it in the initState() like this:
initState()
@override void initState() { list.sort((a, b) => b.title.length.compareTo(a.title.length)); super.initState(); }
And show it with using ListView.builder:
ListView.builder
ListView.builder( shrinkWrap: true, itemCount: list.length, itemBuilder: (context, index) => MaterialButton( minWidth: 300, shape: RoundedRectangleBorder( borderRadius: SizeConstant.borderRadius14, ), color: list[index].color, onPressed: () {}, child: Row( children: [ Icon( list[index].icon, ), const SizedBox(width: 16,), Text(list[index].title), ], ), ).paddingAllDefault(), ),
And will look like this:
Click here to cancel reply.
2
Answers
You can create a list of your title and sort that list according to the length of each title.
Largest at first and smallest at last.
Now using this list create a listView builder
now in each button use Row as its child
and Row children Icon and Text
Now use mainAxisAlignment as MainAxisAlignment.start
and wrap text to expand and align text to center
In this way I think you can achieve what you want.
You can make a model which will have the title and icon and colour for it. make a list of the this model:
Sort it according to the length of the title’s letter length, you can do it in the
initState()
like this:And show it with using
ListView.builder
:And will look like this: