Scaffolding
A brick to scaffold a flutter application for CRUD (Create, Read, Update and Delete)!
This is based on rails and grails scaffolding.
Lots of ideas, structure and approach lifted directly from their documentation, source code and my own prior experience using the scaffolding approach in grails/rails to bootstrap CRUD web application.
In creating this brick I thought why not try take these learning/reduce boilerplate benefits to flutter / dart even if only to help quickly educate the patterns in building, testing and scaling a CRUD application using state management in BLoC (Business logic controller) and unit tests.
Please note this has nothing to do with the flutter Scaffold (https://api.flutter.dev/flutter/material/Scaffold-class.html) material class widget.
Samples screens
Table of Contents
How to use š
Simple flutter project
Start with a simple flutter project and add the following dependencies
equatable
uuid
flutter_bloc
For the generated unit test you will also need
mockito
bloc_test
The steps to add from the command line would be:
flutter create scaffolding_sample
cd scaffolding_sample
flutter pub add equatable uuid flutter_bloc
flutter pub add mocktail bloc_test --dev
Next setup mason to scaffold your code.
Command Line
mason init
mason add scaffolding
mason make scaffolding --package scaffolding_sample --feature feature1 --properties "String firstName=Your first name, String lastName=Your surname, bool registered=false, int age=21" --generate-tests true --generate-home true
To confirm the tests are all passing with full coverage run the following from the based of the project (noting the removal of the widget_test from flutter create):
rm test/widget_test.dart
flutter test --coverage && genhtml -p ${PWD}/lib -o coverage coverage/lcov.info && open coverage/index.html
You should see a result similar to the image below in your browser.
Variables for the Command Line and Config Json āØ
Variable | Description | Default | Type |
---|---|---|---|
package | The name of the package this is generated into | scaffolding_sample | string |
feature | The name of the feature | feature1 | string |
properties | string of the properties of this feature (string,bool,num) | string | |
generate_tests | Generate units tests using scaffolding-tests brick | true | boolean |
generate_home | Generate home/main files tests using scaffolding-main brick | false | boolean |
Config
mason make scaffolding -c scaffolding_config.json
{
"package": "scaffolding_sample",
"feature": "feature1",
"generate-tests": true, // true or false
"generate-home": true, // true or false
"properties": "String firstName=Your first name, String lastName=Your surname, bool registered=false, int age=21"
}
Outputs š¦
--package scaffolding_sample --feature feature1 --properties "String firstName=Your first name, String lastName=Your surname, bool registered=false, int age=21" --generate-tests true --generate-home true
lib/
āā features/
ā āā feature1/
ā āā data/
ā ā āā feature1_model.dart
ā ā āā feature1_repository_impl.dart
ā āā domain/
ā ā āā feature1_repository.dart
ā ā āā feature1.dart
ā āā presentation/
ā āā bloc/
ā ā āā feature1_edit_bloc.dart
ā ā āā feature1_edit_event.dart
ā ā āā feature1_edit_state.dart
ā ā āā feature1_read_bloc.dart
ā ā āā feature1_read_event.dart
ā ā āā feature1_read_state.dart
ā āā views/
ā āā feature1_edit_view.dart
ā āā feature1_read_view.dart
āā shared/
āā presentation/
āā list_table.dart
Files from the scaffolding_test and scaffolding_main that are included for the options --generate-tests true --generate-home true
When --generate-test
true
test/
āā features/
āā feature1/
āā data/
ā āā feature1_model_test.dart
ā āā feature1_repository_impl_test.dart
āā domain/
ā āā feature1_test.dart
āā presentation/
āā bloc/
ā āā feature1_edit_bloc_test.dart
ā āā feature1_read_bloc_test.dart
āā views/
āā feature1_edit_view_test.dart
āā feature1_read_view_test.dart
When --generate-home
true
lib/
āā main.dart
āā scaffold_app.dart
When both --generate-home true
and --generate-test true
test/
āā scaffold_app_test.dart