Skip to content

Commit

Permalink
Refactor: Replace Platform (dart:io) with TargetPlatform (#269)
Browse files Browse the repository at this point in the history
## Description

Support WASM.

![image](https://github.com/user-attachments/assets/590e28bc-133d-4810-a4fd-54f4228b9e65)


## Summary (check all that apply)

- [x] Modified / added code
- [x] Modified / added tests
- [x] Modified / added examples
- [ ] Modified / added others (pubspec.yaml, workflows, etc...)
- [ ] Updated README
- [ ] Contains breaking changes
  - [ ] Created / updated migration guide
- [ ] Incremented version number
  - [ ] Updated CHANGELOG
  • Loading branch information
AmosHuKe authored Oct 19, 2024
1 parent b731d88 commit 1166df3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
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

0 comments on commit 1166df3

Please sign in to comment.