From cb8e273776bd5acb674fbebe5584425b5a8dbf93 Mon Sep 17 00:00:00 2001 From: zhangwanping Date: Sun, 26 Sep 2021 15:13:33 +0800 Subject: [PATCH] feat: add resizeBegin && resizeEnd Functions --- lib/resizable_widget.dart | 2 +- lib/src/resizable_widget.dart | 16 +++++++++----- lib/src/resizable_widget_args_info.dart | 6 ++++- lib/src/resizable_widget_child_data.dart | 0 lib/src/resizable_widget_controller.dart | 15 +++++++++++-- lib/src/resizable_widget_model.dart | 28 +++++++++++++++++------- lib/src/separator.dart | 2 ++ lib/src/separator_args_info.dart | 0 lib/src/separator_controller.dart | 8 +++++++ lib/src/widget_size_info.dart | 0 10 files changed, 60 insertions(+), 17 deletions(-) mode change 100644 => 100755 lib/resizable_widget.dart mode change 100644 => 100755 lib/src/resizable_widget.dart mode change 100644 => 100755 lib/src/resizable_widget_args_info.dart mode change 100644 => 100755 lib/src/resizable_widget_child_data.dart mode change 100644 => 100755 lib/src/resizable_widget_controller.dart mode change 100644 => 100755 lib/src/resizable_widget_model.dart mode change 100644 => 100755 lib/src/separator.dart mode change 100644 => 100755 lib/src/separator_args_info.dart mode change 100644 => 100755 lib/src/separator_controller.dart mode change 100644 => 100755 lib/src/widget_size_info.dart diff --git a/lib/resizable_widget.dart b/lib/resizable_widget.dart old mode 100644 new mode 100755 index a26faae..8295c28 --- a/lib/resizable_widget.dart +++ b/lib/resizable_widget.dart @@ -1,5 +1,5 @@ /// Provide [ResizableWidget]. library resizable_widget; -export 'src/resizable_widget.dart' show ResizableWidget, OnResizedFunc; +export 'src/resizable_widget.dart' show ResizableWidget, OnResizeBeginFunc, OnResizedFunc, OnResizeEndFunc; export 'src/widget_size_info.dart' show WidgetSizeInfo; diff --git a/lib/src/resizable_widget.dart b/lib/src/resizable_widget.dart old mode 100644 new mode 100755 index c1ecc43..9e39ae1 --- a/lib/src/resizable_widget.dart +++ b/lib/src/resizable_widget.dart @@ -1,5 +1,5 @@ import 'package:flutter/material.dart'; -import 'package:resizable_widget/src/resizable_widget_args_info.dart'; +import 'resizable_widget_args_info.dart'; import 'resizable_widget_child_data.dart'; import 'resizable_widget_controller.dart'; import 'separator.dart'; @@ -7,6 +7,8 @@ import 'widget_size_info.dart'; /// The callback argument type of [ResizableWidget.onResized]. typedef OnResizedFunc = void Function(List infoList); +typedef OnResizeBeginFunc = void Function(int separatorIndex, DragDownDetails details); +typedef OnResizeEndFunc = void Function(int separatorIndex, DragEndDetails details); /// Holds resizable widgets as children. /// Users can resize the internal widgets by dragging. @@ -48,23 +50,27 @@ class ResizableWidget extends StatefulWidget { /// Note that [onResized] is called every frame when resizing [children]. final OnResizedFunc? onResized; + final OnResizeBeginFunc? onResizeBegin; + + final OnResizeEndFunc? onResizeEnd; + /// Creates [ResizableWidget]. ResizableWidget({ Key? key, required this.children, this.percentages, - @Deprecated('Use [isHorizontalSeparator] instead') - this.isColumnChildren = false, + @Deprecated('Use [isHorizontalSeparator] instead') this.isColumnChildren = false, this.isHorizontalSeparator = false, this.isDisabledSmartHide = false, this.separatorSize = 4, this.separatorColor = Colors.white12, this.onResized, + this.onResizeBegin, + this.onResizeEnd, }) : super(key: key) { assert(children.isNotEmpty); assert(percentages == null || percentages!.length == children.length); - assert(percentages == null || - percentages!.reduce((value, element) => value + element) == 1); + assert(percentages == null || percentages!.reduce((value, element) => value + element) == 1); } @override diff --git a/lib/src/resizable_widget_args_info.dart b/lib/src/resizable_widget_args_info.dart old mode 100644 new mode 100755 index 2e43b6b..696d545 --- a/lib/src/resizable_widget_args_info.dart +++ b/lib/src/resizable_widget_args_info.dart @@ -8,7 +8,9 @@ class ResizableWidgetArgsInfo { final bool isDisabledSmartHide; final double separatorSize; final Color separatorColor; + final OnResizeBeginFunc? onResizeBegin; final OnResizedFunc? onResized; + final OnResizeEndFunc? onResizeEnd; ResizableWidgetArgsInfo(ResizableWidget widget) : children = widget.children, @@ -20,5 +22,7 @@ class ResizableWidgetArgsInfo { isDisabledSmartHide = widget.isDisabledSmartHide, separatorSize = widget.separatorSize, separatorColor = widget.separatorColor, - onResized = widget.onResized; + onResized = widget.onResized, + onResizeBegin = widget.onResizeBegin, + onResizeEnd = widget.onResizeEnd; } diff --git a/lib/src/resizable_widget_child_data.dart b/lib/src/resizable_widget_child_data.dart old mode 100644 new mode 100755 diff --git a/lib/src/resizable_widget_controller.dart b/lib/src/resizable_widget_controller.dart old mode 100644 new mode 100755 index f0f86a3..5078aea --- a/lib/src/resizable_widget_controller.dart +++ b/lib/src/resizable_widget_controller.dart @@ -11,8 +11,7 @@ class ResizableWidgetController { final ResizableWidgetModel _model; List get children => _model.children; - ResizableWidgetController(ResizableWidgetArgsInfo info) - : _model = ResizableWidgetModel(info) { + ResizableWidgetController(ResizableWidgetArgsInfo info) : _model = ResizableWidgetModel(info) { _model.init(_separatorFactory); } @@ -21,6 +20,12 @@ class ResizableWidgetController { _model.callOnResized(); } + void resizeStart(int separatorIndex, DragDownDetails details) { + _model.resizeStart(separatorIndex, details); + eventStream.add(this); + _model.callResizedBegin(separatorIndex, details); + } + void resize(int separatorIndex, Offset offset) { _model.resize(separatorIndex, offset); @@ -28,6 +33,12 @@ class ResizableWidgetController { _model.callOnResized(); } + void resizeEnd(int separatorIndex, DragEndDetails details) { + _model.resizeEnd(separatorIndex, details); + eventStream.add(this); + _model.callResizedEnd(separatorIndex, details); + } + void tryHideOrShow(int separatorIndex) { final result = _model.tryHideOrShow(separatorIndex); diff --git a/lib/src/resizable_widget_model.dart b/lib/src/resizable_widget_model.dart old mode 100644 new mode 100755 index 3971178..9845d8e --- a/lib/src/resizable_widget_model.dart +++ b/lib/src/resizable_widget_model.dart @@ -20,11 +20,9 @@ class ResizableWidgetModel { void init(SeparatorFactory separatorFactory) { final originalChildren = _info.children; final size = originalChildren.length; - final originalPercentages = - _info.percentages ?? List.filled(size, 1 / size); + final originalPercentages = _info.percentages ?? List.filled(size, 1 / size); for (var i = 0; i < size - 1; i++) { - children.add(ResizableWidgetChildData( - originalChildren[i], originalPercentages[i])); + children.add(ResizableWidgetChildData(originalChildren[i], originalPercentages[i])); children.add(ResizableWidgetChildData( separatorFactory.call(SeparatorArgsBasicInfo( 2 * i + 1, @@ -62,6 +60,10 @@ class ResizableWidgetModel { } } + void resizeStart(int separatorIndex, DragDownDetails details) { + _info.onResizeBegin?.call(separatorIndex, details); + } + void resize(int separatorIndex, Offset offset) { final leftSize = _resizeImpl(separatorIndex - 1, offset); final rightSize = _resizeImpl(separatorIndex + 1, offset * (-1)); @@ -92,11 +94,21 @@ class ResizableWidgetModel { } } + void resizeEnd(int separatorIndex, DragEndDetails details) { + _info.onResizeEnd?.call(separatorIndex, details); + } + + void callResizedBegin(int separatorIndex, DragDownDetails details) { + _info.onResizeBegin?.call(separatorIndex, details); + } + void callOnResized() { - _info.onResized?.call(children - .where((x) => x.widget is! Separator) - .map((x) => WidgetSizeInfo(x.size!, x.percentage!)) - .toList()); + _info.onResized?.call( + children.where((x) => x.widget is! Separator).map((x) => WidgetSizeInfo(x.size!, x.percentage!)).toList()); + } + + void callResizedEnd(int separatorIndex, DragEndDetails details) { + _info.onResizeEnd?.call(separatorIndex, details); } bool tryHideOrShow(int separatorIndex) { diff --git a/lib/src/separator.dart b/lib/src/separator.dart old mode 100644 new mode 100755 index 210d465..119eaf3 --- a/lib/src/separator.dart +++ b/lib/src/separator.dart @@ -39,7 +39,9 @@ class _SeparatorState extends State { height: _info.isHorizontalSeparator ? _info.size : double.infinity, ), ), + onPanDown: (details) => _controller.onPanStart(details), onPanUpdate: (details) => _controller.onPanUpdate(details, context), + onPanEnd: (details) => _controller.onPanEnd(details), onDoubleTap: () => _controller.onDoubleTap(), ); } diff --git a/lib/src/separator_args_info.dart b/lib/src/separator_args_info.dart old mode 100644 new mode 100755 diff --git a/lib/src/separator_controller.dart b/lib/src/separator_controller.dart old mode 100644 new mode 100755 index 8dd0579..f976d22 --- a/lib/src/separator_controller.dart +++ b/lib/src/separator_controller.dart @@ -7,10 +7,18 @@ class SeparatorController { const SeparatorController(this._index, this._parentController); + void onPanStart(DragDownDetails details) { + _parentController.resizeStart(_index, details); + } + void onPanUpdate(DragUpdateDetails details, BuildContext context) { _parentController.resize(_index, details.delta); } + void onPanEnd(DragEndDetails details) { + _parentController.resizeEnd(_index, details); + } + void onDoubleTap() { _parentController.tryHideOrShow(_index); } diff --git a/lib/src/widget_size_info.dart b/lib/src/widget_size_info.dart old mode 100644 new mode 100755