ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Flutter版 WanAndroid App,flutter蓝牙接收数据

2021-12-19 17:06:12  阅读:228  来源: 互联网

标签:const String App value ScreenUtil static context 接收数据 WanAndroid


api

class WanAndroidApi {

/// 首页banner http://www.wanandroid.com/banner/json

static 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”; //退出

// 拼接url

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’);

}

return sb.toString();

}

}

请求与返回实体类 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(’}’);

return sb.toString();

}

}

接口请求&解析 repository

class WanRepository {

Future<List> getBanner() async {

BaseResp baseResp = await DioUtil().request(

Method.get, WanAndroidApi.getPath(path: WanAndroidApi.BANNER));

List 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();

}

return bannerList;

}

}

资源文件 resources


  • |–resources
*   |-- colors.dart
*   |-- dimens.dart
*   |-- strings.dart
*   |-- styles.dart

colors.dart

class ColorT {

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);

}

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;

}

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: ‘主页’,

},

‘HK’: {

Ids.titleHome: ‘主頁’,

},

‘TW’: {

Ids.titleHome: ‘主頁’,

}

}

};

styles.dart

class TextStyles {

static TextStyle listTitle = TextStyle(

fontSize: Dimens.font_sp16,

color: ColorT.text_dark,

fontWeight: FontWeight.bold,

);

static TextStyle listContent = TextStyle(

fontSize: Dimens.font_sp14,

color: ColorT.text_normal,

);

static TextStyle listExtra = TextStyle(

fontSize: Dimens.font_sp12,

color: ColorT.text_gray,

);

}

Flutter 国际化相关

fluintl 是一个为应用提供国际化的库,可快速集成实现应用多语言。该库封装了一个国际化支持类,通过提供统一方法getString(id)获取字符串。

// 在MyApp initState配置多语言资源

setLocalizedValues(localizedValues); //配置多语言资源

// 在MaterialApp指定localizationsDelegates和supportedLocales

MaterialApp(

home: MyHomePage(),

localizationsDelegates: [

GlobalMaterialLocalizations.delegate,

GlobalWidgetsLocalizations.delegate,

CustomLocalizations.delegate //设置本地化代理

],

supportedLocales: CustomLocalizations.supportedLocales,//设置支持本地化语言集合

);

// 字符串获取

IntlUtil.getString(context, Ids.titleHome);

CustomLocalizations.of(context).getString(StringIds.titleHome);

Flutter 屏幕适配 ScreenUtil

方案一、不依赖context

步骤 1

//如果设计稿尺寸默认配置一致,无需该设置。 配置设计稿尺寸 默认 360.0 / 640.0 / 3.0

setDesignWHD(_designW,_designH,_designD);

步骤 2

// 在MainPageState build 调用MediaQuery.of(context)

class MainPageState extends State {

@override

Widget build(BuildContext context) {

// 在 MainPageState build 调用 MediaQuery.of(context)

MediaQuery.of(context);

return new Scaffold(

appBar: new AppBar(),

);

}

}

步骤 3

ScreenUtil.getInstance().screenWidth

ScreenUtil.getInstance().screenHeight

//屏幕适配相关

ScreenUtil.getInstance().getWidth(size); //返回根据屏幕宽适配后尺寸(单位 dp or pt)

ScreenUtil.getInstance().getHeight(size); //返回根据屏幕高适配后尺寸 (单位 dp or pt)

ScreenUtil.getInstance().getWidthPx(sizePx); //sizePx 单位px

ScreenUtil.getInstance().getHeightPx(sizePx); //sizePx 单位px

ScreenUtil.getInstance().getSp(fontSize); //返回根据屏幕宽适配后字体尺寸

方案二、依赖context

//如果设计稿尺寸默认配置一致,无需该设置。 配置设计稿尺寸 默认 360.0 / 640.0 / 3.0

setDesignWHD(_designW,_designH,_designD);

ScreenUtil.getScreenW(context); //屏幕 宽

ScreenUtil.getScreenH(context); //屏幕 高

//屏幕适配相关

ScreenUtil.getScaleW(context, size); //返回根据屏幕宽适配后尺寸(单位 dp or pt)

ScreenUtil.getScaleH(context, size); //返回根据屏幕高适配后尺寸 (单位 dp or pt)

ScreenUtil.getScaleSp(context, size) ;//返回根据屏幕宽适配后字体尺寸

Flutter 数据存储 SpUtil

SpUtil : 单例"同步" SharedPreferences 工具类。

项目中为大家提供SpHelper,方便存取实体对象类。

// 存储SplashModel实体对象

SplashModel model = new SplashModel();

SpHelper.putObject(Constant.KEY_SPLASH_MODEL, model);

// 获取SplashModel实体对象

SplashModel model = SpHelper.getSplashModel();

class SpHelper {

// 存储Obj,T 用于区分存储类型

static void putObject(String key, Object value) {

switch (T) {

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 (ObjectU

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

til.isNotEmpty(_splashModel)) {

Map userMap = json.decode(_splashModel);

return SplashModel.fromJson(userMap);

标签:const,String,App,value,ScreenUtil,static,context,接收数据,WanAndroid
来源: https://blog.csdn.net/flutter001/article/details/122026267

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有