Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Replace Platform (dart:io) with TargetPlatform #269

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions example/lib/showcase/ai_playlist_generator.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart'
show TargetPlatform, defaultTargetPlatform;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:go_router/go_router.dart';
import 'package:smooth_sheets/smooth_sheets.dart';

void main() {
// Make the system navigation bar transparent on Android.
if (Platform.isAndroid) {
if (defaultTargetPlatform == TargetPlatform.android) {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setEnabledSystemUIMode(
SystemUiMode.edgeToEdge,
Expand Down
14 changes: 8 additions & 6 deletions lib/src/foundation/sheet_physics.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'dart:io';
import 'dart:math';

import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart'
show TargetPlatform, defaultTargetPlatform;
import 'package:flutter/gestures.dart';
import 'package:flutter/widgets.dart';

Expand Down Expand Up @@ -36,11 +37,12 @@ abstract class SheetPhysics {
/// the first time or after each time the drag motion stopped.
///
/// If null, no minimum threshold is enforced.
double? get dragStartDistanceMotionThreshold {
return Platform.isIOS
? const BouncingScrollPhysics().dragStartDistanceMotionThreshold
: null;
}
double? get dragStartDistanceMotionThreshold =>
switch (defaultTargetPlatform) {
TargetPlatform.iOS =>
const BouncingScrollPhysics().dragStartDistanceMotionThreshold,
_ => null,
};

/// Create a copy of this object appending the [ancestor] to
/// the physics chain, much like [ScrollPhysics.applyTo].
Expand Down
18 changes: 18 additions & 0 deletions test/foundation/physics_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// ignore_for_file: lines_longer_than_80_chars
import 'package:flutter/foundation.dart'
show debugDefaultTargetPlatformOverride;
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:smooth_sheets/smooth_sheets.dart';
Expand Down Expand Up @@ -71,6 +73,22 @@ void main() {
physicsUnderTest = const _SheetPhysicsWithDefaultConfiguration();
});

test('dragStartDistanceMotionThreshold for different platforms', () {
for (final testTargetPlatform in TargetPlatform.values) {
debugDefaultTargetPlatformOverride = testTargetPlatform;
switch (testTargetPlatform) {
case TargetPlatform.iOS:
expect(
physicsUnderTest.dragStartDistanceMotionThreshold,
const BouncingScrollPhysics().dragStartDistanceMotionThreshold,
);
case _:
expect(physicsUnderTest.dragStartDistanceMotionThreshold, null);
}
debugDefaultTargetPlatformOverride = null;
}
});

test('does not allow over/under dragging', () {
expect(
physicsUnderTest.computeOverflow(10, _positionAtTopEdge),
Expand Down
Loading