This project is all open source. Welcome Star&Fork.
Projects include launch page, lead page, theme color, Internationalization, Bloc, RxDart. Have a good project structure, more standard code. The App has a polished UI, unified interaction, slide-out, lists and Web interfaces that offer quick scrolling to the top. The purpose of the author is to provide a more formal example of the Flutter project. For the latest developments of the project, you can pay attention to the information of the first Hot Item in the App.
App directory structure
- |–lib
- | – blocs (bloc)
- | – common (commonly used class, such as Constant Constant)
- | – data (network data layer)
- | – db (database)
- | – event (event)
- | – models (entity)
- | – res (dimens resource file, string, colors, styles)
- | – UI (interface related page, dialog, widgets)
- | – utils (tools)
Data Indicates the network data layer
- |–data
- | – API (url field)
- | – net (single DioUtil)
- | – protocol (request and returns the entity class)
- | – repository (interface request & analysis)
api
Class WanAndroidApi {/ / / static homepage banner http://www.wanandroid.com/banner/json const String banner ="banner";
static const String USER_REGISTER = "user/register"; Static const String USER_LOGIN ="user/login"; Static const String USER_LOGOUT ="user/logout"; Static String getPath({String path:' ', int page, String resType: 'json'}) {
StringBuffer sb = new StringBuffer(path);
if(page ! = null) { sb.write('/$page');
}
if(resType ! = null && resType.isNotEmpty) { sb.write('/$resType');
}
returnsb.toString(); }}Copy the code
Request and return entity class Protocol
class LoginReq {
String username;
String password;
LoginReq(this.username, this.password);
LoginReq.fromJson(Map<String, dynamic> json)
: username = json['username'],
password = json['password'];
Map<String, dynamic> toJson() => {
'username': username,
'password': password,
};
@override
String toString() {
StringBuffer sb = new StringBuffer('{');
sb.write("\"username\":\"$username\ "");
sb.write(",\"password\":$password");
sb.write('} ');
returnsb.toString(); }}Copy the code
Interface request & parse repository
class WanRepository {
Future<List<BannerModel>> getBanner() async {
BaseResp<List> baseResp = await DioUtil().request<List>(
Method.get, WanAndroidApi.getPath(path: WanAndroidApi.BANNER));
List<BannerModel> bannerList;
if(baseResp.code ! = Constant.STATUS_SUCCESS) {return new Future.error(baseResp.msg);
}
if(baseResp.data ! = null) { bannerList = baseResp.data.map((value) {return BannerModel.fromJson(value);
}).toList();
}
returnbannerList; }}Copy the code
Resource file RES
- |–res
- |– colors.dart
- |– dimens.dart
- |– strings.dart
- |– styles.dart
colors.dart
class Colours {
static const Color app_main = Color(0xFF666666);
static const Color text_dark = Color(0xFF333333);
static const Color text_normal = Color(0xFF666666);
static const Color text_gray = Color(0xFF999999);
}
Copy the code
dimens.dart
class Dimens {
static const double font_sp12 = 12;
static const double font_sp14 = 14;
static const double font_sp16 = 16;
static const double gap_dp5 = 5;
static const double gap_dp10 = 10;
}
Copy the code
strings.dart
class Ids {
static const String titleHome = 'title_home';
}
Map<String, Map<String, Map<String, String>>> localizedValues = {
'en': {
'US': {
Ids.titleHome: 'Home',}},'zh': {
'CN': {
Ids.titleHome: 'home',},'HK': {
Ids.titleHome: 'home',},'TW': {
Ids.titleHome: 'home'}}};Copy the code
styles.dart
class TextStyles { static TextStyle listTitle = TextStyle( fontSize: Dimens.font_sp16, color: Colours.text_dark, fontWeight: FontWeight.bold, ); static TextStyle listContent = TextStyle( fontSize: Dimens.font_sp14, color: Colours.text_normal, ); static TextStyle listExtra = TextStyle( fontSize: Dimens.font_sp12, color: Colours.text_gray, ); } // The Gaps {// Horizontal Gaps static Widget hGap5 = new SizedBox(width: dimens.gap_dp5); static Widget hGap10 = new SizedBox(width: Dimens.gap_dp10); Static Widget vGap5 = new SizedBox(height: dimens.gap_dp5); static Widget vGap10 = new SizedBox(height: Dimens.gap_dp10); }Copy the code
Flutter internationalization correlation
Fluintl is an internationalized library for applications, which can be quickly integrated into multiple languages. The library encapsulates an internationalization support class that gets strings by providing a uniform method getString(ID).
// Configure multilingual resources in MyApp initStatesetLocalizedValues(localizedValues); // localizationsDelegates and supportedLocales MaterialApp(home: MyHomePage(), localizationsDelegates: [GlobalMaterialLocalizations. Delegate, GlobalWidgetsLocalizations. Delegate, CustomLocalizations. Delegate/agent/set localization]. SupportedLocales: CustomLocalizations supportedLocales, / / set support localization language collection); Intlutil. getString(context, ids.titleHome); CustomLocalizations.of(context).getString(StringIds.titleHome);Copy the code
Flutter screen adaptationScreenUtil
Option one, do not rely on context
Step 1 // If the dimensions of the design document are the same by default, you do not need to set this parameter. The default configuration design size is 360.0/640.0/3.0setDesignWHD(_designW,_designH,_designD); Step 2 // Call mediaQuery.of (context) class MainPageState extends State<MainPage> {@override Widget Build (BuildContext Context) {// Call mediaQuery.of (context) mediaQuery.of (context);returnnew Scaffold( appBar: new AppBar(), ); }} Step 3 ScreenUtil.getInstance().screenWidth screenutil.getInstance ().screenheight // Screen adaptation ScreenUtil.getInstance().getWidth(size); // ScreenUtil.getInstance().getheight (size); // ScreenUtil.getInstance().getwidThpx (sizePx); // ScreenUtil.getInstance(). //sizePx unit px screenUtil.getInstance ().getheightpx (sizePx); //sizePx unit px screenutil.getInstance ().getsp (fontSize); // Returns the font size adapted to the screen widthCopy the code
Option two, rely on context
// If the size of the design document is the same by default, you do not need to set this parameter. The default configuration design size is 360.0/640.0/3.0setDesignWHD(_designW,_designH,_designD); ScreenUtil.getScreenW(context); // ScreenUtil. GetScreenH (context); // Screen height // Screen adaptation related screenUtil.getScalew (context, size); Screenutil. getScaleH(context, size); // Screenutil. getScaleH(context, size); // ScreenUtil.getScaleSp(context, size); // ScreenUtil.getScaleSp(context, size); // Returns the font size adapted to the screen widthCopy the code
Flutter data storageSpUtil
SpUtil: singleton “synchronize” SharedPreferences utility class. The project provides SpHelper for easy access to entity object classes.
SplashModel = new SplashModel(); SpHelper.putObject(Constant.KEY_SPLASH_MODEL, model); SplashModel = sphelper.getSplashModel (); Static void putObject<T>(String key, Object value) {switch (T) {static void putObject<T>(String key, Object value) {case int:
SpUtil.putInt(key, value);
break;
case double:
SpUtil.putDouble(key, value);
break;
case bool:
SpUtil.putBool(key, value);
break;
case String:
SpUtil.putString(key, value);
break;
case List:
SpUtil.putStringList(key, value);
break;
default:
SpUtil.putString(key, value == null ? "" : json.encode(value));
break;
}
}
static SplashModel getSplashModel() {
String _splashModel = SpUtil.getString(Constant.KEY_SPLASH_MODEL);
if (ObjectUtil.isNotEmpty(_splashModel)) {
Map userMap = json.decode(_splashModel);
return SplashModel.fromJson(userMap);
}
returnnull; }}Copy the code