Flutter 填坑录 (不定时更新)

一,内存爆表 > 图片缓存

java 复制代码
/// State基类
class BaseState<T extends StatefulWidget> extends State<T>
    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 字体缩小/消失/挤出屏幕等问题

java 复制代码
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,
    );
  }
}
java 复制代码
  @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,

三,未授权导致图片保存失败

java 复制代码
//每次都提示图片已保存成功,相册里并没有图片
  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权限");
    }
  }
java 复制代码
  @override
  void initState() {
    super.initState();
// 先要授权访问相册
    PermissionHandler().requestPermissions(<PermissionGroup>[
      PermissionGroup.storage, // 在这里添加需要的权限
    ]);

  }
相关推荐
张风捷特烈12 小时前
Flutter 伪3D绘制#03 | 轴测投影原理分析
android·flutter·canvas
马拉萨的春天15 小时前
flutter 项目结构目录以及pubspec.ymal等文件描述
flutter
bst@微胖子1 天前
Flutter项目之登录注册功能实现
开发语言·javascript·flutter
小墙程序员1 天前
Flutter 教程(十一)多语言支持
flutter
无知的前端1 天前
Flutter 一文精通Isolate,使用场景以及示例
android·flutter·性能优化
yidahis1 天前
Flutter 运行新建项目也报错?
flutter·trae
木马不在转1 天前
Flutter-权限permission_handler插件配置
flutter
江上清风山间明月2 天前
一周掌握Flutter开发--9. 与原生交互(下)
flutter·交互·原生·methodchannel
GeniuswongAir2 天前
Flutter极速接入IM聊天功能并支持鸿蒙
flutter·华为·harmonyos
sayen2 天前
记录 flutter 文本内容展示过长优化
前端·flutter