I’m wondering why ChoiceChip
is not expanding like ElevatedButton
does.
Here is an example: (look at dartpad)
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: MyWidget(),
),
);
}
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(children: [
IntrinsicWidth(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: ElevatedButton(
child: const Text('Larger Text'),
onPressed: () {},
)),
Expanded(
child: ElevatedButton(
child: const Text('Text'),
onPressed: () {},
)),
],
)),
IntrinsicWidth(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: const [
Expanded(
child: ChoiceChip(
label: Text('Larger Text'),
selected: false,
)),
Expanded(
child: ChoiceChip(
label: Text('Text'),
selected: false,
)),
],
))
]);
}
}
2
Answers
Because under the hood the widgets are designed differently (that’s why the names).
I haven’t checked the code in detail (you can do that by ctrl + click on Widget Name).
But best guess, visual density is lower in chips for being compact and higher in buttons.
More on technical difference to understand which to use : chips-vs-button
To solve this, let’s start by enabling
Flutter Inspector
, we can see the secondIntrinsicWidth
width and its children don’t fill their parent as the first one (two problems)Solve 1st problem: IntrinsicWidth children don’t fill their parent width
So, the problem is the size of the
2nd IntrinsicWidth
‘s children is not wide/big enough so that it can be full of parent width. Try increasing its width manually by wrapping it in aContainer
withdouble.infinity
width, like this:New result:
Solve 2nd problem: the 2nd
IntrinsicWidth
don’t fill their parent widthLet’s leave
Column
children can have the maximum width as it is (removing allIntrinsicWidth
inside its children) and then wrap theColumn
by anIntrinsicWidth
. Complete sample code:Sample code
Final result: