Skip to content

Commit

Permalink
Merge pull request #95 from fluttercandies/add-doc
Browse files Browse the repository at this point in the history
doc: Add api document for dart classes
  • Loading branch information
CaiJingLong authored Jul 19, 2022
2 parents 62b7025 + 8133b30 commit 2c2ac43
Show file tree
Hide file tree
Showing 23 changed files with 282 additions and 11 deletions.
4 changes: 2 additions & 2 deletions example/lib/draw_example_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ class _DrawExamplePageState extends State<DrawExamplePage> {
paint: paint,
);
path.move(Offset.zero);
path.lineTo(randomOffset(), paint);
path.lineTo(randomOffset(), paint);
path.lineTo(randomOffset());
path.lineTo(randomOffset());
path.bezier2To(randomOffset(), randomOffset());
path.bezier3To(randomOffset(), randomOffset(), randomOffset());
await addDrawPart(path);
Expand Down
7 changes: 7 additions & 0 deletions lib/src/channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ import 'package:flutter/services.dart';

import 'option/edit_options.dart';

/// Wrapper for [MethodChannel].
class NativeChannel {
const NativeChannel._();

/// The channel for use native method.
static const MethodChannel channel = MethodChannel(
'com.fluttercandies/image_editor',
);

/// Get the cache directory path.
static Future<Directory> getCachePath() async {
final path = await channel.invokeMethod('getCachePath');
return Directory(path);
}

/// Handle memory source and get file result.
static Future<String> memoryToFile(
Uint8List memory,
ImageEditorOption option,
Expand All @@ -33,6 +37,7 @@ class NativeChannel {
});
}

/// Handle memory source and get momory result.
static Future<Uint8List> memoryToMemory(
Uint8List memory,
ImageEditorOption option,
Expand All @@ -49,6 +54,7 @@ class NativeChannel {
return result;
}

/// Handle file source and get momory result.
static Future<Uint8List> fileToMemory(
String path,
ImageEditorOption option,
Expand All @@ -64,6 +70,7 @@ class NativeChannel {
});
}

/// Handle file source and get file result.
static Future<String> fileToFile(
String src,
ImageEditorOption option,
Expand Down
5 changes: 5 additions & 0 deletions lib/src/convert_value.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import 'package:flutter/services.dart';

/// Used to describe fields that can be JSON,
/// mainly used for [MethodChannel] invoke.
mixin JsonAble {
/// Convert current instance to [Map].
Map<String, Object?> toJson();
}
32 changes: 30 additions & 2 deletions lib/src/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ import 'channel.dart';
import 'image_handler.dart';
import 'option/edit_options.dart';

/// The main class of ImageEditor plugin.
class ImageEditor {
const ImageEditor._();

/// [image] Uint8List
/// [imageEditorOption] option of
/// The [image] is a source of image.
///
/// The [imageEditorOption] option for edit image.
///
/// The method will return a [Uint8List] as image result.
///
/// If result is null, it means handle image error.
static Future<Uint8List?> editImage({
required Uint8List image,
required ImageEditorOption imageEditorOption,
Expand All @@ -31,6 +37,13 @@ class ImageEditor {
return tmp;
}

/// The [file] is the source of image.
///
/// The [imageEditorOption] is the option for edit image.
///
/// The method will return a [Uint8List] as image result.
///
/// If result is null, it means handle image error.
static Future<Uint8List?> editFileImage({
required File file,
required ImageEditorOption imageEditorOption,
Expand All @@ -57,6 +70,13 @@ class ImageEditor {
}
}

/// The [file] is the source of image.
///
/// The [imageEditorOption] is the option for edit image.
///
/// The method will return a [File] as image result.
///
/// If result is null, it means handle image error.
static Future<File?> editFileImageAndGetFile({
required File file,
required ImageEditorOption imageEditorOption,
Expand All @@ -78,6 +98,13 @@ class ImageEditor {
return tmp;
}

/// The [image] is the source of image.
///
/// The [imageEditorOption] is the option for edit image.
///
/// The method will return a [File] as image result.
///
/// If result is null, it means handle image error.
static Future<File> editImageAndGetFile({
required Uint8List image,
required ImageEditorOption imageEditorOption,
Expand All @@ -102,6 +129,7 @@ class ImageEditor {
return file;
}

/// The method will create a temp file path.
static Future<String> _createTmpFilePath() async {
final cacheDir = await NativeChannel.getCachePath();
final name = DateTime.now().millisecondsSinceEpoch;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/error.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/// Error for handle image.
class HandleError implements Exception {
/// Error for handle image.
HandleError([this.msg = '']);

/// The message of error.
final String msg;

@override
Expand Down
1 change: 1 addition & 0 deletions lib/src/font_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:io';

import 'channel.dart';

/// Wrapper for use channel to register font for add text.
class FontManager {
const FontManager._();

Expand Down
1 change: 1 addition & 0 deletions lib/src/image_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'error.dart';
import 'option/edit_options.dart';
import 'type.dart';

/// Wrapper for use channel to handle image.
class ImageHandler {
ImageHandler.memory(this._memory)
: _type = SrcType.memory,
Expand Down
11 changes: 11 additions & 0 deletions lib/src/image_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,50 @@ import 'dart:typed_data';

import 'convert_value.dart';

/// Used to describe the source of an image.
abstract class ImageSource with JsonAble {
/// Used to describe the source of an image.
const ImageSource();

/// Used to describe the memory image.
factory ImageSource.memory(Uint8List uint8list) {
return MemoryImageSource(uint8list);
}

/// Used to describe the file image.
factory ImageSource.file(File file) {
return FileImageSource(file);
}

/// Used to describe the file image.
factory ImageSource.path(String path) {
return FileImageSource.path(path);
}

/// The image data as [Uint8List].
Uint8List get memory;

@override
Map<String, Object?> toJson() => <String, Object?>{'memory': memory};
}

/// The memory image source.
class MemoryImageSource extends ImageSource {
const MemoryImageSource(Uint8List uint8list) : memory = uint8list;

@override
final Uint8List memory;
}

/// The file image source.
class FileImageSource extends ImageSource {
/// Use [file] to create image source.
const FileImageSource(this.file);

/// Use [path] to create image source.
FileImageSource.path(String path) : file = File(path);

/// The file of image.
final File file;

@override
Expand Down
3 changes: 3 additions & 0 deletions lib/src/merge/image_merger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import 'package:image_editor/src/channel.dart';

import 'merge_option.dart';

/// Use the class to merge multiple images.
class ImageMerger {
const ImageMerger._();

/// Merge multiple images.
static Future<Uint8List?> mergeToMemory({required ImageMergeOption option}) {
return NativeChannel.channel.invokeMethod('mergeToMemory', {
'option': option.toJson(),
});
}

/// Merge multiple images and get file.
static Future<File?> mergeToFile({required ImageMergeOption option}) {
return NativeChannel.channel.invokeMethod('mergeToFile', {
'option': option.toJson(),
Expand Down
11 changes: 11 additions & 0 deletions lib/src/merge/merge_option.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ import '../image_source.dart';
import '../output_format.dart';
import '../position.dart';

/// Merge multiple images option.
class ImageMergeOption with JsonAble {
ImageMergeOption({
required this.canvasSize,
this.format = const OutputFormat.jpeg(90),
}) : assert(canvasSize > Size.zero);

/// The canvas size. (output image size)
final Size canvasSize;

/// Output format for result image.
final OutputFormat format;

/// All merge image options.
final List<MergeImageConfig> _mergeImageConfig = <MergeImageConfig>[];

/// Add a merge image option.
void addImage(MergeImageConfig config) {
_mergeImageConfig.add(config);
}
Expand All @@ -31,13 +37,18 @@ class ImageMergeOption with JsonAble {
}
}

/// Merge image option.
class MergeImageConfig with JsonAble {
/// Merge image option.
const MergeImageConfig({
required this.image,
required this.position,
});

/// The image source.
final ImageSource image;

/// The position of image.
final ImagePosition position;

@override
Expand Down
13 changes: 13 additions & 0 deletions lib/src/option/add_text.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
part of 'edit_options.dart';

/// Add text to image.
class AddTextOption implements Option {
AddTextOption();

/// The items of added texts.
final List<EditorText> texts = <EditorText>[];

/// Add [text] to [texts].
void addText(EditorText text) {
texts.add(text);
}
Expand All @@ -23,6 +26,7 @@ class AddTextOption implements Option {
}
}

/// Descript of a text.
class EditorText {
const EditorText({
required this.text,
Expand All @@ -32,10 +36,19 @@ class EditorText {
this.fontName = '',
});

/// The text.
final String text;

/// The offset of text.
final Offset offset;

/// The font size of text.
final int fontSizePx;

/// The color of text.
final Color textColor;

/// The font name of text, if fontName is empty string, the text will use system font.
final String fontName;

int get y {
Expand Down
22 changes: 21 additions & 1 deletion lib/src/option/clip.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
part of 'edit_options.dart';

///
/// {@template image_editor.option.clip}
///
/// The clip option is used to clip image.
///
/// {@endtemplate}
class ClipOption implements Option {
ClipOption({
/// {@macro image_editor.option.clip}
const ClipOption({
this.x = 0,
this.y = 0,
required this.width,
required this.height,
}) : assert(width > 0 && height > 0),
assert(x >= 0, y >= 0);

/// {@macro image_editor.option.clip}
factory ClipOption.fromRect(Rect rect) {
return ClipOption(
x: rect.left.round(),
Expand All @@ -18,6 +26,7 @@ class ClipOption implements Option {
);
}

/// {@macro image_editor.option.clip}
factory ClipOption.fromOffset(Offset start, Offset end) {
return ClipOption(
x: start.dx.round(),
Expand All @@ -27,9 +36,20 @@ class ClipOption implements Option {
);
}

/// The x coordinate of clip.
final num x;

/// The y coordinate of clip.
final num y;

/// The width of clip.
///
/// The width must be greater than 0.
final num width;

/// The height of clip.
///
/// The height must be greater than 0.
final num height;

@override
Expand Down
Loading

0 comments on commit 2c2ac43

Please sign in to comment.