initState method
Called when this object is inserted into the tree.
The framework will call this method exactly once for each State object it creates.
Override this method to perform initialization that depends on the location at which this object was inserted into the tree (i.e., context) or on the widget used to configure this object (i.e., widget).
If a State's build method depends on an object that can itself change state, for example a ChangeNotifier or Stream, or some other object to which one can subscribe to receive notifications, then be sure to subscribe and unsubscribe properly in initState, didUpdateWidget, and dispose:
- In initState, subscribe to the object.
- In didUpdateWidget unsubscribe from the old object and subscribe to the new one if the updated widget configuration requires replacing the object.
- In dispose, unsubscribe from the object.
You should not use BuildContext.dependOnInheritedWidgetOfExactType from this method. However, didChangeDependencies will be called immediately following this method, and BuildContext.dependOnInheritedWidgetOfExactType can be used there.
Implementations of this method should start with a call to the inherited
method, as in super.initState()
.
Implementation
@override
void initState() {
super.initState();
// Initialize debouncers
_onScaleEndDebounce = Debounce(const Duration(milliseconds: 10));
_onScaleAllowUpdateDebounce = Debounce(const Duration(milliseconds: 1));
_scrollHistoryDebounce = Debounce(const Duration(milliseconds: 350));
// Initialize controllers
_bottomBarScrollCtrl = ScrollController();
_fakeHeroTransformConfigs = transformConfigs ?? TransformConfigs.empty();
_interactiveCornerArea = isDesktop
? cropRotateEditorConfigs.desktopCornerDragArea
: cropRotateEditorConfigs.mobileCornerDragArea;
_desktopInteractionManager =
CropDesktopInteractionManager(context: context);
ServicesBinding.instance.keyboard.addHandler(_onKeyEvent);
// Initialize image and layers
_imageNeedDecode = mainImageSize == null;
_imageSizeIsDecoded = !_imageNeedDecode;
_layers = initConfigs.layers ?? [];
_setRawLayers();
// Initialize rotate animation
double initAngle = transformConfigs?.angle ?? 0.0;
rotateCtrl = AnimationController(
duration: cropRotateEditorConfigs.animationDuration, vsync: this);
rotateCtrl.addStatusListener((status) {
if (status == AnimationStatus.completed) {
if (_blockInteraction) {
addHistory(scaleRotation: oldScaleFactor);
}
_blockInteraction = false;
cropRotateEditorCallbacks?.handleRotateEnd(rotateAnimation.value);
}
});
rotateAnimation =
Tween<double>(begin: initAngle, end: initAngle).animate(rotateCtrl);
// Initialize scale animation
double initScale = (transformConfigs?.scaleRotation ?? 1);
scaleCtrl = AnimationController(
duration: cropRotateEditorConfigs.animationDuration, vsync: this);
scaleAnimation =
Tween<double>(begin: initScale, end: initScale).animate(scaleCtrl);
// Initialize aspect ratio
aspectRatio =
cropRotateEditorConfigs.initAspectRatio ?? CropAspectRatios.custom;
// Set pixel ratio if needed
if (widget.initConfigs.convertToUint8List) {
setImageInfos(activeHistory: activeHistory);
}
// Initialize transform configs if available
if (transformConfigs != null && transformConfigs!.isNotEmpty) {
rotationCount = (transformConfigs!.angle * 2 / pi).abs().toInt();
flipX = transformConfigs!.flipX;
flipY = transformConfigs!.flipY;
translate = transformConfigs!.offset;
userScaleFactor = transformConfigs!.scaleUser;
aspectRatio = transformConfigs!.aspectRatio;
cropRect = transformConfigs!.cropRect;
_viewRect = transformConfigs!.cropRect;
oldScaleFactor = transformConfigs!.scaleRotation;
_rotationScaleFactor = oldScaleFactor;
setInitHistory(transformConfigs!);
}
// Initialize fake hero settings
enableFakeHero = initConfigs.enableFakeHero;
_showFakeHero = enableFakeHero;
// Perform post-frame initialization
cropRotateEditorCallbacks?.onInit?.call();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
cropRotateEditorCallbacks?.onAfterViewInit?.call();
initialized = true;
if (transformConfigs != null &&
transformConfigs!.isNotEmpty &&
transformConfigs!.aspectRatio < 0) {
aspectRatio = transformConfigs!.cropRect.size.aspectRatio;
calcCropRect(onlyViewRect: transformConfigs?.isEmpty == false);
aspectRatio = -1;
} else {
calcCropRect(onlyViewRect: transformConfigs?.isEmpty == false);
}
if (!enableFakeHero) hideFakeHero();
_updateAllStates();
_setRawLayers();
/// Skip one frame to ensure the image is correctly transformed
Size? originalSize = transformConfigs?.originalSize;
if (originalSize != null && !originalSize.isInfinite) {
WidgetsBinding.instance.addPostFrameCallback((_) async {
/// Fit to the screen and set duration to zero
double oldScaleAnimationValue = scaleAnimation.value;
scaleCtrl.duration = Duration.zero;
calcFitToScreen();
scaleCtrl.duration = cropRotateEditorConfigs.animationDuration;
_setCropRectBoundings(oldScaleAnimationValue: oldScaleAnimationValue);
});
}
});
}