-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[adaptive_style] add first implementation
- Loading branch information
Francesco Iapicca
committed
Apr 23, 2024
1 parent
d2ca9d2
commit 0539209
Showing
9 changed files
with
234 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
library adaptive_style; | ||
|
||
export 'src/adaptive_size_builder.dart'; | ||
export 'src/adaptive_size_provider.dart'; | ||
export 'src/adaptive_size.dart'; | ||
export 'src/device_size.dart'; | ||
export 'src/extension.dart'; | ||
export 'src/inherited_adaptive_size.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:yak_flutter/yak_flutter.dart'; | ||
import 'extension.dart'; | ||
import 'device_size.dart'; | ||
|
||
typedef AdaptiveSizeNotifier = RestrictedNotifier<AdaptiveSizeData>; | ||
|
||
final class AdaptiveSizeData { | ||
const AdaptiveSizeData({ | ||
required this.realScreenSize, | ||
required this.scaledSize, | ||
required this.scale, | ||
required this.deviceSizes, | ||
required this.mostSimilarDeviceSize, | ||
}); | ||
|
||
factory AdaptiveSizeData.fromSize( | ||
Size size, { | ||
List<DeviceSize> deviceSizes = DeviceSize.values, | ||
}) { | ||
final mostSimilarDeviceSize = deviceSizes.mostSimilarTo(size); | ||
final scale = mostSimilarDeviceSize.size.closestDimentionScale(size); | ||
return AdaptiveSizeData( | ||
deviceSizes: deviceSizes, | ||
realScreenSize: size, | ||
mostSimilarDeviceSize: mostSimilarDeviceSize, | ||
scale: scale, | ||
scaledSize: size * scale, | ||
); | ||
} | ||
|
||
final Size realScreenSize; | ||
final Size scaledSize; | ||
final double scale; | ||
final List<DeviceSize> deviceSizes; | ||
final DeviceSize mostSimilarDeviceSize; | ||
|
||
@override | ||
bool operator ==(Object other) => | ||
other is AdaptiveSizeData && other.hashCode == hashCode; | ||
|
||
@override | ||
int get hashCode => Object.hash(realScreenSize, deviceSizes); | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/adaptive_style/lib/src/adaptive_size_builder.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'package:flutter/widgets.dart'; | ||
|
||
import 'adaptive_size.dart'; | ||
import 'inherited_adaptive_size.dart'; | ||
|
||
typedef AdaptiveSizeBuild = Widget Function(BuildContext, AdaptiveSizeData); | ||
|
||
class AdaptiveSizeBuilder extends StatelessWidget { | ||
final AdaptiveSizeBuild builder; | ||
const AdaptiveSizeBuilder({ | ||
super.key, | ||
required this.builder, | ||
}); | ||
|
||
@override | ||
Widget build(context) { | ||
final adaptiveSize = InheritedAdaptiveSize.maybeOf(context); | ||
if (adaptiveSize == null) { | ||
throw Exception('InheritedAdaptiveSize not found in BuildContext'); | ||
} | ||
|
||
return ValueListenableBuilder( | ||
valueListenable: adaptiveSize as ValueNotifier<AdaptiveSizeData>, | ||
builder: (context, value, _) => builder(context, value), | ||
); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
packages/adaptive_style/lib/src/adaptive_size_provider.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter/widgets.dart'; | ||
import 'package:yak_flutter/yak_flutter.dart'; | ||
|
||
import 'adaptive_size.dart'; | ||
import 'device_size.dart'; | ||
import 'inherited_adaptive_size.dart'; | ||
|
||
class AdaptiveSizeProvider extends StatefulWidget { | ||
final Widget child; | ||
final List<DeviceSize> deviceSizes; | ||
const AdaptiveSizeProvider({ | ||
super.key, | ||
required this.child, | ||
this.deviceSizes = DeviceSize.values, | ||
}); | ||
|
||
@override | ||
State<AdaptiveSizeProvider> createState() => _AdaptiveSizeProvidertState(); | ||
} | ||
|
||
class _AdaptiveSizeProvidertState extends State<AdaptiveSizeProvider> { | ||
final _initialized = Completer<bool>(); | ||
late final ValueNotifier<AdaptiveSizeData> _notifier; | ||
|
||
@override | ||
void didChangeDependencies() { | ||
if (!_initialized.isCompleted) { | ||
_initialize(context); | ||
return; | ||
} | ||
_update(context); | ||
super.didChangeDependencies(); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_notifier.dispose(); | ||
super.dispose(); | ||
} | ||
|
||
void _update(BuildContext context) { | ||
final size = MediaQuery.sizeOf(context); | ||
if (size == _notifier.value.realScreenSize && | ||
widget.deviceSizes == _notifier.value.deviceSizes) { | ||
return; | ||
} | ||
|
||
_notifier.value = AdaptiveSizeData.fromSize( | ||
size, | ||
deviceSizes: widget.deviceSizes, | ||
); | ||
} | ||
|
||
void _initialize(BuildContext size) { | ||
final adaptiveSize = AdaptiveSizeData.fromSize( | ||
MediaQuery.sizeOf(context), | ||
deviceSizes: widget.deviceSizes, | ||
); | ||
_notifier = ValueNotifier<AdaptiveSizeData>(adaptiveSize); | ||
_initialized.complete(true); | ||
} | ||
|
||
@override | ||
Widget build(context) => InheritedAdaptiveSize( | ||
notifier: RestrictedNotifier(_notifier), | ||
child: widget.child, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/adaptive_style/lib/src/inherited_adaptive_size.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:yak_flutter/yak_flutter.dart'; | ||
|
||
import 'adaptive_size.dart'; | ||
|
||
class InheritedAdaptiveSize | ||
extends InheritedRestrictedNotifier<AdaptiveSizeData> { | ||
const InheritedAdaptiveSize({ | ||
super.key, | ||
required super.notifier, | ||
required super.child, | ||
}); | ||
|
||
static AdaptiveSizeNotifier? maybeOf(BuildContext context) { | ||
final inherited = | ||
context.dependOnInheritedWidgetOfExactType<InheritedAdaptiveSize>(); | ||
|
||
if (inherited == null) { | ||
return null; | ||
} | ||
|
||
return inherited.notifier; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# melos_managed_dependency_overrides: yak_flutter,yak_result,yak_runner,yak_tween,yak_utils | ||
dependency_overrides: | ||
yak_flutter: | ||
path: ../yak_flutter | ||
yak_result: | ||
path: ../yak_result | ||
yak_runner: | ||
path: ../yak_runner | ||
yak_tween: | ||
path: ../yak_tween | ||
yak_utils: | ||
path: ../yak_utils |