Flutter – A Simple Multi-Player Dice Game
Here, we are going to create a dice game. As the name suggests there will be two dice and either player one or player two rolls the dice and according to the result the winner is decided.
What is Flutter?
Flutter is Google’s UI toolkit for building beautiful, natively compiled applications for mobile, web, desktop, and embedded devices from a single codebase. Guide on installation of flutter can be found on their documentation.
Create a new Flutter project
When installing flutter you might have selected your preferred editor. Just go on the editor and create a new flutter project. If you wish to create the project via command line or if you are using VS Code enter the following command in terminal
$ flutter create dice
Approach to building the application
Before we start with flutter let’s first take a look at the approach to building our game. We’ll add 6 images of dice each having numbers (1-6). At first, both dice will show 1 and no result. When either player 1 or player 2 rolls the dice we’ll randomly select one of the 6 images we added and based on the outcome we’ll declare the result whether it is player 1 who won or player 2 or it is a draw.
Add assets to the project
To add assets in the flutter project you can create a folder that contains all your assets at the root of the project. Now that we only have image assets we’ll create a folder called “images” and add the images to that folder. Typically you would create a folder called assets then inside it a folder called images this is due to the fact that many times apps have different assets such as audio, video, etc. You can find images with dice on the internet.
To let flutter know that we have added these images we’ll go to pubspec.yaml file and uncomment the assets line and added the image folder.
Code main.dart file
First, make sure that you delete all the code inside the main.dart file and delete the file inside the test folder.
Initialize Flutter app
After running the app you’ll see the app bar with a title on your screen.
Display the dice
Add functionality of rolling the dice and announcing the winner