4.2 KiB
4.2 KiB
title, tags
| title | tags | |
|---|---|---|
| Flutter Dicee Project Study Guide |
|
Flutter Dicee Project Study Guide
Project Overview
This project creates a dice rolling app using Flutter. It demonstrates key concepts in Flutter development, including stateful widgets, random number generation, and interactive UI elements.
Key Components
1. Main Dart File (main.dart)
Entry Point
void main() {
return runApp(
MaterialApp(
// ...
),
);
}
- The
main()function is the entry point of the Flutter app. runApp()takes the root widget of the app as an argument.
App Structure
MaterialApp(
home: Scaffold(
backgroundColor: Colors.red,
appBar: AppBar(
// ...
),
body: DicePage(),
),
)
MaterialAppprovides the base structure for the app.Scaffoldimplements the basic material design layout.AppBarcreates a custom app bar with a title and bottom border.DicePageis the main content of the app.
2. Widget Tree
To include the widget tree diagram in your markdown notes, use the following code block:
graph TD
A[MaterialApp] --> B[Scaffold]
B --> C[AppBar]
B --> D[DicePage Stateful Widget]
D --> E[Center]
E --> F[Row]
F --> G[Expanded Left]
F --> H[Expanded Right]
G --> I[TextButton Left]
H --> J[TextButton Right]
I --> K[Image.asset Left]
J --> L[Image.asset Right]
3. Key Widgets and Their Purposes
- MaterialApp: Provides default theme and navigation structure.
- Scaffold: Implements the basic material design layout.
- AppBar: Creates a custom app bar for the app.
- DicePage (StatefulWidget): The main content widget that can update its state.
- Center: Centers its child within itself.
- Row: Arranges its children horizontally.
- Expanded: Expands a child of a Row, Column, or Flex.
- TextButton: A Material Design text button.
- Image.asset: Displays an image from the app's assets.
4. Stateful Widget Implementation
class DicePage extends StatefulWidget {
@override
State<DicePage> createState() => _DicePageState();
}
class _DicePageState extends State<DicePage> {
int leftDiceNumber = 1;
int rightDiceNumber = 1;
void rollDice() {
setState(() {
leftDiceNumber = Random().nextInt(6) + 1;
rightDiceNumber = Random().nextInt(6) + 1;
});
}
@override
Widget build(BuildContext context) {
// ... widget tree
}
}
DicePageis aStatefulWidgetthat creates and manages a_DicePageState._DicePageStatecontains the mutable state and thebuildmethod.rollDice()method updates the state usingsetState().
5. Interactivity and State Management
TextButtonwidgets are used to make the dice images tappable.onPressedproperty ofTextButtonis set to callrollDice().setState()is used to update the UI when dice values change.
6. Asset Management
In pubspec.yaml:
flutter:
assets:
- images/
This allows the use of Image.asset('images/dice$diceNumber.png') in the code.
Learning Outcomes
- Creating and using Stateful Widgets
- Implementing interactivity with
TextButton - Managing app state with
setState() - Using
Random()for generating random numbers - Structuring a Flutter app with
MaterialAppandScaffold - Using
RowandExpandedfor layout - Loading and displaying images from assets
- String interpolation in Dart (
'images/dice$diceNumber.png')
Exercises to Reinforce Learning
- Add a "Roll Both" button that changes both dice at once.
- Implement a score counter that increments based on the dice values.
- Add animations to the dice when they change.
- Create a custom app theme and apply it to the entire app.
- Implement a "lock" feature for each die, allowing the user to keep one die's value while rolling the other.
Finished App
Additional Resources
Remember to refer to the README.md file for the project goals and additional learning points!
