Skip to content

Commit

Permalink
Refactor: Replace Platform (dart:io) with TargetPlatform
Browse files Browse the repository at this point in the history
  • Loading branch information
AmosHuKe committed Sep 6, 2024
1 parent d2990e3 commit 8b00cf9
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 13 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
> [!IMPORTANT]
> See the [Migration Guide](guides/migration_guide.md) for the details of breaking changes between versions.
## 3.0.6 (Unreleased)
## 3.0.6

### Improvements

- Handling when the platform has no available sensors.
- Replace `Platform` (dart:io) with `TargetPlatform` (flutter/foundation.dart). ([#18](https://github.com/fluttercandies/flutter_tilt/pull/18))
- Handling when the platform has no available sensors. ([#18](https://github.com/fluttercandies/flutter_tilt/pull/18))

## 3.0.5

Expand Down
15 changes: 8 additions & 7 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:io';
import 'dart:math';

import 'package:flutter/foundation.dart';
import 'package:flutter/foundation.dart'
show kIsWeb, kIsWasm, defaultTargetPlatform, TargetPlatform;
import 'package:flutter/widgets.dart';

import 'config/tilt_config.dart';
Expand All @@ -13,11 +13,12 @@ class Utils {
///
/// @return [bool] true: 传感器支持 false: 传感器不支持
static bool sensorsPlatformSupport() {
bool support = false;
if (kIsWeb) return support = true;
if (Platform.isAndroid) support = true;
if (Platform.isIOS) support = true;
return support;
if (kIsWeb || kIsWasm) return true;
return switch (defaultTargetPlatform) {
TargetPlatform.android => true,
TargetPlatform.iOS => true,
_ => false
};
}

/// 区域中心定位
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description: Easily apply tilt parallax hover effects for Flutter, which support
# https://semver.org/spec/v2.0.0-rc.1.html
# https://dart.dev/tools/pub/versioning#semantic-versions
# https://dart.dev/tools/pub/dependencies#version-constraints
version: 3.0.5
version: 3.0.6
homepage: https://amoshuke.github.io/flutter_tilt_book
repository: https://github.com/fluttercandies/flutter_tilt
issue_tracker: https://github.com/fluttercandies/flutter_tilt/issues
Expand Down
1 change: 1 addition & 0 deletions test/tilt_parallax_widget/tilt_parallax_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ void main() {
),
),
);

await tester.pumpAndSettle();
final Offset outerLocation = tester.getCenter(outerFinder);
final Offset innerLocation = tester.getCenter(innerFinder);
Expand Down
2 changes: 1 addition & 1 deletion test/tilt_parallax_widget/tilt_parallax_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class TiltParallaxWidget extends StatelessWidget {
border: border,
borderRadius: borderRadius,
clipBehavior: clipBehavior,
tiltConfig: tiltConfig,
tiltConfig: tiltConfig.copyWith(enableGestureSensors: false),
lightConfig: lightConfig,
shadowConfig: shadowConfig,
onGestureMove: onGestureMove,
Expand Down
3 changes: 3 additions & 0 deletions test/tilt_widget/tilt_config_tilt_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -585,6 +586,7 @@ void main() {
TiltDataModel? tiltDataTest;
GesturesType? gesturesTypeTest;

debugDefaultTargetPlatformOverride = TargetPlatform.windows;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
Expand Down Expand Up @@ -614,6 +616,7 @@ void main() {
),
),
);
debugDefaultTargetPlatformOverride = null;

/// onVerticalDragUpdate
await tester.timedDrag(
Expand Down
4 changes: 4 additions & 0 deletions test/tilt_widget/tilt_gestures_drag_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_tilt/flutter_tilt.dart';
Expand All @@ -9,6 +10,8 @@ void main() {
final Finder scrollFinder = find.byKey(const Key('scroll'));
final ScrollController scrollController = ScrollController();

debugDefaultTargetPlatformOverride = TargetPlatform.windows;

/// 回调赋值
await tester.pumpWidget(
MaterialApp(
Expand All @@ -27,6 +30,7 @@ void main() {
),
),
);
debugDefaultTargetPlatformOverride = null;

/// onVerticalDragUpdate
await tester.timedDrag(
Expand Down
2 changes: 1 addition & 1 deletion test/tilt_widget/tilt_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TiltWidget extends StatelessWidget {
border: border,
borderRadius: borderRadius,
clipBehavior: clipBehavior,
tiltConfig: tiltConfig,
tiltConfig: tiltConfig.copyWith(enableGestureSensors: false),
lightConfig: lightConfig,
shadowConfig: shadowConfig,
onGestureMove: onGestureMove,
Expand Down
14 changes: 13 additions & 1 deletion test/utils_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_tilt/flutter_tilt.dart';
import 'package:flutter_tilt/src/utils.dart';
Expand All @@ -16,7 +17,18 @@ void main() {
const constraintsPosition = Utils.constraintsPosition;

test('sensorsPlatformSupport', () {
expect(sensorsPlatformSupport(), false);
for (final TargetPlatform testTargetPlatform in TargetPlatform.values) {
debugDefaultTargetPlatformOverride = testTargetPlatform;
switch (testTargetPlatform) {
case TargetPlatform.android:
expect(sensorsPlatformSupport(), true);
case TargetPlatform.iOS:
expect(sensorsPlatformSupport(), true);
case _:
expect(sensorsPlatformSupport(), false);
}
debugDefaultTargetPlatformOverride = null;
}
});

test('centerPosition', () {
Expand Down

0 comments on commit 8b00cf9

Please sign in to comment.