Skip to content

Commit

Permalink
fix: The default method of onReorder in ReorderableListBuilder is now…
Browse files Browse the repository at this point in the history
… supported for normal objects.
  • Loading branch information
mathrunet committed Aug 23, 2023
1 parent bc0d8f7 commit 6a2efad
Showing 1 changed file with 78 additions and 25 deletions.
103 changes: 78 additions & 25 deletions packages/masamune/lib/ui/reorderable_list_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,54 +87,107 @@ class ReorderableListBuilder<T> extends StatefulWidget {
_length = top.length + source.length + bottom.length + insert.length,
_topSourcelength = top.length + source.length + insert.length;

/// Method to define [ReorderableListBuilder.onReorder] when the list data consists of [DynamicMap].
///Method to define [ReorderableListBuilder.onReorder] when the data in the list consists of [M].
///
/// The elements to be reordered are passed to [map], [oldPosition] and [newPosition] are the old and new positions, and [reordered] is the list after the reordering.
/// The elements to be reordered are passed to [data], [oldPosition] and [newPosition] are the old and new positions, and [reordered] is the list after the reordering.
///
/// Specify the callback to be executed after reordering in [onSave]. Specify the key of the element for which the order value is to be entered in [key]. Specify the initial value to be passed to [defaultOrder] if [key] has no value.
/// Specify a callback to [onRetrieve] to retrieve the numerical value of the order.
///
/// リストのデータが[DynamicMap]で構成されている場合の[ReorderableListBuilder.onReorder]を定義するためのメソッド。
/// Specify a callback to execute after reordering in [onUpdate].
///
/// [map]に順番が入れ替わった対象の要素、[oldPosition][newPosition]に新旧の位置、[reordered]に順番が入れ替わった後のリストが渡されます。
/// Specify the initial value to be passed to [defaultOrderValue] if [key] has no value.
///
/// リストのデータが[M]で構成されている場合の[ReorderableListBuilder.onReorder]を定義するためのメソッド。
///
/// [data]に順番が入れ替わった対象の要素、[oldPosition][newPosition]に新旧の位置、[reordered]に順番が入れ替わった後のリストが渡されます。
///
/// [onRetrieve]に順番の数値を取得するためのコールバックを指定します。
///
/// [onSave]に順番を入れ替えた後に実行するコールバックを指定します。[key]には順番の値を記載する要素のキーを指定します。[defaultOrder][key]に値がなかったときに渡す初期値を指定します。
static Future<M> setOrderForDynamicMap<M extends DynamicMap>(
/// [onUpdate]に順番を入れ替えた後に実行するコールバックを指定します。
///
/// [defaultOrderValue][key]に値がなかったときに渡す初期値を指定します。
static Future<M> defaultOnReorder<M>(
int oldPosition,
int newPosition,
M map,
M data,
List<M> reordered, {
FutureOr<void> Function(M data)? onSave,
String key = "order",
double? defaultOrder,
required double Function(M data) onRetrieve,
required FutureOr<void> Function(M data, double order) onUpdate,
double? defaultOrderValue,
}) async {
if (reordered.length <= 1) {
return map;
return data;
}
if (oldPosition < newPosition) {
if (reordered.length <= newPosition) {
map[key] =
defaultOrder ?? DateTime.now().millisecondsSinceEpoch.toDouble();
await onUpdate(
data,
defaultOrderValue ?? DateTime.now().millisecondsSinceEpoch.toDouble(),
);
} else {
map[key] = (reordered[newPosition].get(key, 0.0) +
reordered[newPosition - 2].get(key, 0.0)) /
2.0;
await onUpdate(
data,
(onRetrieve(reordered[newPosition]) +
onRetrieve(reordered[newPosition - 2])) /
2.0,
);
}
} else {
if (newPosition <= 0) {
map[key] = reordered[1].get(key, 0.0) / 2.0;
await onUpdate(
data,
onRetrieve(reordered[1]) / 2.0,
);
} else if (reordered.length - 1 <= newPosition) {
map[key] =
defaultOrder ?? DateTime.now().millisecondsSinceEpoch.toDouble();
await onUpdate(
data,
defaultOrderValue ?? DateTime.now().millisecondsSinceEpoch.toDouble(),
);
} else {
map[key] = (reordered[newPosition + 1].get(key, 0.0) +
reordered[newPosition - 1].get(key, 0.0)) /
2.0;
await onUpdate(
data,
(onRetrieve(reordered[newPosition + 1]) +
onRetrieve(reordered[newPosition - 1])) /
2.0,
);
}
}
await onSave?.call(map);
return map;
return data;
}

/// Method to define [ReorderableListBuilder.onReorder] when the list data consists of [DynamicMap].
///
/// The elements to be reordered are passed to [map], [oldPosition] and [newPosition] are the old and new positions, and [reordered] is the list after the reordering.
///
/// Specify the callback to be executed after reordering in [onSaved]. Specify the key of the element for which the order value is to be entered in [key]. Specify the initial value to be passed to [defaultOrderValue] if [key] has no value.
///
/// リストのデータが[DynamicMap]で構成されている場合の[ReorderableListBuilder.onReorder]を定義するためのメソッド。
///
/// [map]に順番が入れ替わった対象の要素、[oldPosition][newPosition]に新旧の位置、[reordered]に順番が入れ替わった後のリストが渡されます。
///
/// [onSaved]に順番を入れ替えた後に実行するコールバックを指定します。[key]には順番の値を記載する要素のキーを指定します。[defaultOrderValue][key]に値がなかったときに渡す初期値を指定します。
static Future<M> defaultOnReorderForDynamicMap<M extends DynamicMap>(
int oldPosition,
int newPosition,
M map,
List<M> reordered, {
FutureOr<void> Function(M data)? onSaved,
String key = "order",
double? defaultOrderValue,
}) =>
defaultOnReorder(
oldPosition,
newPosition,
map,
reordered,
onRetrieve: (map) => map.get(key, 0.0),
onUpdate: (map, order) async {
map[key] = order;
await onSaved?.call(map);
},
defaultOrderValue: defaultOrderValue,
);

/// Scroll direction.
///
/// スクロールの方向。
Expand Down

0 comments on commit 6a2efad

Please sign in to comment.