fCreate
A Simple EYE Test Game, powered by Flutter CustomPainter.
A simple Android Application built with using Flutter. This application is taking part in Flutter Create Competition. That's why it has limited capabilities, built using only 5112 bytes Dart Code. And it's a Dart only application, targets Android SDK version 28.
How's it working ?
- Execution starts with App() class, which extends StatelessWidget.
- As this application only targets Android, it's a MaterialApp.
- As I'm going to add interactivity to our app, I need a StatefulWidget. Home() satisfies that need.
- Due to code limit, I'm restricting my application to be only running in Portrait mode.
- In the build method I'm returing a Scaffold Widget, which provides a sound Material Design skeleton.
- body of Scaffold is a Stack widget, which is very good at handling hiding of overlapping widgets, depending upon certain criteria or user event, by using OpacityWidget as its parent.
- Well I'm also using a FloatingActionButton(), to go to Game Widget, from Help Widget and vice versa.
- So, you've encountered a new function swap(), it just changes opacity value of two widgets and make them either visible or invisible.
- You may have already found a method called setW(), which is present due to implementation of White class in _HomeState.
- A GestureDetector widget is used as parent of CustomPaint, to handle user input event. A single tap denotes, that user selects currently shown Paint widget and wants to know whether he/ she has selected a widget which has atleast 50% white colored balls. In response to this event a Dialog shows up to indicate status of current selection.
- Now if you double click on screen, cancelled timer starts running again.
- A center aligned widget is used a child of Opacity, to display help page. Changing opacity value reveals either help page or game page. Of course parent of these Opacity widget is Stack widget.
- Lets talk about Painter class which subclasses CustomPainter and mainly takes care of painting operation to be performed in CustomPaint widget. Painter's constructor takes two parameters, number of balls to be placed along row and column( it's a square ) and an instance of White class, which works as callback mechanism for updating value of white balls generated randomly during painting, stored in a variable which resides in _HomeState class.
- As per definition of CustomPainter, need to override paint() and shouldRepaint()methods in Painter.
- So, I'll be drawing some circles, for that I need an Offset. Position along X-axis and Y-axis is determined as follows.
- White ball count is determined as following one.
- This is how circle is drawn in iterative fashion, using a while loop until we reach size.width along X-axis or size.height along Y-axis.
- In inner while loop of paint(), coordinate value of x is increased size.width / b in each iteration.
- Same for y too, in outer while loop.
- After drawing is done, number of white balls drawn on screen is updated, where White abstract class plays an role.
- This is it.