You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
147 lines
4.9 KiB
Dart
147 lines
4.9 KiB
Dart
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/rendering.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:path/path.dart' as mypath;
|
|
import 'dart:ui' as ui;
|
|
|
|
Future getIconWidget(String name) async {
|
|
// print("name length = ${name.length}");
|
|
double _widthBtn = name.length * 40.0;
|
|
double _text_height = 45;
|
|
double _icon_width = 40;
|
|
double _icon_height = 65;
|
|
double _edge = 16;
|
|
|
|
return Container(
|
|
width: _widthBtn * 1.5 + _icon_width,
|
|
height: _text_height + _icon_height,
|
|
color: Colors.transparent,
|
|
child: Column(
|
|
children: [
|
|
Container(
|
|
width: _widthBtn,
|
|
height: _icon_height,
|
|
color: Colors.transparent,
|
|
child: Directionality(
|
|
textDirection: TextDirection.ltr,
|
|
child: RichText(
|
|
text: TextSpan(style: TextStyle(color: Colors.black, fontSize: 36.0), children: [
|
|
WidgetSpan(
|
|
child: IconTheme(
|
|
data: IconThemeData(size: 64, color: Colors.red),
|
|
child: Icon(
|
|
new IconData(0xe60b, fontFamily: 'myfont', matchTextDirection: true),
|
|
),
|
|
)),
|
|
// TextSpan(text: "\n白塔山公园", style: TextStyle(color: Colors.black, fontSize: 36.0)),
|
|
]),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
),
|
|
),
|
|
Row(
|
|
textDirection: TextDirection.ltr,
|
|
children: [
|
|
SizedBox(width: _widthBtn * 0.5 + _icon_width),
|
|
Container(
|
|
// margin: EdgeInsets.only(left: _widthBtn * 0.5),
|
|
// padding: EdgeInsets.only(left: _widthBtn * 0.5),
|
|
width: _widthBtn,
|
|
height: _text_height,
|
|
color: Colors.yellow,
|
|
child: Directionality(
|
|
textDirection: TextDirection.ltr,
|
|
child: Text(name,
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
color: Colors.black, fontWeight: FontWeight.bold, fontSize: 36.0))),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Future createDir(String dirName) async {
|
|
final appDir = await getApplicationDocumentsDirectory();
|
|
final myDir = Directory('${appDir.path}/$dirName');
|
|
|
|
// 查看目录是否存在
|
|
bool isExist = await myDir.exists();
|
|
if (!isExist) {
|
|
myDir.create(recursive: true);
|
|
}
|
|
return myDir.path;
|
|
}
|
|
|
|
Future<void> saveImage(String dirName, String fileName, ByteData image) async {
|
|
final appDir = await getApplicationDocumentsDirectory();
|
|
final myDir = Directory('${appDir.path}/$dirName');
|
|
|
|
// 查看目录是否存在
|
|
bool isExist = await myDir.exists();
|
|
if (!isExist) {
|
|
myDir.create(recursive: true);
|
|
}
|
|
|
|
// final myDir = await Directory('${appDir.path}/$dirName').create(recursive: true);
|
|
final localPath = mypath.join(myDir.path, fileName);
|
|
print('localPath = $localPath');
|
|
// localPath = /data/user/0/com.example.hyzp_yibin_bmfmap/app_flutter/myIcons/myIcon1.png
|
|
File(localPath).writeAsBytesSync(image.buffer.asInt8List());
|
|
}
|
|
|
|
Future<ByteData> widgetToImage(Widget widget,
|
|
{Alignment alignment = Alignment.center,
|
|
Size size = const Size(double.maxFinite, double.maxFinite),
|
|
double devicePixelRatio = 1.0,
|
|
double pixelRatio = 1.0}) async {
|
|
RenderRepaintBoundary repaintBoundary = RenderRepaintBoundary();
|
|
|
|
RenderView renderView = RenderView(
|
|
child: RenderPositionedBox(alignment: alignment, child: repaintBoundary),
|
|
configuration: ViewConfiguration(
|
|
size: size,
|
|
devicePixelRatio: devicePixelRatio,
|
|
),
|
|
window: ui.window,
|
|
);
|
|
|
|
PipelineOwner pipelineOwner = PipelineOwner();
|
|
pipelineOwner.rootNode = renderView;
|
|
renderView.prepareInitialFrame();
|
|
|
|
BuildOwner buildOwner = BuildOwner();
|
|
buildOwner.focusManager = FocusManager();
|
|
RenderObjectToWidgetElement rootElement = RenderObjectToWidgetAdapter(
|
|
container: repaintBoundary,
|
|
child: widget,
|
|
).attachToRenderTree(buildOwner);
|
|
buildOwner.buildScope(rootElement);
|
|
buildOwner.finalizeTree();
|
|
|
|
pipelineOwner.flushLayout();
|
|
pipelineOwner.flushCompositingBits();
|
|
pipelineOwner.flushPaint();
|
|
|
|
ui.Image image = await repaintBoundary.toImage(pixelRatio: pixelRatio);
|
|
// E/flutter (10823): [ERROR:flutter/flow/layers/transform_layer.cc(24)] TransformLayer is constructed with an invalid matrix.
|
|
// E/flutter (10823): [ERROR:flutter/flow/layers/transform_layer.cc(24)] TransformLayer is constructed with an invalid matrix.
|
|
ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
|
|
|
|
return byteData;
|
|
}
|
|
|
|
// read image file from path
|
|
Future<File> getLocalFile(String filename) async {
|
|
String dir = (await getApplicationDocumentsDirectory()).path;
|
|
final directoryName = "myIcons";
|
|
File file = new File('$dir/$directoryName/$filename');
|
|
return file;
|
|
}
|