Flutter 项目开发中遇到的问题

358 阅读2分钟

布局问题

Column 里面不能放 ListView 问题

Column(
  children: [
    _buildContentTop(),
    Expanded(child: _buildContentList()), // 在ListView外面套一个Expanded
  ],
),

Column 高度根据子视图自适应


 Column(
    mainAxisSize: MainAxisSize.min, // Column 高度自适应
    children: [
      _buildTitle(),
      _buildFoot(),
    ],
  )

去除ListView头部空白

body: MediaQuery.removePadding(
  context: context, 
  removeTop: true,
  child: ListView(
    children: [
      _getBannerCell(), 
      _getTitleCell(), 
      ...this.dataArray.map((e) {
        return _getListCell(e);
      }).toList()
    ],
  )
),

ListView 不满屏无法滚动问题

// listView 添加属性
physics: const AlwaysScrollableScrollPhysics(),

其他问题

延迟加载

 EasyLoading.show(status: 'loading', maskType: EasyLoadingMaskType.clear);
    Future.delayed(Duration(milliseconds: (1 * 1000).round()), () {
      EasyLoading.dismiss();
      _showUpdateAlert();
    });

优化setState 局部刷新

// 1、定义 GlobalKey
GlobalKey<TextWidgetState> textKey = GlobalKey(); 

// 2、自定义 需要局部刷新的控件
// 封装的文本组件Widget
class TextWidget extends StatefulWidget {
  final Key key;

  // 接收一个Key
  TextWidget(this.key);
  
  @override
  State<StatefulWidget> createState() => TextWidgetState();
}

class TextWidgetState extends State<TextWidget> {
  String _text = "0";

  @override
  Widget build(BuildContext context) {
    return new Text(
      _text,
      style: TextStyle(
          fontSize: 14,
          color: ColorUtils.hexColor(0x939699),
          fontWeight: FontWeight.w500
      ),
    );
  }
  void onPressed(int count, int total) {
    setState(() => _text = "${this._getSaveMString(count)}/${this._getSaveMString(total)}");
  }
  String _getSaveMString(int count) {
    return (count.toDouble() / 1024 / 1024).toStringAsFixed(2) + "M";
  }
}

// 3、引用控件
TextWidget(textKey)

// 4、调用 onPressed 更新自定义控件
textKey.currentState!.onPressed(widget.coount, widget.total); // 更新文字

定时器

time = Timer.periodic(Duration(milliseconds: (0.2 * 1000).round()), (timer) { 
  eventBus.fire(Event_NewVersionInDown);
});

使用dio下载

    Dio dio = Dio();

    //设置连接超时时间
    dio.options.connectTimeout = 10000;

    //设置数据接收超时时间
    dio.options.receiveTimeout = 10000;

    Response response = await dio.download(
        "****",
        "./temp/update.zip", onReceiveProgress: (int count, int total) {
      
      // 下载完成
      if (count == total && this.haveShowProgress) {
        this.progressView.haveDown();
        Navigator.pop(context);
        this.haveShowProgress = false;
        return;
      }

      this.progressView.coount = count;
      this.progressView.total = total;

      // 开始下载
      if (count > 0 && total > 0 && this.haveShowProgress == false) {
        this.haveShowProgress = true;
        this._showProgressView();
        this.progressView.beginDown();
        EasyLoading.dismiss();
      }

      this.progressView.update();
    });

项目找不到头文件

  • 刚 clone 下来的找不到头文件
importpackage:flutter/material.dart‘;
// 错误里写的是不存在,上网搜索了一下解决方法:
// 在项目的目录打开终端,输入 flutter packages get ,

Error running pod install

  1. Automatically assigning platform iOS with version 8.0 on target Runner because no platform was specified. Please specify a platform for this target

    解决:只要修改ios目录下podfile 第二行 去掉#号

\flutter\bin\cache\dart-sdk\bin\dart.exe"' 不是内部或外部命令

删除flutter\bin\cache 文件夹,重新执行flutter docator 即可

block 回调

typedef void MassInputClickSureCallBack(String inputText);
​
final MassInputClickSureCallBack _callClick;
JCMassTextInputView(this._callClick);
  
this.callback(_contentController.text);

解决输入框添加文字光标不在最后面问题

_contentController.text = _contentController.text + '[${expression.name}]';
_contentController.selection = TextSelection.fromPosition(TextPosition(offset: _contentController.text.length));

Row 里面套 TextFeild

会报错的。 因为textField 的大小需要依靠 Row 解决办法在 textfield 外面加一个 expanded。

如果 外层有 多个row 。 那就多加几个 expanded

字符串判断

str ?? "" // ?? 可以判断 str 是不是为 null

str.isEmpty 并不能判断 str 是不是为null。 如果为null 这个判断会出问题