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, // 在这里添加需要的权限
    ]);

  }
相关推荐
️ 邪神17 小时前
【Android、IOS、Flutter、鸿蒙、ReactNative 】文本点击事件
flutter·ios·鸿蒙·reactnative·anroid
️ 邪神18 小时前
【Android、IOS、Flutter、鸿蒙、ReactNative 】文本Text显示
flutter·ios·鸿蒙·reactnative·anroid
iFlyCai19 小时前
Flutter中有趣的级联语法
flutter
恋猫de小郭19 小时前
Flutter 小技巧之 Shader 实现酷炫的粒子动画
flutter
hello world smile1 天前
Dart中List API用法大全
flutter·list·dart
lqj_本人1 天前
Flutter&鸿蒙next 使用 BLoC 模式进行状态管理详解
flutter·华为·harmonyos
Miketutu1 天前
flutter 项目初建碰到的控制台报错无法启动问题
flutter
lqj_本人1 天前
flutter&鸿蒙next 使用 InheritedWidget 实现跨 Widget 传递状态
flutter·华为·harmonyos
氤氲息1 天前
flutter 发版的时候设置版本号
flutter
潘敬2 天前
flutter 语法糖库 flutter_magic 发布 1.0.1
开发语言·前端·javascript·flutter·typescript