Challenge: Beating Heart
Test your understanding of adding animations to your app.
In this lesson, you’ll work on a challenge that will test your understanding of animations.
Goal
Understand how to create a forever-repeating animation.
Specs
The heart icon should animate in size from small, size 16, to large, size 24, to small again continuously.
Starter code
Before beginning the challenge, you can look at the starter code below and run it. Once done, you can dive into the challenges below.
import 'package:flutter/material.dart'; import 'single_contact_screen.dart'; class ContactListScreen extends StatefulWidget { const ContactListScreen({ Key? key, }) : super(key: key); @override State<ContactListScreen> createState() => _ContactListScreenState(); } // TODO: CHALLENGE #1 - add TickerProviderStateMixin to the widget state class class _ContactListScreenState extends State<ContactListScreen> { // TODO: CHALLENGE #2 Create an animation controller // TODO: CHALLENGE #3 Create an animation that uses the animation controller @override Widget build(BuildContext context) { return Scaffold( body: SafeArea( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: const EdgeInsets.all(16.0), child: Wrap( direction: Axis.vertical, children: [ Text( "Contacts", style: Theme.of(context) .textTheme .headline3! .copyWith(color: Colors.black), ), Text("20 Contacts", style: Theme.of(context) .textTheme .bodyText2! .copyWith(color: Colors.black54)), ], ), ), Expanded( child: ListView.separated( itemCount: 20, itemBuilder: (context, index) => Dismissible( key: Key("$index"), child: ListTile( onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => const SingleContactScreen()), ); }, leading: const CircleAvatar(child: Text("IW")), title: const Text("Ivy Walobwa"), // TODO: CHALLENGE #4 - Animate the heart icon size trailing: const Icon(Icons.favorite, color: Colors.red), ), ), separatorBuilder: (BuildContext context, int index) => const Divider(height: 1), ), ), ], ), ), ); } @override void dispose() { super.dispose(); // TODO: Challenge #5 - dispose the animation controller on widget dispose } }
Beating heart challenge starter app