Introduction
A command-line tool that simplifies auto-code generation. Allowing you to generate a feature, widget, model, and API call code for your Flutter applications.
Features
- Feature code generation (Basic structure with classes)
- Widget code generation
- Model code generation
- GET method API call code generation
- POST method API call code generation
- Multipart API call code generation
- Assets image generation from 3x to 1x and 2x
Flutter Compatibility
Package version | Dart version | Flutter version | Boilerplate version |
---|---|---|---|
0.0.2 - 1.3.1 | 3.2.6 | 3.16.0 - 3.19.3 | 3.16.9+1 |
Getting Started
To integrate the package into your Flutter project, follow these steps:
Add Dependency
Add the following dependency to your pubspec.yaml file:
dependencies:
jt_flutter_cli: ^1.3.1
Then run:
flutter pub get
Rules
Common Rules:
- All the files must contain only one class
- Wherever you use Name or any keyword it must be unique
API Rules:
- The feature must be created using the CLI tool
- The developer must not change any file name of the feature
- The API name must be in lowercase with an underscore (Valid Ex: get_profile, profile)
Multipart API Rules:
- All file parameters keys in request json must be postfix with _file. eg (xyzAbc_file="")
- File parameters value must be empty string. eg (xyzAbc_file="")
- All list of files parameters keys in request json must be postfix with _fileList. eg (xyzAbc_fileList="")
- List of files parameters value must be empty string. eg (xyzAbc_fileList="")
Usage
Create new feature:
flutter pub run jt_flutter_cli create feature {name_of_feature}
In the above configuration, the package is creating a new feature that is available in the lib/feature/name_of_feature path.
It will generate the name_of_feature folder, which has the dart files and the domain, presentation, and data folders as shown below.
.
├── ...
├── name_of_feature
│ ├── data
│ │ ├── data_source
│ │ │ └── name_of_feature_data_source.dart
│ │ │ └── name_of_feature_data_source_impl.dart
│ │ ├── repository
│ │ │ └── name_of_feature_repo_impl.dart
│ │ │ └── mock_name_of_feature_repo_impl.dart
│ ├── domain
│ │ ├── repositories
│ │ │ └── name_of_feature_repo.dart
│ ├── presentation
│ │ ├── bloc
│ │ │ └── name_of_feature_bloc.dart
│ │ │ └── name_of_feature_data_event.dart
│ │ │ └── name_of_feature_data_state.dart
│ │ ├── screen
│ │ │ └── name_of_feature_screen.dart
│ │ └── name_of_feature_page.dart
└── ...
Create new widget:
flutter pub run jt_flutter_cli create widget {name_of_widget}
CLI Tool will prompt the user with the following question in terminal:
- Do you want to create a common widget or a feature widget? (common/feature)
- common: Widget will be created inside the folder lib/core/ui/widgets
- feature: It will prompt the user to enter the feature name, and the widget will be created inside the respective feature's folder at lib/feature/name_of_feature/presentation/screen/widgets
Create new model:
flutter pub run jt_flutter_cli create model {model_name} {feature_name} {json_file_path}
Add GET API call:
flutter pub run jt_flutter_cli add get-api-call {API_end_point} {API_name}
CLI Tool will prompt the user with the following questions for GET API call in terminal:
-
Enter feature name:
-
Does this API have request query parameters? (Y/N):
- Y : It will ask for json file path
- N : It will skip this question and move to next question
-
Enter request json file path:
-
Does this API have response body? (Y/N):
- Y : It will ask for json file path
- N : It will skip this question and move to next question
-
Enter response json file path:
-
Do you want to add shimmer to this API call? (Y/N):
- Y : It will generate the bloc state for showing shimmer UI in screen
- N : It will generate code to call base bloc event for showing progress indicator UI in dialog with no dismissible dialog
Add POST API call:
flutter pub run jt_flutter_cli add post-api-call {API_end_point} {API_name}
CLI Tool will prompt the user with the following questions for POST API call in terminal:
-
Enter feature name:
-
Does this API have request body? (Y/N):
- Y : It will ask for json file path
- N : It will skip this question and move to next question
-
Enter request json file path:
-
Does this API have response body? (Y/N):
- Y : It will ask for json file path
- N : It will skip this question and move to next question
-
Enter response json file path:
-
Do you want to add shimmer to this API call? (Y/N):
- Y : It will generate the bloc state for showing shimmer UI in screen
- N : It will generate code to call base bloc event for showing progress indicator UI in dialog with no dismissible dialog
Add MULTIPART API call:
flutter pub run jt_flutter_cli add multipart-api-call {API_end_point} {API_name}
Request JSON guide for file parameter:
- For file parameters define key with _file postfix in request json. eg (xyzAbc_file="")
- Set the file parameters value to empty string. eg (xyzAbc_file="")
- For list of files parameters define key with _fileList postfix in request json. eg (xyzAbc_fileList="")
- Set the List of files parameters value to empty string. eg (xyzAbc_fileList="")
CLI Tool will prompt the user with the following questions for MULTIPART API call in terminal:
-
Enter feature name:
-
Does this API have request body? (Y/N):
- Y : It will ask for json file path
- N : It will skip this question and move to next question
-
Enter request json file path:
-
Does this API have response body? (Y/N):
- Y : It will ask for json file path
- N : It will skip this question and move to next question
-
Enter response json file path:
-
Do you want to add shimmer to this API call? (Y/N):
- Y : It will generate the bloc state for showing shimmer UI in screen
- N : It will generate code to call base bloc event for showing progress indicator UI in dialog with no dismissible dialog
Assets image generation from 3x to 1x and 2x:
flutter pub run jt_flutter_cli create assets {3x image path} {directory name in assets/images/ (This is where image will be saved)} {image_const_name}
Follow below instruction to Auto add generated files to git
- Go to Settings->Version control->Confirmation and Select “Add silently” for “When files are created” and also check the “Apply to files created outside of Android Studio”
Existing project upgrade guide to utilize CLI tool in existing project
Below changes are required to utilize the CLI tool in the existing project
- Upgrade the Flutter version to
3.16.9
- Rename
MasterBloc
toApiBaseBloc
and move a file tocore/base/bloc/api_base_bloc/api_base_bloc.dart
- Rename
MasterApiEvents
toApiBaseBlocEvents
and move a file tocore/base/bloc/api_base_bloc/api_base_bloc_event.dart
- Rename
MasterApiState
toApiBaseBlocState
and move a file tocore/base/bloc/api_base_bloc/api_base_bloc_state.dart
- Move
/core/base/widget/base_bloc_widget.dart
to/core/base/screen/base_bloc_widget.dart
- Move
data_source
tocore/api
- Move
base_api_repo.dart
tocore/api/repository
- Move
/core/domain/commons/entities/api_response.dart
to/core/api/model/api_response.dart
- Move
/core/domain/commons/entities/no_request.dart
tocore/api/model/no_request.dart
- Move
/core/domain/commons/entities/success.dart
tocore/api/model/success.dart
- Move
/core/data/error/failure.dart
to/core/api/model/failure.dart
- Update
Failure
andServerFailure
classes fromfailure.dart
file with the latest boilerplateFailure
andServerFailure
classes fromfailure.dart
file - Move
/core/domain/commons/usecases/usecase.dart
to/core/api/base_usecases/base_usecase.dart
- Rename
UseCase
toBaseUseCase
- Create
/core/const/mock_api_constants.dart
file and move all const variable to this file from/core/api/data_source/mock/mock_data_impl.dart
- base_bloc change the name of
baseBlocObject
toapiBaseBlocObject
- Add
Failure
toErrorApiEvent
as a parameter and update the below lines with
apiBaseBlocObject.add(ErrorApiEvent(failure: failure));
baseBlocObject.failureModel = failure;
baseBlocObject.add(ErrorApiEvent());
- Remove
failureModel
parameter fromApiBaseBloc
and use data from event - Replace the
Success
class with theSuccess
class from the boilerplate - Move
/core/util/api_constant.dart
to/core/const/api_constants.dart
and update the file name api_constants - Copy
lib/core/api/model/file_upload_request_model.dart
file from boilerplate and Add the same file in your project at the same path - Replace
IFileMultiRequest
model withFileUploadRequestModel
in executeMultipart method fromremote_ds.dart
andremote_ds_impl.dart
file - Remove
IFileRequest
andIFileMultiRequest
classes fromrequest.dart
Note:
- Some features of CLI may require more changes than defined here depending on your project structure and version
Libraries
- command_executor
- core/code/code_handler
- core/const/app_const
- core/const/command_const
- core/const/string_const
- core/package/json_ast/error
- core/package/json_ast/json_ast
- core/package/json_ast/location
- core/package/json_ast/parse
- core/package/json_ast/parse_error_types
- core/package/json_ast/tokenize
- core/package/json_ast/tokenize_error_types
- core/package/json_ast/utils/substring
- core/path/path_handler
- core/util/model_util/helpers
- core/util/model_util/model_generator
- core/util/model_util/syntax
- core/util/util
- core/util/validator_util
- feature/add_api_call/add_api_call_util
- feature/add_api_call/api_call_cli
- feature/add_api_call/usecases/basic_structure_usecase
- feature/create_assets/asset_generation_util
- feature/create_assets/create_assets_cli
- feature/create_feature/basic_structure/basic_structure_page
- feature/create_feature/basic_structure/data/data_source/basic_structure_data_source
- feature/create_feature/basic_structure/data/data_source/basic_structure_data_source_impl
- feature/create_feature/basic_structure/data/repository/basic_structure_repo_impl
- feature/create_feature/basic_structure/data/repository/mock_basic_structure_repo_impl
- feature/create_feature/basic_structure/domain/repositories/basic_structure_repo
- feature/create_feature/basic_structure/presentation/bloc/basic_structure_bloc
- feature/create_feature/basic_structure/presentation/bloc/basic_structure_event
- feature/create_feature/basic_structure/presentation/bloc/basic_structure_state
- feature/create_feature/basic_structure/presentation/screen/basic_structure_screen
- feature/create_feature/create_feature_cli
- feature/create_model/create_model_cli
- feature/create_widget/basic_widget/basic_widget
- feature/create_widget/create_widget_cli