Built In Animation

At this point, your app has all the functionality that you'll implement.

There are some easy animations Flutter gives you that will enhance the UX by a mile.

  • Add placeholder for images, so they fade in on load.
  • Add a hero animation that connects your DogCard to the dog_detail_page.

1. Add an AnimatedCrossFade to load Widgets on state change

This all happens in your _DogCardState class.

  1. // dog_card.dart
  2. Widget get dogImage {
  3. var dogAvatar = Container(
  4. width: 100.0,
  5. height: 100.0,
  6. decoration: BoxDecoration(
  7. shape: BoxShape.circle,
  8. image: DecorationImage(
  9. fit: BoxFit.cover,
  10. image: NetworkImage(renderUrl ?? ''),
  11. ),
  12. ),
  13. );
  14. // Placeholder is a static container the same size as the dog image.
  15. var placeholder = Container(
  16. width: 100.0,
  17. height: 100.0,
  18. decoration: BoxDecoration(
  19. shape: BoxShape.circle,
  20. gradient: LinearGradient(
  21. begin: Alignment.topLeft,
  22. end: Alignment.bottomRight,
  23. colors: [Colors.black54, Colors.black, Colors.blueGrey[600]],
  24. ),
  25. ),
  26. alignment: Alignment.center,
  27. child: Text(
  28. 'DOGGO',
  29. textAlign: TextAlign.center,
  30. ),
  31. );
  32. // This is an animated widget built into flutter.
  33. return AnimatedCrossFade(
  34. // You pass it the starting widget and the ending widget.
  35. firstChild: placeholder,
  36. secondChild: dogAvatar,
  37. // Then, you pass it a ternary that should be based on your state
  38. //
  39. // If renderUrl is null tell the widget to use the placeholder,
  40. // otherwise use the dogAvatar.
  41. crossFadeState: renderUrl == null
  42. ? CrossFadeState.showFirst
  43. : CrossFadeState.showSecond,
  44. // Finally, pass in the amount of time the fade should take.
  45. duration: Duration(milliseconds: 1000),
  46. );
  47. }

Give your app a full restart and you should see it fade in (slowly) when the image is loaded.