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

  }
相关推荐
迷雾漫步者2 小时前
Flutter组件————FloatingActionButton
前端·flutter·dart
coder_pig7 小时前
📝小记:Ubuntu 部署 Jenkins 打包 Flutter APK
flutter·ubuntu·jenkins
捡芝麻丢西瓜9 小时前
flutter自学笔记5- dart 编码规范
flutter·dart
恋猫de小郭9 小时前
什么?Flutter 可能会被 SwiftUI/ArkUI 化?全新的 Flutter Roadmap
flutter·ios·swiftui
sunly_1 天前
Flutter:导航,tab切换,顶部固定,列表分页滚动
开发语言·javascript·flutter
敲代码的小强2 天前
Flutter项目兼容鸿蒙Next系统
flutter·华为·harmonyos
Zh-jie2 天前
flutter 快速实现侧边栏
前端·javascript·flutter
truemi.733 天前
flutter --no-color pub get 超时解决方法
android·flutter
王家视频教程图书馆3 天前
flutter 使用dio 请求go语言后台数据接口展示瀑布流图片
flutter
迷雾漫步者3 天前
Flutter组件————AppBar
flutter·跨平台·dart