How can I create a card layout like below in Flutter?
See my code below. The card has inner padding I can’t get rid of. This is needed to create the header. Also, I am not sure how to place the button with offset.
Card(
elevation: 10,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
//set border radius more than 50% of height and width to make circle
),
child: Column(
children: <Widget>[
Container(
padding: const EdgeInsets.only(
top: 10,
bottom: 10,
),
child: Container(
child: Text(
CustomFunctions.getProductName(product.title),
style: const TextStyle(
fontSize: 18.0,
),
),
)),
const Expanded(
child: Center(
child: FaIcon(
FontAwesomeIcons.coins,
color: CustomColors.accentColor,
))),
_buildButton("${product.currencyCode}${product.price}", () => {}),
],
),
);
}
3
Answers
I’m not sure if its the best way to do it, but I would create a
Stack
for this, instead of aCard
. Here are my results:This is build method inside of
Card
widget in thecard.dart
. The padding is coming from themargin
param insideContainer
. Ifmargin
andcardTheme.margin
is null, it will take the defaultmargin
of 4 px.If you want to change only this instance of
Card
, set themargin
. If you want to change all instances ofCard
, set themargin
inCardTheme
.From the picture, as the widgets can be placed on top of other widgets. I recommend to use
Stack
.First answer actually explains quite everything. You can use
Stack
withPositioned
to get your desired output.Your unwanted padding is causing due to your use of
Column
andExpanded
inside your card.Column
takes infinite height, andExpanded
takes infinite width. You can use something likeSizedBox
or other widgets withheight
andwidth
properties to wrap around your card that is going constrain the size of the card.Here is a Card widget I’ve used with a similar implementation I hope It will help you in your implementation:
SS Widget screenshot: