flutter-app boilerplate
this repo is a boilerplate to create flutter application easily. it is based on getx. more info about getx here. the app has been setup to work with retrofit, dio, json_annotation, intl_utils and shimmer
getting started
- install flutter sdk. require flutter 2.0
- install plugins in android studio
- download the repo.
- run
flutter pub get
- run
flutter pub run intl_utils:generate
- run
flutter pub run build_runner build --delete-conflicting-outputs
- run app.
file structure
assets
└───font
└───image
└───2.0x
└───3.0x
libs
└───common
│ └───app_colors.dart
│ └───app_dimens.dart
│ └───app_images.dart
│ └───app_shadows.dart
│ └───app_text_styles.dart
│ └───app_themes.dart
└───configs
│ └───app_configs.dart
└───database
│ └───secure_storage_helper.dart
│ └───shared_preferences_helper.dart
│ └─── ...
└───l10n
└───models
│ └───entities
│ │ └───user_entity.dart
│ │ └─── ...
│ └───enums
│ │ └───load_status.dart
│ │ └─── ...
│ └───params
│ │ └───sign_up_param.dart
│ │ └─── ...
│ └───response
│ └───array_response.dart
│ └───object_response.dart
└───networks
│ └───api_client.dart
│ └───api_interceptors.dart
│ └───api_util.dart
└───router
│ └───route_config.dart
└───services
│ └───api
│ └───store
│ └───auth_service.dart
│ └───cache_service.dart
│ └───setting_service.dart
└───ui
│ └───commons
│ │ └───app_bottom_sheet.dart
│ │ └───app_dialog.dart
│ │ └───app_snackbar.dart
│ │ └───...
│ └───pages
│ │ └───splash
│ │ │ └───splash_logic.dart
│ │ │ └───splash_state.dart
│ │ │ └───splash_view.dart
│ │ └───...
│ └───widget
│ └───appbar
│ └───buttons
│ │ └───app_button.dart
│ │ └───app_icon_button.dart
│ │ └───...
│ └───images
│ │ └───app_cache_image.dart
│ │ └───app_circle_avatar.dart
│ └───textfields
│ └───shimmer
│ └───...
└───utils
│ └───date_utils.dart
│ └───file_utils.dart
│ └───logger.dart
│ └───utils.dart
└───main.dart
main.dart
the “entry point” of program.
in general, main.dart
contain appmaterial, but this repo use getmaterialapp whichs has the default materialapp as a child.
assets
this folder is to store static assests like fonts and images.
common
configs
this folder hold the config of your applications.
database
l10n
this folder contain all localized string. see more
models
networks
router
this folder contain the route navigation
services
this folder contain all getxservice or any service which can not be removed from memory.
ui
utils
how to use
creating a screen.
all screen should be created in the ui/pages
folder
user the getx plugin to create new screen.
example: moviesection
logic: movies_section_logic.dart
class moviessectionlogic extends getxcontroller {
final state = moviessectionstate();
final apiservice = get.find<apiservice>();
void fetchinitialmovies() async {
state.loadmoviestatus.value = loadstatus.loading;
try {
final result = await apiservice.getmovies(page: 1);
state.loadmoviestatus.value = loadstatus.success;
state.movies.value = result.results;
state.page.value = result.page;
state.totalpages.value = result.totalpages;
} catch (e) {
state.loadmoviestatus.value = loadstatus.failure;
}
}
...
}
state: movies_section_state.dart
class moviessectionstate {
final loadmoviestatus = loadstatus.initial.obs;
final movies = <movieentity>[].obs;
final page = 1.obs;
final totalresults = 0.obs;
final totalpages = 0.obs;
...
}
view: movies_section_view.dart
class moviessectionpage extends statefulwidget {...}
class _moviessectionpagestate extends state<moviessectionpage> {
final moviessectionlogic logic = get.put(moviessectionlogic());
final moviessectionstate state = get.find<moviessectionlogic>().state;
@override
widget build(buildcontext context) {
return obx(() {
if (state.loadmoviestatus.value == loadstatus.loading) {
return _buildloadinglist();
} else if (state.loadmoviestatus.value == loadstatus.failure) {
return container();
} else {
return _buildsuccesslist(
state.movies,
showloadingmore: !state.hasreachedmax,
);
}
});
}
}
creating api service.
- create entity object in folder
lib/models/entities
ex:movie_entity.dart
import 'package:json_annotation/json_annotation.dart';
part 'movie_entity.g.dart';
@jsonserializable()
class movieentity {
@jsonkey()
string? title;
...
factory movieentity.fromjson(map<string, dynamic> json) => _$movieentityfromjson(json);
map<string, dynamic> tojson() => _$movieentitytojson(this);
}
class must have @jsonserializable()
for generator. read json_serializable
- define and generate your api in file
lib/networks/api_client.dart
ex: get movies
/// movie
@get("/3/discover/movie")
future<arrayresponse<movieentity>> getmovies(@query('api_key') string apikey, @query('page') int page);
note: using arrayresponse and objectresponse for generic response
- require run command line:
flutter pub run build_runner build --delete-conflicting-outputs
- create api service file for your feature in folder
lib/services/api
ex:movies_api.dart
part of 'api_service.dart';
extension movieapiservice on apiservice {
future<arrayresponse<movieentity>> getmovies({int page = 1}) async {
return _apiclient.getmovies(movieapiconfig.apikey, page);
}
}
after, add part 'auth_api.dart';
to services/api/api_service
- you can call api in the logic of screen.
ex:
final apiservice = get.find<apiservice>();
final result = await apiservice.getmovies(page: 1);
support multiple theme and language
see settingservice class for more detail
other
logger
logger.d("message"); //"�� debug: message"
logger.i("message"); //"�� info: message"
logger.e("message"); //"❤️ error: message"
logger.log("very very very long message");
snackbar
appsnackbar.showinfo(message: 'info');
appsnackbar.showwarning(message: 'warning');
appsnackbar.showerror(message: 'error');
dialog
appdialog.defaultdialog(
message: "an error happened. please check your connection!",
textconfirm: "retry",
onconfirm: () {
//do something
},
);
button ui when call api
return obx(() {
return container(
padding: edgeinsets.symmetric(horizontal: 20),
child: apptintbutton(
title: 'sign in',
onpressed: _signin,
isloading: state.signinstatus.value == loadstatus.loading,
),
);
});
Comments are closed.