Hello, welcome to this chapter of the course where we will build the first of the Flutter beginner projects, a simple calculator app with a good UI.
Below is the application that we will be developing step by step in this chapter.
Note: It is preferrable you have a basic foundation of Flutter before you tackle these tutorials. You can go through the tutorials in the course Flutter Tour or use the official Flutter docs.
Pause
Okay, now that you are done, let's begin from the same knowledge base. Quickly generating the app template that becomes our reference
siliconSavanna@Fourier-PC
flutter
flutter create calculator_app_doc|
Note: When running flutter run, the emulator should be running (or the physical device should be connected)
After generating the app, the codebase for lib/main.dart should be up to date as follows:
Now that we have a template of the calculator app, let's start with generating two widgets, the top part and the bottom part.
Then, we'll first focus on the bottom part, that is, the buttons for the calculator app.Let us create the two widgets that form the two parts of the calculator app
The top part, the part that will encompass the widget for the toggle buttons for the dark and light mode, and also the view for the pressed actions for the buttons of the calculator.
Before we render the buttons onto the gridview in the bottom widget, let us initialize an array of the data of the buttons. We'll do this in a file, buttons_data.dart
Then, we'll render this in ButtonsPalette, injected as a descendant of the BottomWidget
I'll create the ButtonsPalette widget in a separate file, buttons_palette.dart, and import it to the lib/main.dart
Before we furnish the styles for the buttons in the ButtonsPalette, let's import the theme data we'll use in the MaterialApp widget. The contents of the lib/theme_data.dart are as follows
Now let's style the buttons of the ButtonsPalette widget. Note that the Center that wraps the TextButton prevents buttons from having unconstrained height and width, from where we set our height and width using the fixedSize attribute.
Also note, the first three buttons, index <= 2, have green text, and also, the last column buttons have red text.
You must do a hot restart for the theme data to be rendered in the buttons.
Now onto the top part of the calculator app, the widgets wrapping the dark mode toggle and the calculator screen results are in a column, one at the very top and the other at the very end, at the half part of the app.
Let us create two files, lib/light_toggle.dart and lib/calculation_results.dart.