Skip to content

Instantly share code, notes, and snippets.

@rei-codes
rei-codes / always_scrollable.dart
Last active April 28, 2023 14:21
Always scrollable
class AlwaysScrollable extends StatelessWidget {
const AlwaysScrollable({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Always scrollable'),
),
body: RefreshIndicator(
@rei-codes
rei-codes / extensions.dart
Created February 12, 2023 09:05
DebounceExtension
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>();
@rei-codes
rei-codes / extensions.dart
Created February 12, 2023 09:05
CacheExtension
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
@rei-codes
rei-codes / extensions.dart
Created February 12, 2023 09:05
CancelToken extension
extension CancelTokenExtension on AutoDisposeRef {
/// creates a token to cancel API requests
CancelToken cancelToken() {
var token = CancelToken();
onCancel(token.cancel);
return token;
}
}
@rei-codes
rei-codes / user_provider.dart
Created February 12, 2023 09:04
users provider
/// 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');
@rei-codes
rei-codes / user_provider.dart
Created February 12, 2023 09:04
user provider
/// 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
@rei-codes
rei-codes / index_table.csv
Created January 8, 2023 21:10
example index table of infiinite listview project
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
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
context.showCustomDialog(
// 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
// 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,
});