Skip to content

Commit

Permalink
bump/v1.0.4 (#19)
Browse files Browse the repository at this point in the history
Merge pull request #19 from insolite-dev/develop
  • Loading branch information
theiskaa authored May 13, 2023
2 parents 7349663 + 6b1cce4 commit 032d929
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 178 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v1.0.4 - 14/05/2023

- Migrated to Flutter 3.10 and Dart 3
- Resolved [#17](https://github.com/insolite-dev/hidable/issues/17)

# v1.0.3 - 03/09/2022

- Resolved [#10](https://github.com/insolite-dev/hidable/issues/10)
Expand Down
4 changes: 0 additions & 4 deletions analysis_options.yaml

This file was deleted.

3 changes: 1 addition & 2 deletions lib/hidable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

library hidable;

export 'src/hidable_widget.dart';
export 'src/hidable_controller_ext.dart';
export 'src/hidable_widget.dart';
45 changes: 28 additions & 17 deletions lib/src/hidable_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,28 @@
import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart';

/// Simple extension to generate [HidableController] from scroll controller instance directly.
extension HidableControllerExt on ScrollController {
static final hidableControllers = <int, HidableController>{};

/// Shortcut way of creating hidable controller
HidableController hidable(double size) {
// If the same instance was created before, we should keep using it.
if (hidableControllers.containsKey(hashCode)) {
return hidableControllers[hashCode]!;
}

return hidableControllers[hashCode] = HidableController(
scrollController: this,
size: size,
);
}
}

/// A custom wrapper for scroll controller.
///
/// It have state notifiers and it's default scroll listener, Which used as default scrolling detection.
/// Implements the main listener mehtod for [ScrollController].
/// And the [sizeNotifier] for providing/updating the hideable status.
class HidableController {
/// The main scroll controller.
ScrollController scrollController;
Expand All @@ -26,21 +45,17 @@ class HidableController {

double li = 0.0, lastOffset = 0.0;

/// The main value notifier of widget's size.
final sizeNotifier = ValueNotifier<double>(1.0);

/// The main value notifier of widget's stickiness.
final stickinessNotifier = ValueNotifier<bool>(false);

/// Adds new state value to stickiness notifier.
///
/// Basically, used to enable/disable stickness of widget.
void setStickinessState(bool state) => stickinessNotifier.value = state;

/// Took size factor from "li" and "size".
double sizeFactor() => 1.0 - (li / size);

/// Default listener that detects movements on the screen, and alerts size's value-notifier.
/// Listener is the main "extenssion" method for the [ScrollController],
/// which calculates the position of scroll and decides wheter it should collapse or
/// show-up the static located widget.
///
/// The caluclation data will be emited to the [sizeNotifier]. Where 0 to 1 is the
/// static-located-widget appearing status. (0 = closed) and (1 = opened).
void listener() {
final p = scrollController.position;

Expand All @@ -49,7 +64,7 @@ class HidableController {
lastOffset = p.pixels;

// If scrolled down, size-notifiers value should be zero.
// Can be imagined as [zero - false] | [one - true].
// Can be imagined as [zero = false] | [one = true].
if (p.axisDirection == AxisDirection.down && p.extentAfter == 0.0) {
if (sizeNotifier.value == 0.0) return;

Expand All @@ -72,9 +87,5 @@ class HidableController {
sizeNotifier.value = sizeFactor();
}

/// Closes size and stickness notifiers.
void close() {
stickinessNotifier.dispose();
sizeNotifier.dispose();
}
void close() => sizeNotifier.dispose();
}
27 changes: 0 additions & 27 deletions lib/src/hidable_controller_ext.dart

This file was deleted.

42 changes: 15 additions & 27 deletions lib/src/hidable_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
//

import 'package:flutter/material.dart';
import 'package:hidable/src/hidable_controller_ext.dart';
import 'package:hidable/src/hidable_controller.dart';

/// ### Widget that can make anything hidable.
/// Hidable is a widget that makes any static located widget hideable while scrolling.
///
/// To Use:
/// Wrap your static located widget with [Hidable],
/// then your widget will support scroll to hide/show feature.
///
/// Note: scroll controller that you give to [Hidable], also must be given to your scrollable widget,
/// It could, [ListView], [GridView], etc.
///
/// #### For more information refer to - [documentation](https://github.com/insolite-dev/hidable#readme)
class Hidable extends StatelessWidget with PreferredSizeWidget {
class Hidable extends StatelessWidget implements PreferredSizeWidget {
/// Child widget, which you want to add scroll-to-hide effect to it.
///
/// It should be static located widget:
Expand Down Expand Up @@ -50,32 +51,19 @@ class Hidable extends StatelessWidget with PreferredSizeWidget {

@override
Widget build(BuildContext context) {
final hidable = controller.hidable(preferredWidgetSize.height);

return ValueListenableBuilder<bool>(
valueListenable: hidable.stickinessNotifier,
builder: (_, isStickinessEnabled, __) {
// If stickiness of hidable was enabled, return card with one factor.
// So, that hidable's movement would be disabled.
if (isStickinessEnabled) return hidableCard(1.0, hidable);
// TODO: implement artificial scroll place to controller it it blocks.

return ValueListenableBuilder<double>(
valueListenable: hidable.sizeNotifier,
builder: (_, height, __) => hidableCard(height, hidable),
);
},
);
}
final hidable = controller.hidable(preferredWidgetSize.height);

// Custom alignment wrapper card of hidable.
// Returns whole card at given factor.
Widget hidableCard(double factor, hidable) {
return Align(
heightFactor: factor,
alignment: const Alignment(0, -1),
child: SizedBox(
height: hidable.size,
child: wOpacity ? Opacity(opacity: factor, child: child) : child,
return ValueListenableBuilder<double>(
valueListenable: hidable.sizeNotifier,
builder: (_, factor, __) => Align(
heightFactor: factor,
alignment: const Alignment(0, -1),
child: SizedBox(
height: hidable.size,
child: wOpacity ? Opacity(opacity: factor, child: child) : child,
),
),
);
}
Expand Down
Loading

0 comments on commit 032d929

Please sign in to comment.