截图
class PngHome extends StatefulWidget {
PngHome({Key key}) : super(key: key);
@override
_PngHomeState createState() => _PngHomeState();
}
class _PngHomeState extends State<PngHome> {
GlobalKey globalKey = GlobalKey();
Future<void> _capturePng() async {
RenderRepaintBoundary boundary = globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage(pixelRatio: window.devicePixelRatio);
ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
print(pngBytes);
}
@override
Widget build(BuildContext context) {
return RepaintBoundary(
key: globalKey,
child: Center(
child: FlatButton(
child: Text('Hello World', textDirection: TextDirection.ltr),
onPressed: _capturePng,
),
),
);
}
}
注意
boundary.toImage(pixelRatio: window.devicePixelRatio);
中的pixelRatio表示设备像素比。不传默认是1,截图会比较模糊;设置为当前设备的像素比后,在iOS中如果直接用 [UIImage imageNamed:imageFileName]; 获取,图片的像素 = 设备像素*缩放因子,比较大。如果想得到原尺寸,需要使用下面方式获取。
NSData *imageData = [NSData dataWithContentsOfFile:imageFileName];
UIImage *img = [UIImage imageWithData:imageData scale:[UIScreen mainScreen].scale];
TextField 输入文字过长不显示
出现这种情况,可能是TextField布局有问题,尺寸偏小。我遇到的情况是,开始将TextField 的高度设置多小,输入超长后,突然就不显示了,将高度设置高点,解决了。
zsh: command not found: flutter
安装完flutter重启电脑后运行flutter,报zsh: command not found: flutter,想要flutter永久有效,执行以下
在终端打开:vim ~/.zshrc
在里面添加一句:source ~/.bash_profile
然后保存,
执行 source ~/.zshrc
,解决!