I began writing articles long before I started Flutter development. Yet, since I began building apps with Flutter, I haven't written any personal articles on it. Hell, I haven't written any personal articles, period. Well, except you count this one. So, to force myself to write, I decided to start a small Flutter project. I realized I didn't know how to build a Flutter package, so I set out to do that.
To ensure I completed the project, I spent some time thinking up something that was interesting, but not necessarily useful. I came up with —drum rolls🥁— Balablu generator🙃. The idea was to build a package that randomly plays the viral short clip by BAT. If you don't know what any of that is, perhaps it is for the best😂.
I actually worked on the package and completed it. But I soon realized that it was a joke with a short lifespan. At some point, the hilarity of the whole thing will fade. Perhaps, it already has.
So, I pivoted.
I decided to create another package that randomly plays horror-themed sounds. I reused most of the code from balablu and added new audio files and code to play a random audio clip.
The result: goosebumps.
Goosebumps Requirement
Before you can give yourself (your app, really. Wait🤔, It's actually both) goosebumps, you need to do the following:
Install goosebumps.
Include the
VIBRATE
permission in AndroidManifest.xml. You'll see why soon:<uses-permission android:name="android.permission.VIBRATE"/>
Set
compileSDKVersion
to 33 inandroid\app\build.gradle
.
On To Horror Stuff
Once that's done, you can use the package in your app. To initialize the package...:
Import the package:
import 'package:goosebumps/goosebumps.dart';
Initialize horror sounds generation:
void main() { Horror.init(); runApp(MyApp()); }
And that's it! When you launch an app initialized with Horror
, it plays horror-themed sounds at random. This could be anywhere from once a second to once an hour, or even more, depending on the frequency set.
NB: You don't have to initialize Horror
in the main()
function. You can place it anywhere in your code, even a widget's build()
method.
Setting The Frequency
By default, the frequency is set very high. To change that, set the frequency
parameter of the init()
method to one of the following frequency constants:
Frequencies.veryLow
Frequencies.low
Frequencies.medium
Frequencies.high
Frequencies.veryHigh
Alternatively, you can set a custom frequency value like so:
Horror.init(frequency: Frequency(milliHertz: 2));
That is a very low value, by the way.
Enable/Disable 'Shake to Disable'
Another feature of the package is that you can enable or disable sound generation by shaking the phone. Sound generation is enabled when you launch the app for the first time. Shaking the phone causes it to vibrate for a second, thereafter disabling it. To enable it, you just shake again.
Now, to turn off the 'shake to disable' feature, i.e, shaking the phone would no longer do anything, set shakeToDisable
to false
like so:
Horror.init(shakeToDisable: false)
Camouflage API
This is probably my favorite feature of the package. So, the most 'useful' use case of this package (at least to me) is to prank a colleague or a friend. However, it wouldn't take long for a colleague to figure out what's happening and take out the rouge code.
So, what if the code looked like it belonged? That's where the below API comes in.
Instead of using Horror.init()
to initialize the package, use this:
WidgetFlutterBinding.ensureInitialized();
Looks familiar doesn't it? If you've worked with packages before now, you may have come across one that requires you to call WidgetsFlutterBinding.ensureInitialized();
in the main()
function of your flutter project.
The camouflage API looks just like it but uses Widget
instead of Widgets
. In fact, when you hover over the function, it displays the same documentation!
I'm also considering creating a package disguised as a common/useful package to make it even harder to discover😀.
Conclusion
Even though this package isn't very useful, it was interesting enough for me to complete and write about. Hopefully, going forward, I have more useful stuff to write about. I hope you enjoyed reading this. See you in the next article.
By the way, y'all should like and use the package😒. Got to get those numbers up.