Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Option to customize available paintmodes #95

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/src/_paint_over_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ImagePainter extends StatefulWidget {
this.networkUrl,
this.byteArray,
this.file,
this.customPaintModes,
this.height,
this.width,
this.placeHolder,
Expand Down Expand Up @@ -56,6 +57,7 @@ class ImagePainter extends StatefulWidget {
String url, {
required ImagePainterController controller,
Key? key,
List<PaintMode>? customPaintModes,
double? height,
double? width,
Widget? placeholderWidget,
Expand All @@ -82,6 +84,7 @@ class ImagePainter extends StatefulWidget {
key: key,
controller: controller,
networkUrl: url,
customPaintModes: customPaintModes,
height: height,
width: width,
placeHolder: placeholderWidget,
Expand Down Expand Up @@ -111,6 +114,7 @@ class ImagePainter extends StatefulWidget {
String path, {
required ImagePainterController controller,
Key? key,
List<PaintMode>? customPaintModes,
double? height,
double? width,
bool? scalable,
Expand All @@ -137,6 +141,7 @@ class ImagePainter extends StatefulWidget {
controller: controller,
key: key,
assetPath: path,
customPaintModes: customPaintModes,
height: height,
width: width,
isScalable: scalable ?? false,
Expand Down Expand Up @@ -166,6 +171,7 @@ class ImagePainter extends StatefulWidget {
File file, {
required ImagePainterController controller,
Key? key,
List<PaintMode>? customPaintModes,
double? height,
double? width,
bool? scalable,
Expand All @@ -192,6 +198,7 @@ class ImagePainter extends StatefulWidget {
controller: controller,
key: key,
file: file,
customPaintModes: customPaintModes,
height: height,
width: width,
placeHolder: placeholderWidget,
Expand Down Expand Up @@ -221,6 +228,7 @@ class ImagePainter extends StatefulWidget {
Uint8List byteArray, {
required ImagePainterController controller,
Key? key,
List<PaintMode>? customPaintModes,
double? height,
double? width,
bool? scalable,
Expand All @@ -247,6 +255,7 @@ class ImagePainter extends StatefulWidget {
controller: controller,
key: key,
byteArray: byteArray,
customPaintModes: customPaintModes,
height: height,
width: width,
placeHolder: placeholderWidget,
Expand Down Expand Up @@ -276,6 +285,7 @@ class ImagePainter extends StatefulWidget {
required ImagePainterController controller,
required double height,
required double width,
List<PaintMode>? customPaintModes,
Key? key,
Color? signatureBgColor,
List<Color>? colors,
Expand All @@ -299,6 +309,7 @@ class ImagePainter extends StatefulWidget {
return ImagePainter._(
controller: controller,
key: key,
customPaintModes: customPaintModes,
height: height,
width: width,
isSignature: true,
Expand Down Expand Up @@ -339,6 +350,10 @@ class ImagePainter extends StatefulWidget {
///Only accessible through [ImagePainter.asset] constructor.
final String? assetPath;

///Custom definition and order of available paint modes.
///If not provided, all modes are available in default order.
final List<PaintMode>? customPaintModes;

///Height of the Widget. Image is subjected to fit within the given height.
final double? height;

Expand Down Expand Up @@ -712,7 +727,7 @@ class ImagePainterState extends State<ImagePainter> {
child: Center(
child: SizedBox(
child: Wrap(
children: paintModes(textDelegate)
children: paintModes(textDelegate, widget.customPaintModes)
.map(
(item) => SelectionItems(
data: item,
Expand Down Expand Up @@ -830,7 +845,7 @@ class ImagePainterState extends State<ImagePainter> {
AnimatedBuilder(
animation: _controller,
builder: (_, __) {
final icon = paintModes(textDelegate)
final icon = paintModes(textDelegate, widget.customPaintModes)
.firstWhere((item) => item.mode == _controller.mode)
.icon;
return PopupMenuButton(
Expand Down
81 changes: 47 additions & 34 deletions lib/src/widgets/_mode_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,40 +51,53 @@ class SelectionItems extends StatelessWidget {
}
}

List<ModeData> paintModes(TextDelegate textDelegate) => [
ModeData(
icon: Icons.zoom_out_map,
mode: PaintMode.none,
label: textDelegate.noneZoom),
ModeData(
icon: Icons.horizontal_rule,
mode: PaintMode.line,
label: textDelegate.line),
ModeData(
icon: Icons.crop_free,
mode: PaintMode.rect,
label: textDelegate.rectangle),
ModeData(
icon: Icons.edit,
mode: PaintMode.freeStyle,
label: textDelegate.drawing),
ModeData(
icon: Icons.lens_outlined,
mode: PaintMode.circle,
label: textDelegate.circle),
ModeData(
icon: Icons.arrow_right_alt_outlined,
mode: PaintMode.arrow,
label: textDelegate.arrow),
ModeData(
icon: Icons.power_input,
mode: PaintMode.dashLine,
label: textDelegate.dashLine),
ModeData(
icon: Icons.text_format,
mode: PaintMode.text,
label: textDelegate.text),
];
List<ModeData> paintModes(TextDelegate textDelegate, [List<PaintMode>? customPaintModes]) {
final Map<PaintMode, ModeData> allPaintModes = {
PaintMode.none: ModeData(
icon: Icons.zoom_out_map,
mode: PaintMode.none,
label: textDelegate.noneZoom),
PaintMode.line: ModeData(
icon: Icons.horizontal_rule,
mode: PaintMode.line,
label: textDelegate.line),
PaintMode.rect: ModeData(
icon: Icons.crop_free,
mode: PaintMode.rect,
label: textDelegate.rectangle),
PaintMode.freeStyle: ModeData(
icon: Icons.edit,
mode: PaintMode.freeStyle,
label: textDelegate.drawing),
PaintMode.circle: ModeData(
icon: Icons.lens_outlined,
mode: PaintMode.circle,
label: textDelegate.circle),
PaintMode.arrow: ModeData(
icon: Icons.arrow_right_alt_outlined,
mode: PaintMode.arrow,
label: textDelegate.arrow),
PaintMode.dashLine: ModeData(
icon: Icons.power_input,
mode: PaintMode.dashLine,
label: textDelegate.dashLine),
PaintMode.text: ModeData(
icon: Icons.text_format,
mode: PaintMode.text,
label: textDelegate.text),
};

final List<ModeData> customModeData = <ModeData>[];

if (customPaintModes != null) {
for (final PaintMode mode in customPaintModes) {
customModeData.add(allPaintModes[mode]!);
}
return customModeData;
} else {
return allPaintModes.values.toList();
}
}

@immutable
class ModeData {
Expand Down