1

I'm creating a card widget that will be used to create a list of cards. I want to pass a parameter isLastCard to each card, so that I can increase margin for the last card in the list.

I have the following setup:

class CardsIndex extends StatelessWidget {

  Widget _card(context, label, bool isLastCard) {
    const double bottomMargin = isLastCard ? 40.0 : 8.0;

    return new Container(
      margin: const EdgeInsets.fromLTRB(12.0, 8.0, 12.0, bottomMargin),
      child: new Row(
          children: <Widget>[
          new Expanded(
              child: new Text(label),
          ),
          ],
      ),
    );
  }

  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
            title: new Text("Cards"),
        ),
        body: new Container(
          child: new Stack(
            children: [
              new Container(
                child: new ListView(
                  children: <Widget>[
                      _card(context, 'Card 1', false),
                      _card(context, 'Card 2', false),
                      _card(context, 'Card 3', true),
                  ],
                )
              )
            ],
          ),
        ),
    );
  }
}

This gives me this error in the output, for isLastCard inside the turnary: Const variables must be initialized with a constant value.

How do I correctly define isLastCard and bottomMargin in the _card widget?

Thanks!

1 Answer 1

2

Figured it out.

I had to define bottomMargin as so: double bottomMargin = isLastCard ? 40.0 : 8.0;

And because I was using this to set margin on the container, I had to not define margin as a const, like so: margin: EdgeInsets.fromLTRB(12.0, 8.0, 12.0, bottomMargin)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.