Skip to content

Instantly share code, notes, and snippets.

View ulusoyca's full-sized avatar
💙
Flutter + Android

Cagatay Ulusoy ulusoyca

💙
Flutter + Android
View GitHub Profile
@ulusoyca
ulusoyca / http_retry_duration_list_generator.dart
Created November 16, 2023 14:29
An example for using retry policy with dart http package
import 'dart:async';
import 'dart:math';
import 'package:http/http.dart' as http;
/// A utility class for generating a list of waiting times (delays) for HTTP request retries.
/// This class uses an exponential backoff strategy with jitter for calculating delay times.
class HttpRetryDurationListGenerator {
/// Generates a list of waiting times (durations) to be used between retry attempts.
///
@ulusoyca
ulusoyca / scrollable_bottom_sheet_top_bar.dart
Created January 29, 2022 20:01
ScrollableBottomSheetTopBar
class ScrollableBottomSheetTopBar extends StatelessWidget {
final String topBarTitle;
final double topBarHeight;
final double? heroImageHeight;
final Color backgroundColor;
final ValueListenable<double> currentScrollPositionListenable;
final GlobalKey titleKey;
final double pageTitleTopPadding;
final double topBarTranslationYAmountInPx;
@ulusoyca
ulusoyca / hero_image_full_code.dart
Last active January 29, 2022 20:10
HeroImageFullCode
class ScrollableBottomSheetHeroImage extends StatelessWidget {
final Widget heroImage;
final double topBarHeight;
final double heroImageHeight;
const ScrollableBottomSheetHeroImage({
required this.heroImage,
required this.topBarHeight,
required this.heroImageHeight,
Key? key,
@ulusoyca
ulusoyca / scrollable_bottom_sheet.dart
Last active January 23, 2022 20:07
ScrollableBottomSheet
const _emptyContainerLayoutId = 'empty_container';
const _bottomSheetLayoutId = 'bottomSheet';
Future<T?> showScrollableBottomSheet<T>(
{required BuildContext context,
required List<ScrollableBottomSheetPage> Function(BuildContext) pages,
ValueNotifier<int>? pageIndexListenable,
EdgeInsetsDirectional? edgeInsetsDirectional,
ScrollController? scrollController,
final _scaleStart = 1.1;
final _scaleEnd = 1.0;
double _calculateScale(double currentScrollPosition) {
final startPointInPx = 0.0;
final endPointInPx = heroImageHeight - topBarHeight;
final distanceInPx = endPointInPx - startPointInPx;
final progressInPx = currentScrollPosition - startPointInPx;
final progress = progressInPx / distanceInPx;
final rawScale = _scaleStart - (progress * (_scaleStart - _scaleEnd));
flowPaintingContext.paintChild(
0,
transform: Matrix4.diagonal3Values(scale, scale, 1.0),
opacity: opacity,
);
@ulusoyca
ulusoyca / hero_image_paint_children.dart
Last active January 29, 2022 19:49
HeroImagePaintChildren
@override
void paintChildren(FlowPaintingContext flowPaintingContext) {
final currentScrollPosition = scrollPosition.pixels;
/// Calculate scale
double scale = calculateTransformationValue(
startValue: 1.1,
endValue: 1.0,
rangeInPx: heroImageHeight - topBarHeight,
progressInRangeInPx: currentScrollPosition,
@ulusoyca
ulusoyca / calculate_hero_opacity.dart
Last active January 23, 2022 21:26
CalculateHeroopacity
/// Calculate opacity
final _opacityStart = 1.0;
final _opacityEnd = 0.0;
double opacity = _calculatePropertyValue(
currentScrollPosition: currentScrollPosition,
startPointInPx: (heroImageHeight / 2) - topBarHeight,
endPointInPx: heroImageHeight - topBarHeight,
lowerLimit: _opacityEnd,
upperLimit: _opacityStart,
);
@ulusoyca
ulusoyca / calculate_opacity_hero_image.dart
Last active January 23, 2022 21:26
CalculateOpacityHeroImage
final _opacityStart = 1.0;
final _opacityEnd = 0.0;
double _calculateOpacity(double currentScrollPosition) {
final double distance = heroImageHeight / 2;
final double startPoint = heroImageHeight - topBarHeight;
final rawOpacity = ((startPoint - currentScrollPosition) / distance);
return rawOpacity.clamp(_opacityEnd, _opacityStart);
}
@ulusoyca
ulusoyca / calculate_generic_property_change.dart
Last active January 29, 2022 19:44
CalculateGenericPropertChange
double calculateTransformationValue({
required double rangeInPx,
required double progressInRangeInPx,
required double startValue,
required double endValue,
}) {
final progress = progressInRangeInPx / rangeInPx;
final rawValue = startValue + (progress * (endValue - startValue));
return rawValue.clamp(min(startValue, endValue), max(startValue, endValue));
}