Skip to content

Commit

Permalink
Adding id to be used as an internal Key and facilitating
Browse files Browse the repository at this point in the history
reconfiguration of the layout while maintaining the state of the
widgets. #68
  • Loading branch information
caduandrade committed Jun 11, 2024
1 parent 52d025d commit 70e6659
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
11 changes: 8 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.2.0

* `Area`
* Adding `id` to be used as an internal Key and facilitating reconfiguration of the layout while maintaining the state of the widgets.

## 3.1.0

* `Area`
Expand All @@ -9,11 +14,11 @@

## 3.0.2

Bug fix: Other Widgets visually behind are not receiving mouse events.
Bugfix: Other Widgets visually behind are not receiving mouse events.

## 3.0.1

Bug fix: Stretching to recover the minimum size when space is available.
Bugfix: Stretching to recover the minimum size when space is available.

## 3.0.0

Expand All @@ -30,7 +35,7 @@ Bug fix: Stretching to recover the minimum size when space is available.
* `MultiSplitView`
* Width and height configuration to be used in an unbounded situation.
* Setting the widget directly in the area or through builders.
* Bug fix
* Bugfix
* Area using the child widget key.

## 2.4.0
Expand Down
22 changes: 15 additions & 7 deletions lib/src/area.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:meta/meta.dart';
import 'package:multi_split_view/src/area_widget_builder.dart';
import 'package:multi_split_view/src/internal/num_util.dart';
Expand All @@ -25,9 +24,11 @@ class Area {
double? flex,
double? min,
double? max,
dynamic id,
this.data,
this.builder})
: _size = size,
: this.id = id != null ? id : _AreaId(),
_size = size,
_flex = flex,
_min = min,
_max = max {
Expand Down Expand Up @@ -87,18 +88,17 @@ class Area {
/// Any data associated with the area.
dynamic data;

UniqueKey _key = UniqueKey();
/// Used as an internal Key and facilitates reconfiguration of the layout
/// while maintaining the state of the widget.
/// It will never be null and must be unique in the layout.
final dynamic id;

/// The widget builder.
AreaWidgetBuilder? builder;
}

@internal
class AreaHelper {
static Key keyFrom({required Area area}) {
return area._key;
}

/// Sets the area flex value.
static void setFlex({required Area area, required double flex}) {
flex = NumUtil.fix('flex', flex);
Expand Down Expand Up @@ -134,3 +134,11 @@ class AreaHelper {
area._index = index;
}
}

/// Default area id object
class _AreaId {
@override
String toString() {
return 'area id: $hashCode';
}
}
9 changes: 9 additions & 0 deletions lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class MultiSplitViewController extends ChangeNotifier {

/// Allows to automatically set a new value for the data attribute of the [Area].
AreaDataModifier? _areaDataModifier;

AreaDataModifier? get areaDataModifier => _areaDataModifier;

set areaDataModifier(AreaDataModifier? modifier) {
if (_areaDataModifier != modifier) {
_areaDataModifier = modifier;
Expand All @@ -39,9 +41,11 @@ class MultiSplitViewController extends ChangeNotifier {
Object _areasUpdateHash = Object();

double _flexCount = 0;

double get flexCount => _flexCount;

double _totalFlex = 0;

double get totalFlex => _totalFlex;

/// Applies the current data modifier.
Expand All @@ -57,12 +61,17 @@ class MultiSplitViewController extends ChangeNotifier {
/// Updates the areas.
/// Changes the flex to 1 if the total flex of the areas is 0.
void _updateAreas() {
Set<dynamic> ids = {};

_areasUpdateHash = Object();

_totalFlex = 0;
_flexCount = 0;
int index = 0;
for (Area area in _areas) {
if (!ids.add(area.id)) {
throw StateError('Area with duplicate id.');
}
AreaHelper.setIndex(area: area, index: index);
if (area.flex != null) {
_totalFlex += area.flex!;
Expand Down
4 changes: 1 addition & 3 deletions lib/src/multi_split_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,7 @@ class _MultiSplitViewState extends State<MultiSplitView> {
? HitTestBehavior.opaque
: HitTestBehavior.translucent);
children.add(LayoutId(
key: AreaHelper.keyFrom(area: area),
id: index,
child: ClipRect(child: child)));
key: ValueKey(area.id), id: index, child: ClipRect(child: child)));

//divisor widget
if (index < _controller.areasCount - 1) {
Expand Down

0 comments on commit 70e6659

Please sign in to comment.