项目中常用插件
屏幕适配
get install flutter_screenutil
轮播图
get install flutter_swiper_view
心得
Swiper(
pagination: const SwiperPagination(
alignment: Alignment.bottomRight, // dot选择器所在位置
builder: RectSwiperPaginationBuilder(
color: Colors.green, // dot的颜色
activeColor: Colors.red, // dot选中的颜色
),
),
itemCount: controller.swipers.length, // 轮播图长度
autoplay: true, // 自动播放
loop: true, // 循环播放
itemBuilder: (BuildContext context, index) {
String picUrl = "https://xxx.xxx.com/${controller.swipers[index].pic}"; // 图片地址
return FadeInImage.assetNetwork(
placeholder: 'assets/images/x.png', // 图片占位图 如果网络原因图片加载慢就用这个
image: picUrl.replaceAll("\\", "/"), // 图片
fit: BoxFit.fill, // 图片展示的模式 fill 代表填充 还有其他的
imageErrorBuilder: (ctx, err, stackTrace) => Image.asset( // 如果网络原因或者资源服务器不存在图片就展示默认图
'assets/images/x.png', //默认显示图片
height: 250, // 按要求写
width: double.infinity,
),
);
},
),
网络请求库
get install dio
瀑布流布局
get install flutter_staggered_grid_view
命令
创建 套件 的命令
get create page:mine
// 就会生成 controller view binding 文件
创建 model 的命令
get generate model on models from "https://xxx.com/api/focus"
// 输入 model 的名字即可 例如:接口地址叫 focus 那就输入 focus
自定义的静态资源
flutter:
uses-material-design: true
fonts:
- family: MyIcon #指定一个字体名 根据自己的需求定义
fonts:
- asset: assets/fonts/iconfont.ttf
assets:
- assets/images/xiaomiBanner.png
- assets/images/2.0x/xiaomiBanner.png
- assets/images/3.0x/xiaomiBanner.png
- assets/images/xiaomiBanner2.png
- assets/images/2.0x/xiaomiBanner2.png
- assets/images/3.0x/xiaomiBanner2.png
自己封装的组件
keep_alive_wrapper.dart
import 'package:flutter/material.dart';
class KeepAliveWrapper extends StatefulWidget {
const KeepAliveWrapper(
{Key? key, @required this.child, this.keepAlive = true})
: super(key: key);
final Widget? child;
final bool keepAlive;
@override
State<KeepAliveWrapper> createState() => _KeepAliveWrapperState();
}
class _KeepAliveWrapperState extends State<KeepAliveWrapper>
with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
super.build(context);
return widget.child!;
}
@override
bool get wantKeepAlive => widget.keepAlive;
}
screen_adapter.dart
import 'package:flutter_screenutil/flutter_screenutil.dart';
class ScreenAdapter{
static width(num v){
return v.w;
}
static height(num v){
return v.h;
}
static fontSize(num v){
return v.sp;
}
static getScreenWidth(){
// return ScreenUtil().screenWidth;
return 1.sw;
}
static getScreenHeight (){
// return ScreenUtil().screenHeight;
return 1.sh;
}
}
AndroidManifest.xml
用到的权限
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:label="lesson5"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!--读取手机状态-->
<uses-permission android:name="android.permission.INTERNET" /> <!--允许程序打开网络套接字(Allows applications to open network sockets)-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!--是获取网络状态的权限控制,如果获取当前GSM网络相关信息必需在androidmanifest.xml中声明android.permission.ACCESS_NETWORK_STATE这句。-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!--允许程序访问Wi-Fi网络状态信息(Allows applications to access information about Wi-Fi networks)-->
<uses-permission android:name="android.permission.BATTERY_STATS" /><!--BATTERY_STATS是获取Android平台上电池设备的权限令牌,必需声明android.permission.BATTERY_STATS才可以获得电池信息-->
<uses-permission android:name="android.permission.ADD_SYSTEM_SERVICE" /><!--是系统服务数据库的管理权限,比如添加一个系统服务必需声明android.permission.ADD_SYSTEM_SERVICE-->
<uses-permission android:name="android.permission.BLUETOOTH" /><!--BLUETOOTH蓝牙信息类,获取相关的蓝牙信息必声明android.permission.BLUETOOTH-->
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /><!--BLUETOOTH_ADMIN是蓝牙管理权限包含了身份安全认证,必需添加android.permission.BLUETOOTH_ADMIN类-->
<uses-permission android:name="android.permission.CAMERA" /> <!--CAMERA是摄像头权限控制,可以管理照相功能的启用 "android.permission.CAMERA"-->
</manifest>
生成 .jks 文件
keytool -genkeypair -alias 123456 -keypass 123456 -keyalg RSA -keysize 1024 -validity 365 -keystore ace.jks