学习Flutter的第十三天
6.13 import
import 'dart:xxx';
引入Dart标准库
import 'xxx/xxx.dart';
引入相对路径的Dart文件
import 'package:xxx/xxx.dart';
引入Pub仓库pub.dev(或者pub.flutter-io.cn)中的第三方库
import 'package:project/xxx/xxx.dart';
引入自定义的dart文件
import 'xxx' show compute1,compute2
只导入compute1,compute2
import 'xxx' hide compute3
除了compute都引入
import 'xxx' as compute4
将库重命名,当有名字冲突时
library compute5;
定义库名称
part of compute6;
表示文件属于某个库
// dart sdk 内的库
import 'dart:io';
// flutter内的库
import 'package:material/material.dart';
// 第三方库
import 'package:dio/dio.dart';
// 自己的库(文件)
import 'package:project/common/uitls.dart';
// 相对路径引用
import 'xxx/xxx/xxx/xxx.dart';
命名规范:
文件夹:小写下划线 lowercase_with_underscores
文件:小写下划线 lowercase_with_underscores
类名:大写开头的驼峰命名法 UpperCamelCase
变量名:小写开头的驼峰命名法 lowerCamelCase
常量:小写开头的驼峰命名法 lowerCamelCase
首字母缩写词长度不超过两个字母的,首字母大写,比如 HttpRequest
长度两个字母的首字母缩写词可完全大写,比如 IOStream
, DBUtils
但单个单词缩写仍然仅首字母大写,比如 Id
6.14 加载16进制图片
Flutter中, Color 类仅接收整数作为参数. 你也可以使用 fromARGB 或者 fromRGBO .
比如拿到了一个16进制颜色 #b74093 . 因为 Color 还需要传入透明度, 255 就是最大值(也就是不透明), 转为16进制就是 0xFF , 所以我们只需这样表示:
const color = Color(0xffb74093);
正规一点的写法(可选, 因为大小写不敏感):
const color = Color(0xFFB74093);
或者创建一个方法
class HexColor extends Color {
static int _getColorFromHex(String hexColor) {
hexColor = hexColor.toUpperCase().replaceAll("#", "");
if (hexColor.length == 6) {
hexColor = "FF" + hexColor;
}
return int.parse(hexColor, radix: 16);
}
HexColor(final String hexColor) : super(_getColorFromHex(hexColor));
}
// 调用
Color color1 = HexColor("b74093");
Color color2 = HexColor("#b74093");
6.15 沉浸状态栏
参考:https://www.bilibili.com/read/cv5799233
修改安卓文件:
package com.example.fm_app
import io.flutter.embedding.android.FlutterActivity
// 需要引入下面的包
import android.os.Build;
import android.os.Bundle;
class MainActivity: FlutterActivity() {
// 需要添加如下代码
override fun onCreate(saveInstanceState:Bundle?){
super.onCreate(saveInstanceState);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
window?.statusBarColor=0
}
}
}
6.16 显示/隐藏控件
参考:https://www.jianshu.com/p/7250226506af
6.16.1 if 判断
实际效果,不会占位,隐藏时TestWidget
不会加载,可else
一个占位(则会加载占位的组件)
if(!_hidden)
Container(
color: Colors.deepOrangeAccent,
child: TestWidget(msg: 'if 判断',),
)
6.16.2 Opacity
实际效果,会占位,显示隐藏TestWidget都会加载
Container(
color: Colors.red,
child: Opacity(
opacity: _hidden?0:1,//会占位高度
child: TestWidget(msg: 'Opacity',),
),
),
6.16.3 Offstage
实际效果,不会占位,显示隐藏TestWidget都会加载
Container(
color: Colors.grey,
child: Offstage(
offstage: _hidden,///无占位高度
child: TestWidget(msg: 'Offstage',),
),
),
6.16.4 Visibility
实际效果,可选择是否占位,不占位,隐藏时TestWidget不会加载,占位,显示隐藏TestWidget都会加载
Container(
color: Colors.cyanAccent,
child: Visibility(
maintainAnimation: true,
maintainState: true,
maintainSize: true,//隐藏需要占位,前俩个也需要为true,内部断言会判断,不需要时都为false,maintainState影响是否加载
child: TestWidget(msg: 'Visibility',),
visible: _hidden
),
),
6.17 下载文件到本地目录
var tempDir = await getApplicationDocumentsDirectory();
String fullPath = tempDir.path + "/abc.xlsx'";
var data = HttpUtils.download(urlStr, fullPath);
File file = File(fullPath);
bool exist = await file.exists();
if (exist) {
print("文件存在!"+fullPath);
}
var raf = file.openWrite(mode: FileMode.append);
raf.write(data);
await raf.close();