tutorial_coach_mark
tutorial_coach_mark copied to clipboard
Throw custom Error/Exception when target position is not found
Hi there, great package!
I was wondering if it would be possible to add a feature which throws a custom Error/Exception when the target position is not found, so that it can be handled. E.g., when the widget is not shown in a specific UI flow, then the following is printed:
"TutorialCoachMark (ERROR): It was not possible to obtain target position."
I would like to catch this and show the tutorial again next time on a different flow, so I was wondering if the following would work in util.dart.
import 'package:flutter/widgets.dart';
import 'package:tutorial_coach_mark/src/target/target_focus.dart';
import 'package:tutorial_coach_mark/src/target/target_position.dart';
enum ShapeLightFocus { Circle, RRect }
TargetPosition? getTargetCurrent(TargetFocus target,
//!
{bool throwNotFoundException = false}) {
if (target.keyTarget != null) {
var key = target.keyTarget!;
try {
final RenderBox renderBoxRed =
key.currentContext!.findRenderObject() as RenderBox;
final size = renderBoxRed.size;
final state =
key.currentContext!.findAncestorStateOfType<NavigatorState>();
Offset offset;
if (state != null) {
offset = renderBoxRed.localToGlobal(Offset.zero,
ancestor: state.context.findRenderObject());
} else {
offset = renderBoxRed.localToGlobal(Offset.zero);
}
return TargetPosition(size, offset);
} catch (e) {
//!
if (throwNotFoundException) {
throw TargetNotFoundException(
'TutorialCoachMark (ERROR): It was not possible to obtain target position.');
} else {
print(
"TutorialCoachMark (ERROR): It was not possible to obtain target position.");
return null;
}
}
} else {
return target.targetPosition;
}
}
abstract class TutorialCoachMarkController {
void next();
void previous();
void skip();
}
extension StateExt on State {
void safeSetState(VoidCallback call) {
if (mounted) {
// ignore: invalid_use_of_protected_member
setState(call);
}
}
}
//!
class TargetNotFoundException implements Exception {
String message;
TargetNotFoundException(this.message);
}