I have a parent widget GeneralBtn
:
class GeneralBtn extends StatelessWidget {
final VoidCallback onPressed;
final Widget? child;
final bool compact
GeneralBtn({
thispact = false,
required this.onPressed,
this.child,
});
@override
Widget build(BuildContext context) {
return ElevatedButton(
style: ElevatedButton.styleFrom(
padding: compact
? const EdgeInsets.symmetric(horizontal: 12, vertical: 0) // Reduced padding
: const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
visualDensity: compact
? VisualDensitypact
: VisualDensity.standard,
),
onPressed: onPressed,
child: child ?? Text(""),
);
}
}
The padding
and visualDensity
are set in GeneralBtn
.
I also have a child widget that inherits GeneralBtn
, but more importantly, it builds a GeneralBtn
that encloses another widget ListTile
. Thus ListTile
in this case is more like a child of GeneralBtn
.
class RecentCallBtn extends GeneralBtn {
// constructor ...
// build function
return GeneralBtn(
text: text,
onPressed: onPhoneServiceCall,
child: child??
ListTile(
contentPadding: compactItem
? const EdgeInsets.symmetric(horizontal: 12, vertical: 0) // Reduced padding
: const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
visualDensity: compactItem
? VisualDensitypact
: VisualDensity.standard,
// other stuffs
);
} //end build function
}// end RecentCallBtn
I see that the constrain padding and visualDensity is not applied if I put them on GeneralBtn build function. I thought that:
Constraints go down. Sizes go up. Parent sets position
as explained in /ui/layout/constraints
The following is the output if I comment out the constrain the build function of RecentCallBtn
or put the constrain in the build function of GeneralBtn
:
On the other hand, if I put the constrain of padding
and visualDensity
in RecentCallBtn
build function and remove them from GeneralBtn
, then the compact applies: (it looks more compact)