Skip to content

Commit

Permalink
Refactor (#95)
Browse files Browse the repository at this point in the history
To prepare for #89.
  • Loading branch information
fujidaiti authored Apr 18, 2024
1 parent 1170461 commit 57dde8b
Show file tree
Hide file tree
Showing 23 changed files with 487 additions and 474 deletions.
1 change: 1 addition & 0 deletions package/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ linter:
flutter_style_todos: false
cascade_invocations: false
join_return_with_assignment: false
prefer_relative_imports: true
6 changes: 3 additions & 3 deletions package/lib/smooth_sheets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ library smooth_sheets;

export 'src/draggable/draggable_sheet.dart';
export 'src/draggable/sheet_draggable.dart';
export 'src/foundation/animation.dart';
export 'src/foundation/activities.dart';
export 'src/foundation/animations.dart';
export 'src/foundation/framework.dart';
export 'src/foundation/keyboard_dismissible.dart';
export 'src/foundation/notifications.dart';
export 'src/foundation/sheet_activity.dart';
export 'src/foundation/physics.dart';
export 'src/foundation/sheet_content_scaffold.dart';
export 'src/foundation/sheet_controller.dart' hide SheetControllerScope;
export 'src/foundation/sheet_extent.dart';
export 'src/foundation/sheet_physics.dart';
export 'src/foundation/theme.dart';
export 'src/modal/cupertino.dart';
export 'src/modal/modal_sheet.dart';
Expand Down
142 changes: 102 additions & 40 deletions package/lib/src/draggable/draggable_sheet.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import 'package:flutter/widgets.dart';
import 'package:smooth_sheets/smooth_sheets.dart';
import 'package:smooth_sheets/src/foundation/sized_content_sheet.dart';

import '../foundation/activities.dart';
import '../foundation/framework.dart';
import '../foundation/keyboard_dismissible.dart';
import '../foundation/physics.dart';
import '../foundation/sheet_controller.dart';
import '../foundation/sheet_extent.dart';
import '../foundation/theme.dart';
import '../scrollable/scrollable_sheet.dart';
import 'sheet_draggable.dart';

/// A sheet that can be dragged.
///
/// Note that this widget does not work with scrollable widgets.
/// Instead, use [ScrollableSheet] for this usecase.
class DraggableSheet extends SizedContentSheet {
class DraggableSheet extends StatelessWidget {
/// Creates a sheet that can be dragged.
///
/// The maximum height will be equal to the [child]'s height.
Expand All @@ -22,79 +30,133 @@ class DraggableSheet extends SizedContentSheet {
const DraggableSheet({
super.key,
this.hitTestBehavior = HitTestBehavior.translucent,
super.keyboardDismissBehavior,
super.initialExtent,
super.minExtent,
super.maxExtent,
super.physics,
super.controller,
required super.child,
this.keyboardDismissBehavior,
this.initialExtent = const Extent.proportional(1),
this.minExtent = const Extent.proportional(1),
this.maxExtent = const Extent.proportional(1),
this.physics = const StretchingSheetPhysics(
parent: SnappingSheetPhysics(),
),
required this.child,
this.controller,
});

/// The strategy to dismiss the on-screen keyboard when the sheet is dragged.
final SheetKeyboardDismissBehavior? keyboardDismissBehavior;

/// {@macro SizedContentSheetExtent.initialExtent}
final Extent initialExtent;

/// {@macro SheetExtent.minExtent}
final Extent minExtent;

/// {@macro SheetExtent.maxExtent}
final Extent maxExtent;

/// {@macro SheetExtent.physics}
final SheetPhysics physics;

/// An object that can be used to control and observe the sheet height.
final SheetController? controller;

/// The content of the sheet.
final Widget child;

/// How to behave during hit testing.
///
/// This value will be passed to the constructor of internal [SheetDraggable].
final HitTestBehavior hitTestBehavior;

@override
SizedContentSheetState<SizedContentSheet> createState() {
return _DraggableSheetState();
}
}
Widget build(BuildContext context) {
final theme = SheetTheme.maybeOf(context);
final keyboardDismissBehavior =
this.keyboardDismissBehavior ?? theme?.keyboardDismissBehavior;

class _DraggableSheetState extends SizedContentSheetState<DraggableSheet> {
@override
SheetExtentFactory createExtentFactory() {
return DraggableSheetExtentFactory(
initialExtent: widget.initialExtent,
minExtent: widget.minExtent,
maxExtent: widget.maxExtent,
physics: widget.physics,
Widget result = SheetContainer(
controller: controller,
config: DraggableSheetExtentConfig(
initialExtent: initialExtent,
minExtent: minExtent,
maxExtent: maxExtent,
physics: physics,
),
child: SheetDraggable(
behavior: hitTestBehavior,
child: child,
),
);
}

@override
Widget buildContent(BuildContext context) {
return SheetDraggable(
behavior: widget.hitTestBehavior,
child: super.buildContent(context),
);
if (keyboardDismissBehavior != null) {
result = SheetKeyboardDismissible(
dismissBehavior: keyboardDismissBehavior,
child: result,
);
}

return result;
}
}

/// Factory of [DraggableSheetExtent].
class DraggableSheetExtentFactory extends SizedContentSheetExtentFactory {
const DraggableSheetExtentFactory({
required super.initialExtent,
required super.minExtent,
required super.maxExtent,
required super.physics,
/// A configuration of a [DraggableSheetExtent].
class DraggableSheetExtentConfig extends SheetExtentConfig {
const DraggableSheetExtentConfig({
required this.initialExtent,
required this.minExtent,
required this.maxExtent,
required this.physics,
});

/// {@macro DraggableSheetExtent.initialExtent}
final Extent initialExtent;

/// {@macro SheetExtent.minExtent}
final Extent minExtent;

/// {@macro SheetExtent.maxExtent}
final Extent maxExtent;

/// {@macro SheetExtent.physics}
final SheetPhysics physics;

@override
bool shouldRebuild(BuildContext context, SheetExtent oldExtent) {
return oldExtent is! DraggableSheetExtent ||
oldExtent.minExtent != minExtent ||
oldExtent.maxExtent != maxExtent ||
oldExtent.initialExtent != initialExtent ||
oldExtent.physics != physics;
}

@override
SheetExtent create({required SheetContext context}) {
SheetExtent build(BuildContext context, SheetContext sheetContext) {
return DraggableSheetExtent(
context: sheetContext,
initialExtent: initialExtent,
minExtent: minExtent,
maxExtent: maxExtent,
physics: physics,
context: context,
);
}
}

/// [SheetExtent] for a [DraggableSheet].
class DraggableSheetExtent extends SizedContentSheetExtent {
class DraggableSheetExtent extends SheetExtent {
DraggableSheetExtent({
required super.context,
required super.physics,
required super.minExtent,
required super.maxExtent,
required super.initialExtent,
required this.initialExtent,
}) {
goIdle();
}

/// {@template DraggableSheetExtent.initialExtent}
/// The initial extent of the sheet when it is first shown.
/// {@endtemplate}
final Extent initialExtent;

@override
void goIdle() {
beginActivity(_IdleDraggableSheetActivity(
Expand Down
9 changes: 5 additions & 4 deletions package/lib/src/draggable/sheet_draggable.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/widgets.dart';
import 'package:smooth_sheets/src/draggable/draggable_sheet.dart';
import 'package:smooth_sheets/src/foundation/sheet_activity.dart';
import 'package:smooth_sheets/src/foundation/sheet_extent.dart';
import 'package:smooth_sheets/src/scrollable/scrollable_sheet.dart';

import '../foundation/activities.dart';
import '../foundation/sheet_extent.dart';
import '../scrollable/scrollable_sheet.dart';
import 'draggable_sheet.dart';

/// A widget that makes its child as a drag-handle for a sheet.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import 'dart:math';
import 'package:flutter/gestures.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/widgets.dart';
import 'package:smooth_sheets/smooth_sheets.dart';
import 'package:smooth_sheets/src/foundation/sheet_status.dart';
import 'notifications.dart';
import 'sheet_extent.dart';
import 'sheet_status.dart';

abstract class SheetActivity extends ChangeNotifier {
bool _mounted = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/animation.dart';
import 'package:smooth_sheets/src/foundation/sheet_controller.dart';
import 'package:smooth_sheets/src/foundation/sheet_extent.dart';
import 'sheet_controller.dart';
import 'sheet_extent.dart';

class ExtentDrivenAnimation extends Animation<double> {
ExtentDrivenAnimation({
Expand Down
10 changes: 5 additions & 5 deletions package/lib/src/foundation/framework.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:smooth_sheets/src/foundation/sheet_controller.dart';
import 'package:smooth_sheets/src/foundation/sheet_extent.dart';
import 'sheet_controller.dart';
import 'sheet_extent.dart';

class SheetContainer extends StatelessWidget {
const SheetContainer({
super.key,
this.controller,
this.onExtentChanged,
required this.factory,
required this.config,
required this.child,
});

final SheetController? controller;
final ValueChanged<SheetExtent?>? onExtentChanged;
final SheetExtentFactory factory;
final SheetExtentConfig config;
final Widget child;

@override
Widget build(BuildContext context) {
return SheetExtentScope(
factory: factory,
config: config,
controller: controller ?? SheetControllerScope.maybeOf(context),
onExtentChanged: onExtentChanged,
child: Builder(
Expand Down
4 changes: 2 additions & 2 deletions package/lib/src/foundation/keyboard_dismissible.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/widgets.dart';
import 'package:smooth_sheets/src/draggable/draggable_sheet.dart';
import 'package:smooth_sheets/src/foundation/notifications.dart';
import '../draggable/draggable_sheet.dart';
import 'notifications.dart';

/// A widget that dismisses the on-screen keyboard when the user
/// drags the sheet below this widget.
Expand Down
5 changes: 3 additions & 2 deletions package/lib/src/foundation/notifications.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/widgets.dart';
import 'package:smooth_sheets/src/foundation/sheet_extent.dart';
import 'package:smooth_sheets/src/foundation/sheet_physics.dart';

import 'physics.dart';
import 'sheet_extent.dart';

/// A [Notification] that is dispatched when the sheet extent changes.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import 'dart:ui';
import 'package:collection/collection.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/widgets.dart';
import 'package:smooth_sheets/src/foundation/sheet_extent.dart';
import 'package:smooth_sheets/src/internal/double_utils.dart';

import '../internal/double_utils.dart';
import 'sheet_extent.dart';

const _minSettlingDuration = Duration(milliseconds: 160);
const _defaultSettlingSpeed = 600.0; // logical pixels per second
Expand Down
5 changes: 4 additions & 1 deletion package/lib/src/foundation/sheet_content_scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:smooth_sheets/smooth_sheets.dart';

import '../draggable/sheet_draggable.dart';
import 'framework.dart';
import 'sheet_extent.dart';

class SheetContentScaffold extends StatelessWidget {
const SheetContentScaffold({
Expand Down
2 changes: 1 addition & 1 deletion package/lib/src/foundation/sheet_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/widgets.dart';
import 'package:meta/meta.dart';
import 'package:smooth_sheets/src/foundation/sheet_extent.dart';
import 'sheet_extent.dart';

class SheetController extends ChangeNotifier
implements ValueListenable<double?> {
Expand Down
Loading

0 comments on commit 57dde8b

Please sign in to comment.