Reusable Custom Card Widget

1. Create Dog Card Widget

We need a nice widget to display our doggos.

First you'll make a card that looks like this:

dog card

Create a new file called 'dog_card.dart`.

In that file, make a new, blank StatefulWidget. It should take a Dog in its constructor.

For the time being, all this will do is display the name of a dog.

  1. // dog_card.dart
  2. import 'package:flutter/material.dart';
  3. import 'dog_model.dart';
  4. class DogCard extends StatefulWidget {
  5. final Dog dog;
  6. DogCard(this.dog);
  7. @override
  8. _DogCardState createState() => _DogCardState(dog);
  9. }
  10. class _DogCardState extends State<DogCard> {
  11. Dog dog;
  12. _DogCardState(this.dog);
  13. @override
  14. Widget build(BuildContext context) {
  15. return Text(widget.dog.name);
  16. }
  17. }

In order to make the DogCard appear, let's modify the _MyHomePageState build method in main.dart:

  1. // main.dart
  2. @override
  3. Widget build(BuildContext context) {
  4. return Scaffold(
  5. appBar: AppBar(
  6. title: Text(widget.title),
  7. backgroundColor: Colors.black87,
  8. ),
  9. body: Container(
  10. child: DogCard(initialDoggos[1]), // New code
  11. ),
  12. );
  13. }

And import dog_card.dart:

  1. // main.dart
  2. import 'package:flutter/material.dart';
  3. import 'dog_card.dart';
  4. import 'dog_model.dart';

Refresh your app and you can see that it's wired up now. Time to build the card.

2. Dog Card UI

There are two main parts to this card. The image, and the actual Card that sits under it.

First, make that image.

Add this getter to you _DogCardState class:

  1. // dog_card.dart
  2. // A class property that represents the URL flutter will render
  3. // from the Dog class.
  4. String renderUrl;
  5. Widget get dogImage {
  6. return Container(
  7. // You can explicitly set heights and widths on Containers.
  8. // Otherwise they take up as much space as their children.
  9. width: 100.0,
  10. height: 100.0,
  11. // Decoration is a property that lets you style the container.
  12. // It expects a BoxDecoration.
  13. decoration: BoxDecoration(
  14. // BoxDecorations have many possible properties.
  15. // Using BoxShape with a background image is the
  16. // easiest way to make a circle cropped avatar style image.
  17. shape: BoxShape.circle,
  18. image: DecorationImage(
  19. // Just like CSS's `imagesize` property.
  20. fit: BoxFit.cover,
  21. // A NetworkImage widget is a widget that
  22. // takes a URL to an image.
  23. // ImageProviders (such as NetworkImage) are ideal
  24. // when your image needs to be loaded or can change.
  25. // Use the null check to avoid an error.
  26. image: NetworkImage(renderUrl ?? ''),
  27. ),
  28. ),
  29. );
  30. }

In order to see this image, you'll first need to tell the Dog class to get that image from the internets.

In your dog card, add this to your _DogCardState class:

  1. // dog_card.dart
  2. // State classes run this method when the state is created.
  3. // You shouldn't do async work in initState, so we'll defer it
  4. // to another method.
  5. void initState() {
  6. super.initState();
  7. renderDogPic();
  8. }
  9. // IRL, we'd want the Dog class itself to get the image
  10. // but this is a simpler way to explain Flutter basics
  11. void renderDogPic() async {
  12. // this makes the service call
  13. await dog.getImageUrl();
  14. // setState tells Flutter to rerender anything that's been changed.
  15. // setState cannot be async, so we use a variable that can be overwritten
  16. if (mounted) { // Avoid calling `setState` if the widget is no longer in the widget tree.
  17. setState(() {
  18. renderUrl = dog.imageUrl;
  19. });
  20. }
  21. }

Now you have a doggos avatar that's properly getting the URL to render.

In order to get the overlap look of the card, use the built-in widget Stack.

The Stack widget lays out its children relative to its edges.

In other words, it's CSS's position, top, bottom, left and right properties.

Within a stack, you can wrap children in 'Position' widgets, but you don't have to.

  • Position wrapped widgets are outside of document flow, to use web development terms. They'll be at position [0,0] by default — the top corner of the Stack widget.
  • Non-wrapped widgets aren't positioned. They stay in normal 'document flow', laid out as a column of widgets by default.

This is how the stack is going to start:

  1. // dog_card.dart
  2. @override
  3. Widget build(BuildContext context) {
  4. // Start with a container so we can add layout and style props:
  5. return Container(
  6. // Arbitrary number that I decided looked good:
  7. height: 115.0,
  8. // A stack takes children, with a list of widgets.
  9. child: Stack(
  10. children: <Widget>[
  11. // position our dog image, so we can explicitly place it.
  12. // We'll place it after we've made the card.
  13. Positioned(
  14. child: dogImage,
  15. ),
  16. ],
  17. ),
  18. );
  19. }

Reload your app to see a picture of a dog in the top corner.

Let's create the card and layout in _DogCardState:

  1. // dog_card.dart
  2. Widget get dogCard {
  3. // A new container
  4. // The height and width are arbitrary numbers for styling.
  5. return Container(
  6. width: 290.0,
  7. height: 115.0,
  8. child: Card(
  9. color: Colors.black87,
  10. // Wrap children in a Padding widget in order to give padding.
  11. child: Padding(
  12. // The class that controls padding is called 'EdgeInsets'
  13. // The EdgeInsets.only constructor is used to set
  14. // padding explicitly to each side of the child.
  15. padding: const EdgeInsets.only(
  16. top: 8.0,
  17. bottom: 8.0,
  18. left: 64.0,
  19. ),
  20. // Column is another layout widget -- like stack -- that
  21. // takes a list of widgets as children, and lays the
  22. // widgets out from top to bottom.
  23. child: Column(
  24. // These alignment properties function exactly like
  25. // CSS flexbox properties.
  26. // The main axis of a column is the vertical axis,
  27. // `MainAxisAlignment.spaceAround` is equivalent of
  28. // CSS's 'justify-content: space-around' in a vertically
  29. // laid out flexbox.
  30. crossAxisAlignment: CrossAxisAlignment.start,
  31. mainAxisAlignment: MainAxisAlignment.spaceAround,
  32. children: <Widget>[
  33. Text(widget.dog.name,
  34. // Themes are set in the MaterialApp widget at the root of your app.
  35. // They have default values -- which we're using because we didn't set our own.
  36. // They're great for having consistent, app-wide styling that's easily changed.
  37. style: Theme.of(context).textTheme.headline),
  38. Text(widget.dog.location,
  39. style: Theme.of(context).textTheme.subhead),
  40. Row(
  41. children: <Widget>[
  42. Icon(
  43. Icons.star,
  44. ),
  45. Text(': ${widget.dog.rating} / 10')
  46. ],
  47. )
  48. ],
  49. ),
  50. ),
  51. ),
  52. );
  53. }

Almost there. One more thing you need to do to complete the DogCard UI. Add a bit more styling to the main widget in the build method:

  1. // dog_card.dart
  2. @override
  3. Widget build(BuildContext context) {
  4. return Padding(
  5. padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
  6. child: Container(
  7. height: 115.0,
  8. child: Stack(
  9. children: <Widget>[
  10. Positioned(
  11. left: 50.0,
  12. child: dogCard,
  13. ),
  14. Positioned(top: 7.5, child: dogImage),
  15. ],
  16. ),
  17. ),
  18. );
  19. }