一,内存爆表 > 图片缓存
/// State基类
class BaseState extends State
with
AutomaticKeepAliveClientMixin,
WidgetHelper,
DialogHelper,
EventListener {
@mustCallSuper
void initState() {
if (isListenEvent()) {
EventManager.instance.listen(this);
}
//指定图片缓存大小,默认是1000
PaintingBinding.instance.imageCache.maximumSize = 10;
super.initState();
LogEvent.onPageStart('${this}');
}
@mustCallSuper
void dispose() {
super.dispose();
deactivate();
// 清理缓存
PaintingBinding.instance.imageCache.clear();
LogEvent.onPageEnd('${this}');
if (isListenEvent()) {
EventManager.instance.cancel(this);
}
}
二,iOS 字体缩小/消失/挤出屏幕等问题
import 'dart:math' as math;
import 'package:flutter/material.dart';
class NoScaleTextWidget extends StatelessWidget {
final Widget child;
const NoScaleTextWidget({
Key key,
@required this.child,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaxScaleTextWidget(
max: 1.0,
child: child,
);
}
}
class MaxScaleTextWidget extends StatelessWidget {
final double max;
final Widget child;
const MaxScaleTextWidget({
Key key,
this.max = 1.2,
@required this.child,
}) : super(key: key);
@override
Widget build(BuildContext context) {
var data = MediaQuery.of(context);
var scale = math.min(max, data.textScaleFactor);
return MediaQuery(
data: data.copyWith(textScaleFactor: scale),
child: child,
);
}
}
class ScaleTextWidget extends StatelessWidget {
final double scale;
final Widget child;
const ScaleTextWidget({
Key key,
@required this.scale,
@required this.child,
}) : super(key: key);
@override
Widget build(BuildContext context) {
var data = MediaQuery.of(context);
var scale = this.scale ?? data.textScaleFactor;
return MediaQuery(
data: data.copyWith(textScaleFactor: scale),
child: child,
);
}
}
@override
Widget build(BuildContext context) {
loadData();
return MaterialApp(
builder: (ctx, w) {
return MaxScaleTextWidget(
max: 1.0,
child: w,
);
},
theme: ThemeData(
primarySwatch: Colors.blue,
textTheme: TextTheme(
title: TextStyles.titleStyle, button: TextStyles.titleStyle),
),
home: homePage,
三,未授权导致图片保存失败
//每次都提示图片已保存成功,相册里并没有图片
void _onSaveImage() async {
print("_onImageSaveButtonPressed");
final QrPainter painter = QrPainter(
data: _appDownloadLink,
color: const Color(0xff222222),
emptyColor: const Color(0xffffffff),
version: 4,
gapless: true,
);
final ByteData imageData = await painter.toImageData(300.0);
final result = await ImageGallerySaver.save(imageData.buffer.asUint8List());
if (result) {
toast("二维码已成功保存到手机相册");
} else {
toast("二维码保存到手机相册失败, 请查看App权限");
}
}
@override
void initState() {
super.initState();
// 先要授权访问相册
PermissionHandler().requestPermissions([
PermissionGroup.storage, // 在这里添加需要的权限
]);
}