TaylorR
TaylorR

Reputation: 4013

Flutter Gallery demo: Cards to change card's height

I am trying to modify the cards_demo.dart found in Flutter examples. My purpose is that instead of having the built-in two cards' height be fixed as:

static final double height=300.0 (or some mandatory and fixed number), I want to have different height for the two cards.

So I modified the TravelDestination class to include a property height:

class TravelDestination {
  const TravelDestination({ this.assetName, this.title, this.description, this.height });

  final String assetName;
  final String title;
  final List<String> description;
  final double height;

  bool get isValid => assetName != null && title != null && description?.length == 3;
}

Then, in class TravelDestinationItem build function:

class TravelDestinationItem extends StatelessWidget {
  TravelDestinationItem({ Key key, @required this.destination }) : super(key: key) {
    assert(destination != null && destination.isValid);
  }

  static final double height = 512.0;
  final TravelDestination destination;

  @override
  Widget build(BuildContext context) {
    final ThemeData theme = Theme.of(context);
    final TextStyle titleStyle = theme.textTheme.headline.copyWith(color: Colors.white);
    final TextStyle descriptionStyle = theme.textTheme.subhead;

    return new Container(
      padding: const EdgeInsets.all(8.0),
      height: destination.height,
      //height: height,
      child: new Card(
        child: new Column(... ...

I assigned different height property to the two cards but the result is not working: they are still the same height as specified by static final double height.

If I comment out static final double height line, the compiler will remind me: No static getter 'height' declared...

I am very much confused on this behavior.

Can anyone help?

Upvotes: 0

Views: 5434

Answers (1)

Collin Jackson
Collin Jackson

Reputation: 116728

Since you're using items of varying height, you should remove this line from the call to the ListView constructor:

itemExtent: TravelDestinationItem.height,

Also, you'll need to hot-restart the app (hot-reloading won't update the destinations list with the new data, since it's a global variable).

Upvotes: 2

Related Questions