I have a list of strings for tabs, and want to give same width to all tabs instead of based on text length…
Like I have 4 tabs , it should be occupy same width ,
and if text length is bigger than text size should b adjusted..
DONT want to user listview, all tabs should be adjusted according to available width
like width is 300 and tabs are 2, each tab should be in width of 150(including padding etc..)
but I am getting following which I dont want to set widget based on text length
class HomeScreen extends StatelessWidget {
HomeScreen({Key? key}) : super(key: key);
List<String> titles=['All','Income','Expense','Debt','Others','Liabilities'];
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Container(
height: 100,
color: Colors.grey,
child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: titles.map((e) => Padding(
padding: const EdgeInsets.symmetric(horizontal: 2.0),
child: Container(
padding: EdgeInsets.symmetric(horizontal: 8,vertical: 4),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.white,
),
child: Text(e)),
)).toList(),),
),
),
);
}
}
4
Answers
You can use a LayoutBuilder.
This widget allows you to get the parent’s widget width, and according to that you can then use N SizedBox to limit the width of your chips.
You can achieve this by wrapping each tab in a Container widget with a fixed width and set the mainAxisAlignment property of the parent Row widget to MainAxisAlignment.spaceBetween. This will distribute the tabs evenly across the available width like this.
You can adjust the width property of the Container widgets as per your requirements. If the text is larger than the container width, you can adjust the text size using the style property of the Text widget and set the overflow property of the Container widget to TextOverflow.ellipsis to display an ellipsis (…) when the text overflows the container.
Wrap each child of Row inside Expanded and Replace Text With AutoSizeText https://pub.dev/packages/auto_size_text
Output:
Take a look at Boxy, which even has an example of doing what you want to do (make a series of items the width of the largest item).