This file contains hidden or 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
| class AlwaysScrollable extends StatelessWidget { | |
| const AlwaysScrollable({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: const Text('Always scrollable'), | |
| ), | |
| body: RefreshIndicator( |
This file contains hidden or 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
| extension DebounceExtension on Ref { | |
| /// delays the execution of the code for the given duration, | |
| /// if any dependency changes during the period, | |
| /// the timer will reset and restart | |
| /// if nothing changes, the rest of the code will be executed. | |
| Future<void> debounce([ | |
| Duration duration = const Duration(milliseconds: 350), | |
| ]) { | |
| final completer = Completer<void>(); |
This file contains hidden or 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
| extension CacheExtension on AutoDisposeRef { | |
| KeepAliveLink cacheFor([Duration duration = const Duration(seconds: 3)]) { | |
| Timer? timer; | |
| // prevents being disposed | |
| final link = keepAlive(); | |
| // when the provider is no longer used (removed all listeners) | |
| // the timer will be started with the given cache duration | |
| // when the time expires, the link will be closed, | |
| // and the provider will dispose itself |
This file contains hidden or 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
| extension CancelTokenExtension on AutoDisposeRef { | |
| /// creates a token to cancel API requests | |
| CancelToken cancelToken() { | |
| var token = CancelToken(); | |
| onCancel(token.cancel); | |
| return token; | |
| } | |
| } |
This file contains hidden or 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
| /// gets a user list | |
| final usersProvider = FutureProvider.autoDispose<List<User>>( | |
| (ref) async { | |
| /// debounce only when refreshing | |
| if (ref.state.isRefreshing) { | |
| await ref.debounce(); | |
| } | |
| log('get users', name: 'usersProvider'); |
This file contains hidden or 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
| /// gets a user with given id | |
| final userProvider = FutureProvider.autoDispose.family<User, int>( | |
| (ref, id) async { | |
| /// logs the lifecycle | |
| ref.logger(); | |
| /// caches for 3 seconds (it's a default duration for this example) | |
| final link = ref.cacheFor(); | |
| /// creates a cancel token with auto cancel option |
This file contains hidden or 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
| Index | Page | Index in Page | |
|---|---|---|---|
| 0 | 1 | 0 | |
| 1 | 1 | 1 | |
| 2 | 1 | 2 | |
| 3 | 1 | 3 | |
| 4 | 2 | 0 | |
| 5 | 2 | 1 | |
| 6 | 2 | 2 | |
| 7 | 2 | 3 | |
| 8 | 3 | 0 |
This file contains hidden or 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
| class HomePage extends StatelessWidget { | |
| const HomePage({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| body: Center( | |
| child: ElevatedButton( | |
| onPressed: () { | |
| context.showCustomDialog( |
This file contains hidden or 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
| // Custom Dialog implementation | |
| class CustomDialogRoute<T> extends PopupRoute<T> { | |
| CustomDialogRoute({ | |
| required this.builder, | |
| super.settings, | |
| }); | |
| // We're getting a widget with context | |
| final Widget Function(BuildContext context) builder; | |
| @override |
This file contains hidden or 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
| // Just a simple custom dialog, nothing special | |
| class CustomDialog extends StatelessWidget { | |
| const CustomDialog({ | |
| super.key, | |
| required this.title, | |
| required this.description, | |
| required this.confirmText, | |
| this.onTap, | |
| }); |