diff --git a/CHANGELOG.md b/CHANGELOG.md index dbcdfe9..d938adf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +## [1.2.2] - 2026-04-28 + +@rickypid +Co-authored-by: RichiB20 + +- Fix `setState` called during build in `initState` by deferring `controller.fetch()` via `addPostFrameCallback`. +- Fix `setState` called on unmounted widget in `_getAsync` by adding `mounted` guards before each `setState` call. +- Skip `controller.fetch()` on pan end when the visible date range has not changed (e.g. simple tap or sub-day drag). + ## [1.2.1] - 2026-03-31 @rickypid diff --git a/example/pubspec.lock b/example/pubspec.lock index 897b077..5c30c61 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -36,7 +36,7 @@ packages: path: ".." relative: true source: path - version: "1.2.1" + version: "1.2.2" flutter_lints: dependency: "direct dev" description: diff --git a/lib/src/widgets/gantt.dart b/lib/src/widgets/gantt.dart index 40e7d43..83cd4c8 100644 --- a/lib/src/widgets/gantt.dart +++ b/lib/src/widgets/gantt.dart @@ -136,6 +136,7 @@ class _GanttState extends State { late GanttTheme theme; late GanttController controller; Offset? _lastPosition; + DateTime? _panStartDate; late LinkedScrollControllerGroup _linkedControllers; late ScrollController _listController; late ScrollController _gridColumnsController; @@ -161,7 +162,9 @@ class _GanttState extends State { if (widget.activities != null) { controller.setActivities(widget.activities!, notify: false); } else { - controller.fetch(); + WidgetsBinding.instance.addPostFrameCallback((_) { + if (mounted) controller.fetch(); + }); } if (widget.highlightedDates != null) { controller.setHighlightedDates(widget.highlightedDates!, notify: false); @@ -185,8 +188,10 @@ class _GanttState extends State { super.dispose(); } - void _handlePanStart(DragStartDetails details) => - _lastPosition = details.localPosition; + void _handlePanStart(DragStartDetails details) { + _lastPosition = details.localPosition; + _panStartDate = controller.startDate; + } void _handlePanUpdate( DragUpdateDetails details, @@ -234,23 +239,31 @@ class _GanttState extends State { void _handlePanCancel() => _reset(); void _reset() { + final dateChanged = + _panStartDate != null && + !controller.startDate.isAtSameMomentAs(_panStartDate!); _lastPosition = null; - controller.fetch(); + _panStartDate = null; + if (dateChanged) controller.fetch(); } Future _getAsync() async { + if (!mounted) return; if (widget.activitiesAsync != null || widget.holidaysAsync != null) { var activities = []; var holidays = []; - setState(() { - _loading = true; - }); + if (mounted) { + setState(() { + _loading = true; + }); + } if (widget.activitiesAsync != null) { activities = await widget.activitiesAsync!( controller.startDate, controller.endDate, controller.activities, ); + if (!mounted) return; controller.setActivities(activities, notify: false); } if (widget.holidaysAsync != null) { @@ -259,11 +272,14 @@ class _GanttState extends State { controller.endDate, controller.holidays, ); + if (!mounted) return; controller.setHolidays(holidays, notify: false); } - setState(() { - _loading = false; - }); + if (mounted) { + setState(() { + _loading = false; + }); + } } } diff --git a/pubspec.yaml b/pubspec.yaml index 2946360..c1001d6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_gantt description: "Flutter package for rendering an interactive and customizable Gantt chart widget." -version: 1.2.1 +version: 1.2.2 homepage: https://github.com/insideapp-srl/flutter_gantt repository: https://github.com/insideapp-srl/flutter_gantt