forked from openfoodfacts/smooth-app
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework how the carousel is managed (openfoodfacts#4403)
- Loading branch information
Showing
9 changed files
with
114 additions
and
123 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
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,86 @@ | ||
import 'package:carousel_slider/carousel_controller.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:smooth_app/helpers/haptic_feedback_helper.dart'; | ||
|
||
class ExternalCarouselManager extends StatefulWidget { | ||
const ExternalCarouselManager({ | ||
super.key, | ||
required this.child, | ||
}); | ||
|
||
final Widget child; | ||
|
||
static ExternalCarouselManagerState watch(BuildContext context) { | ||
return context | ||
.dependOnInheritedWidgetOfExactType<_InheritedCarouselManager>()! | ||
.state; | ||
} | ||
|
||
static ExternalCarouselManagerState? find(BuildContext context) { | ||
return context | ||
.findAncestorWidgetOfExactType<_InheritedCarouselManager>() | ||
?.state; | ||
} | ||
|
||
static ExternalCarouselManagerState read(BuildContext context) { | ||
return find(context)!; | ||
} | ||
|
||
@override | ||
State<ExternalCarouselManager> createState() => | ||
ExternalCarouselManagerState(); | ||
} | ||
|
||
class ExternalCarouselManagerState extends State<ExternalCarouselManager> { | ||
final CarouselController _controller = CarouselController(); | ||
|
||
String? _currentBarcode; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return _InheritedCarouselManager( | ||
state: this, | ||
child: widget.child, | ||
); | ||
} | ||
|
||
void showSearchCard({bool notify = false}) { | ||
animatePageTo(0); | ||
|
||
if (notify) { | ||
SmoothHapticFeedback.lightNotification(); | ||
} | ||
} | ||
|
||
// With an animation | ||
void animatePageTo(int page) => _controller.animateToPage(page); | ||
|
||
// Without an animation | ||
void moveToSearchCard() => _controller.jumpToPage(0); | ||
|
||
CarouselController get controller => _controller; | ||
|
||
String? get currentBarcode => _currentBarcode; | ||
|
||
set currentBarcode(String? barcode) => | ||
setState(() => _currentBarcode = barcode); | ||
|
||
bool updateShouldNotify(ExternalCarouselManagerState oldState) { | ||
return oldState.currentBarcode != _currentBarcode; | ||
} | ||
} | ||
|
||
class _InheritedCarouselManager extends InheritedWidget { | ||
const _InheritedCarouselManager({ | ||
required Widget child, | ||
required this.state, | ||
Key? key, | ||
}) : super(key: key, child: child); | ||
|
||
final ExternalCarouselManagerState state; | ||
|
||
@override | ||
bool updateShouldNotify(_InheritedCarouselManager oldWidget) { | ||
return state.updateShouldNotify(oldWidget.state); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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