This tutorial is motivated by two main factors:
- Firstly, I wanted to make use of devices' physical sensors, which rarely get the spotlight in applications I work with on daily basis.
- Secondly, I wanted to challenge myself to add new functionality to the original code while changing it as little as possible. The idea is to illustrate the benefits of a good app architecture and how it enables us to easily add new features without having to understand the details of the existing code.
This article, however, won't go into detail about the following topics:
- It will not explain Very Good Ventures' architecture. I suggest poking around the codebase yourself to get a feeling about it. You can also check out Flutter Community's videos that go into detail about this topic.
- It will not explain how phone's sensors work.
To follow the content, you should be comfortable using Dart's Streams and know the basics of bloc state management, specifically with Cubits, which we will be using in this challenge.
Although not necessarily required, you'll have a better experience testing the game on a real device, rather than an emulator or a simulator.
Note that this is a step-by-step guide to the final solution. It takes you through the entire journey that I went through, including a few dead ends.
If you just want working code without all the text, you can find in on GitHub, with most of it placed in lib/movement folder.
Setup
Before we get started, you will need to pull the starter source code.
Optionally, you can also edit analysis_options file to match your coding style. I prefer setting my formatter's line length to 120, since I believe it improves readability, especially when it comes to Widget's build methods. For this tutorial I'm also choosing to ignore the warning about documenting public methods and properties, although VGV's documentation of their source code made it way easier to understand their code at first.