Skip to content
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
15 changes: 12 additions & 3 deletions lib/src/controller/dashboard_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ class DashboardItemController<T extends DashboardItem> with ChangeNotifier {
/// Changes cannot be handled.
DashboardItemController({
required List<T> items,
bool isEditing = false,
}) : _items = items.asMap().map(
(key, value) => MapEntry(value.identifier, value),
),
_initialIsEditing = isEditing,
itemStorageDelegate = null;

/// You can create [DashboardItemController] with an [itemStorageDelegate].
Expand All @@ -34,15 +36,21 @@ class DashboardItemController<T extends DashboardItem> with ChangeNotifier {
///
/// If the delegate is waiting for a Future to load the items, this will throw
/// error at the end of the [timout].
DashboardItemController.withDelegate(
{Duration? timeout, required this.itemStorageDelegate})
: _timeout = timeout ?? const Duration(seconds: 10);
DashboardItemController.withDelegate({
Duration? timeout,
required this.itemStorageDelegate,
bool isEditing = false,
}) : _timeout = timeout ?? const Duration(seconds: 10),
_initialIsEditing = isEditing;

/// To define [itemStorageDelegate] use [DashboardItemController.withDelegate]
///
/// For more see [DashboardItemStorageDelegate] documentation.
final DashboardItemStorageDelegate<T>? itemStorageDelegate;

/// Initial editing state for the controller.
final bool _initialIsEditing;

/// Users can only edit the layout when [isEditing] is true.
/// The [isEditing] does not have to be true to add or delete items.
///
Expand Down Expand Up @@ -206,6 +214,7 @@ class DashboardItemController<T extends DashboardItem> with ChangeNotifier {

void _attach(_DashboardLayoutController layoutController) {
_layoutController = layoutController;
_layoutController!.isEditing = _initialIsEditing;
}
}

Expand Down
55 changes: 21 additions & 34 deletions lib/src/widgets/dashboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class Dashboard<T extends DashboardItem> extends StatefulWidget {
this.absorbPointer = true,
this.animateEverytime = true,
this.itemStyle = const ItemStyle(),
this.scrollToAdded = true})
: assert((slotHeight == null && slotAspectRatio == null) ||
!(slotHeight != null && slotAspectRatio != null)),
this.scrollToAdded = true,
this.child})
: assert((slotHeight == null && slotAspectRatio == null) || !(slotHeight != null && slotAspectRatio != null)),
editModeSettings = editModeSettings ?? EditModeSettings(),
super(key: key);

Expand Down Expand Up @@ -195,12 +195,14 @@ class Dashboard<T extends DashboardItem> extends StatefulWidget {
/// Look [Material] documentation for more.
final ItemStyle itemStyle;

/// Optional child widget to be added to the Stack.
final Widget? child;

@override
State<Dashboard<T>> createState() => _DashboardState<T>();
}

class _DashboardState<T extends DashboardItem> extends State<Dashboard<T>>
with TickerProviderStateMixin {
class _DashboardState<T extends DashboardItem> extends State<Dashboard<T>> with TickerProviderStateMixin {
///
@override
void initState() {
Expand Down Expand Up @@ -229,8 +231,7 @@ class _DashboardState<T extends DashboardItem> extends State<Dashboard<T>>

AsyncSnapshot? get _snap => widget.dashboardItemController._asyncSnap?.value;

bool get _withDelegate =>
widget.dashboardItemController.itemStorageDelegate != null;
bool get _withDelegate => widget.dashboardItemController.itemStorageDelegate != null;

///
late _DashboardLayoutController<T> _layoutController;
Expand Down Expand Up @@ -300,21 +301,16 @@ class _DashboardState<T extends DashboardItem> extends State<Dashboard<T>>
if (widget.slotHeight != null) {
h = widget.slotHeight!;
} else if (widget.slotAspectRatio != null) {
h = _layoutController._viewportDelegate.resolvedConstrains.maxWidth /
widget.slotCount /
widget.slotAspectRatio!;
h = _layoutController._viewportDelegate.resolvedConstrains.maxWidth / widget.slotCount / widget.slotAspectRatio!;
} else {
h = _layoutController._viewportDelegate.resolvedConstrains.maxWidth /
widget.slotCount;
h = _layoutController._viewportDelegate.resolvedConstrains.maxWidth / widget.slotCount;
}

_layoutController._setSizes(
_layoutController._viewportDelegate.resolvedConstrains, h);
_layoutController._setSizes(_layoutController._viewportDelegate.resolvedConstrains, h);

_offset = o;

offset.applyViewportDimension(
_layoutController._viewportDelegate.constraints.maxHeight);
offset.applyViewportDimension(_layoutController._viewportDelegate.constraints.maxHeight);

var maxIndex = (_layoutController._endsTree.lastKey() ?? 0);

Expand All @@ -337,11 +333,9 @@ class _DashboardState<T extends DashboardItem> extends State<Dashboard<T>>
late double _maxExtend;

///
final GlobalKey<_DashboardStackState<T>> _stateKey =
GlobalKey<_DashboardStackState<T>>();
final GlobalKey<_DashboardStackState<T>> _stateKey = GlobalKey<_DashboardStackState<T>>();

final GlobalKey<ScrollableState> _scrollableKey =
GlobalKey<ScrollableState>();
final GlobalKey<ScrollableState> _scrollableKey = GlobalKey<ScrollableState>();

bool scrollable = true;

Expand All @@ -355,8 +349,7 @@ class _DashboardState<T extends DashboardItem> extends State<Dashboard<T>>
(!_reloading || differentReload) &&
_layoutController.slotCount != widget.slotCount &&
_withDelegate &&
widget
.dashboardItemController.itemStorageDelegate!.layoutsBySlotCount) {
widget.dashboardItemController.itemStorageDelegate!.layoutsBySlotCount) {
_reloading = true;
_reloadFor = widget.slotCount;
widget.dashboardItemController._items.clear();
Expand Down Expand Up @@ -403,11 +396,8 @@ class _DashboardState<T extends DashboardItem> extends State<Dashboard<T>>
if (_withDelegate) {
if (_snap!.connectionState == ConnectionState.none) {
_building = false;
return widget.errorPlaceholder
?.call(_snap!.error!, _snap!.stackTrace!) ??
const SizedBox();
} else if (_snap!.connectionState == ConnectionState.waiting ||
_reloading) {
return widget.errorPlaceholder?.call(_snap!.error!, _snap!.stackTrace!) ?? const SizedBox();
} else if (_snap!.connectionState == ConnectionState.waiting || _reloading) {
_building = false;

return widget.loadingPlaceholder ??
Expand All @@ -423,8 +413,7 @@ class _DashboardState<T extends DashboardItem> extends State<Dashboard<T>>

Widget dashboardWidget(BoxConstraints constrains) {
return Scrollable(
physics:
scrollable ? widget.physics : const NeverScrollableScrollPhysics(),
physics: scrollable ? widget.physics : const NeverScrollableScrollPhysics(),
key: _scrollableKey,
controller: widget.scrollController,
semanticChildCount: widget.dashboardItemController._items.length,
Expand Down Expand Up @@ -454,16 +443,14 @@ class _DashboardState<T extends DashboardItem> extends State<Dashboard<T>>
key: _stateKey,
itemBuilder: widget.itemBuilder,
dashboardController: _layoutController,
offset: offset);
offset: offset,
child: widget.child);
});
}
}

class _ItemCurrentPositionTween extends Tween<_ItemCurrentPosition> {
_ItemCurrentPositionTween(
{required _ItemCurrentPosition begin,
required _ItemCurrentPosition end,
required this.onlyDimensions})
_ItemCurrentPositionTween({required _ItemCurrentPosition begin, required _ItemCurrentPosition end, required this.onlyDimensions})
: super(begin: begin, end: end);

bool onlyDimensions;
Expand Down
Loading