Skip to content

Commit cca6017

Browse files
committed
+Add back readme
1 parent bfc5d04 commit cca6017

3 files changed

Lines changed: 159 additions & 2 deletions

File tree

README.md

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<a href="https://www.buymeacoffee.com/dev_cetera" target="_blank"><img align="right" src="https://cdn.buymeacoffee.com/buttons/default-orange.png" height="48"></a>
2+
<a href="https://discord.gg/gEQ8y2nfyX" target="_blank"><img align="right" src="https://raw.githubusercontent.com/dev-cetera/resources/refs/heads/main/assets/discord_icon/discord_icon.svg" height="48"></a>
3+
4+
Dart & Flutter Packages by dev-cetera.com & contributors.
5+
6+
[![sponsor](https://img.shields.io/badge/sponsor-grey?logo=github-sponsors)](https://github.com/sponsors/dev-cetera)
7+
[![patreon](https://img.shields.io/badge/patreon-grey?logo=patreon)](https://www.patreon.com/c/RobertMollentze)
8+
[![pub](https://img.shields.io/pub/v/df_cleanup.svg)](https://pub.dev/packages/df_cleanup)
9+
[![tag](https://img.shields.io/badge/tag-v0.4.7-purple?logo=github)](https://github.com/dev-cetera/df_cleanup/tree/v0.4.7)
10+
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/dev-cetera/df_cleanup/main/LICENSE)
11+
12+
---
13+
14+
<!-- BEGIN _README_CONTENT -->
15+
16+
## Overview
17+
18+
This package offers a flexible solution for managing the cleanup of resources in Flutter. Whether you're handling controllers, stream subscriptions, or any other type of disposable, cancelable, stoppable, or closable resources, it ensures that these resources are efficiently and automatically managed, minimizing the risk of memory leaks and keeping your code clean and maintainable.
19+
20+
## Quickstart
21+
22+
- This package streamlines the management of resource cancellation, closing, disposal, and stopping.
23+
- The cleanup methods available are `willCancel`, `willClose`, `willDispose`, and `willStop`.
24+
- Enhance your widgets by using `StatelessAttachableMixin` or `StatefulAttachableMixin` for seamless access to the cleanup methods via `BuildContext`.
25+
- Alternatively, use `CleanupState` instead of `State` for an even simpler way to manage resources within your stateful widgets.
26+
- Apply these [mixins](https://github.com/dev-cetera/df_cleanup/blob/main/lib/src/will) to any class (not just widgets) to access the cleanup methods.
27+
28+
### Example 1 - Mixins:
29+
30+
`DisposeMixin` and `WillDisposeMixin` can be applied to any class to efficiently manage disposable resources. For `StatefulWidget`, you can further simplify your code by using `WillDisposeState` instead of `State`. These mixins allow you to easily mark resources like `TextEditingController` and `ValueNotifier` for automatic disposal when the widget is removed from the widget tree. This approach ensures that all resources are properly cleaned up without requiring manual intervention, streamlining resource management in your stateful widgets.
31+
32+
```dart
33+
class _CounterState extends CleanupState<Counter> {
34+
// Define resources and schedule them to be disposed when this widget's
35+
// dispose method is called.
36+
late final _secondsRemaining = willDispose(ValueNotifier<int>(60));
37+
late final _tickCounter = willDispose(ValueNotifier<int>(0));
38+
late final _timer = willCancel(
39+
Timer.periodic(
40+
const Duration(seconds: 1),
41+
_onTick,
42+
),
43+
);
44+
45+
void _onTick(Timer timer) {
46+
if (_secondsRemaining.value > 0) {
47+
_secondsRemaining.value--;
48+
_tickCounter.value++;
49+
} else {
50+
timer.cancel();
51+
}
52+
}
53+
54+
@override
55+
Widget build(BuildContext context) {
56+
// Do not define methods with willDispose witin the build function, as
57+
// this will create an additional resource to clean up every time the
58+
// state is rebuilt.
59+
final doNotDoThis = willDispose(ValueNotifier('DO NOT DO THIS'));
60+
return Column(
61+
mainAxisAlignment: MainAxisAlignment.center,
62+
children: [
63+
ValueListenableBuilder<int>(
64+
valueListenable: _secondsRemaining,
65+
builder: (context, value, child) {
66+
return Text(
67+
'$value seconds remaining',
68+
style: const TextStyle(fontSize: 24),
69+
);
70+
},
71+
),
72+
const SizedBox(height: 20),
73+
ValueListenableBuilder<int>(
74+
valueListenable: _tickCounter,
75+
builder: (context, value, child) {
76+
return Text(
77+
'Ticks: $value',
78+
style: const TextStyle(fontSize: 24),
79+
);
80+
},
81+
),
82+
],
83+
);
84+
}
85+
```
86+
87+
### Example 2 - BuildContext:
88+
89+
In some cases, you may prefer or need to manage disposable resources within a `StatelessWidget`, or you might use a `StatefulWidget` but you don't want to incorporate the mixins. The `willDispose` method can still be used effectively in these scenarios by leveraging the `BuildContext`.
90+
91+
```dart
92+
class ChatBox extends StatelessWidget {
93+
const ChatBox({super.key});
94+
95+
@override
96+
Widget build(BuildContext context) {
97+
// Define resources and schedule them to be disposed when this widget is
98+
// removed from the widget tree.
99+
final textEditingController = context.willDispose(TextEditingController());
100+
final focusNode = context.willDispose(FocusNode());
101+
102+
return Row(
103+
children: [
104+
TextField(
105+
controller: textEditingController,
106+
focusNode: focusNode,
107+
),
108+
ElevatedButton(
109+
onPressed: () {
110+
final text = textEditingController.text;
111+
print('Submitted: $text');
112+
textEditingController.clear();
113+
focusNode.requestFocus();
114+
},
115+
child: const Text('Submit!'),
116+
),
117+
],
118+
);
119+
}
120+
}
121+
```
122+
123+
<!-- END _README_CONTENT -->
124+
125+
---
126+
127+
☝️ Please refer to the [API reference](https://pub.dev/documentation/df_cleanup/) for more information.
128+
129+
---
130+
131+
## 💬 Contributing and Discussions
132+
133+
This is an open-source project, and we warmly welcome contributions from everyone, regardless of experience level. Whether you're a seasoned developer or just starting out, contributing to this project is a fantastic way to learn, share your knowledge, and make a meaningful impact on the community.
134+
135+
### ☝️ Ways you can contribute
136+
137+
- **Buy me a coffee:** If you'd like to support the project financially, consider [buying me a coffee](https://www.buymeacoffee.com/dev_cetera). Your support helps cover the costs of development and keeps the project growing.
138+
- **Find us on Discord:** Feel free to ask questions and engage with the community here: https://discord.gg/gEQ8y2nfyX.
139+
- **Share your ideas:** Every perspective matters, and your ideas can spark innovation.
140+
- **Help others:** Engage with other users by offering advice, solutions, or troubleshooting assistance.
141+
- **Report bugs:** Help us identify and fix issues to make the project more robust.
142+
- **Suggest improvements or new features:** Your ideas can help shape the future of the project.
143+
- **Help clarify documentation:** Good documentation is key to accessibility. You can make it easier for others to get started by improving or expanding our documentation.
144+
- **Write articles:** Share your knowledge by writing tutorials, guides, or blog posts about your experiences with the project. It's a great way to contribute and help others learn.
145+
146+
No matter how you choose to contribute, your involvement is greatly appreciated and valued!
147+
148+
### ☕ We drink a lot of coffee...
149+
150+
If you're enjoying this package and find it valuable, consider showing your appreciation with a small donation. Every bit helps in supporting future development. You can donate here: https://www.buymeacoffee.com/dev_cetera
151+
152+
<a href="https://www.buymeacoffee.com/dev_cetera" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" height="40"></a>
153+
154+
## 🧑‍⚖️ License
155+
156+
This project is released under the [MIT License](https://raw.githubusercontent.com/dev-cetera/df_cleanup/main/LICENSE). See [LICENSE](https://raw.githubusercontent.com/dev-cetera/df_cleanup/main/LICENSE) for more information.
157+
File renamed without changes.

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ repository: https://github.com/robmllze/df_cleanup
1616
funding:
1717
- https://www.buymeacoffee.com/dev_cetera
1818
description: A flexible solution for managing the cleanup of resources in Flutter.
19-
version: 0.4.7
19+
version: 0.4.8
2020
topics:
2121
- cancel
2222
- dispose
@@ -34,7 +34,7 @@ environment:
3434
dependencies:
3535
flutter:
3636
sdk: flutter
37-
df_type: ^0.14.1
37+
df_type: ^0.14.2
3838

3939
## -----------------------------------------------------------------------------
4040

0 commit comments

Comments
 (0)