Flutter Made Easy: My Helper Functions (Part 1)

As a seasoned Flutter developer, I understand the challenges that come with the vast and dynamic world of app development. Over time, I’ve crafted and curated a set of helper functions that have proven to be invaluable in streamlining my Flutter development process. Now, I want to share these powerful tools with you.
In this article, we’ll embark on a journey through a collection of helper functions that are designed to simplify and accelerate your Flutter development. Whether you’re a beginner looking to enhance your skills or an experienced developer seeking more efficiency, these functions are tailored to make your Flutter journey smoother.
Let’s dive into the world of “Flutter Made Easy: My Helper Functions” and unlock the potential to elevate your app development experience.
THelperFunctions
One such utility class that stands out is THelperFunctions. This class provides a range of helpful methods for handling colors, displaying notifications, navigating screens, and more. In this article, we will explore the different functionalities provided by THelperFunctions and how to leverage them in your Flutter applications.
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class THelperFunctions {
static Color? getColor(String value) {
/// Define your product specific colors here and it will match the attribute colors and show specific 🟠🟡🟢🔵🟣🟤
if (value == 'Green') {
return Colors.green;
} else if (value == 'Green') {
return Colors.green;
} else if (value == 'Red') {
return Colors.red;
} else if (value == 'Blue') {
return Colors.blue;
} else if (value == 'Pink') {
return Colors.pink;
} else if (value == 'Grey') {
return Colors.grey;
} else if (value == 'Purple') {
return Colors.purple;
} else if (value == 'Black') {
return Colors.black;
} else if (value == 'White') {
return Colors.white;
} else if (value == 'Yellow') {
return Colors.yellow;
} else if (value == 'Orange') {
return Colors.deepOrange;
} else if (value == 'Brown') {
return Colors.brown;
} else if (value == 'Teal') {
return Colors.teal;
} else if (value == 'Indigo') {
return Colors.indigo;
} else {
return null;
}
}
static void showSnackBar(context, String message) {
ScaffoldMessenger.of(context!).showSnackBar(
SnackBar(content: Text(message)),
);
}
static void showAlert(context, String title, String message) {
showDialog(
context: context!,
builder: (BuildContext context) {
return AlertDialog(
title: Text(title),
content: Text(message),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('OK'),
),
],
);
},
);
}
static void navigateToScreen(BuildContext context, Widget screen) {
Navigator.push(
context,
MaterialPageRoute(builder: (_) => screen),
);
}
static String truncateText(String text, int maxLength) {
if (text.length <= maxLength) {
return text;
} else {
return '${text.substring(0, maxLength)}...';
}
}
static bool isDarkMode(BuildContext context) {
return Theme.of(context).brightness == Brightness.dark;
}
static Size screenSize(context) {
return MediaQuery.of(context!).size;
}
static double screenHeight(context) {
return MediaQuery.of(context!).size.height;
}
static double screenWidth(context) {
return MediaQuery.of(context!).size.width;
}
static String getFormattedDate(DateTime date, {String format = 'dd MMM yyyy'}) {
return DateFormat(format).format(date);
}
static List<T> removeDuplicates<T>(List<T> list) {
return list.toSet().toList();
}
static List<Widget> wrapWidgets(List<Widget> widgets, int rowSize) {
final wrappedList = <Widget>[];
for (var i = 0; i < widgets.length; i += rowSize) {
final rowChildren = widgets.sublist(i, i + rowSize > widgets.length ? widgets.length : i + rowSize);
wrappedList.add(Row(children: rowChildren));
}
return wrappedList;
}
}
THelperFunctions Overview
THelperFunctions is a utility class containing static methods to perform various tasks commonly needed in Flutter applications. Let’s take a closer look at some of its key functionalities:
Color Handling
getColor(String value): Maps a given string value to a corresponding color. This method can be used to easily associate product-specific colors. Notification Display
showSnackBar(String message):
Displays a SnackBar with the provided message. showAlert(String title, String message): Shows a simple alert dialog with the specified title and message.
Navigation
navigateToScreen(BuildContext context, Widget screen): Navigates to a new screen using the provided BuildContext and widget.
Text Manipulation
truncateText(String text, int maxLength): Truncates a given text to a specified maximum length.
Dark Mode Detection
isDarkMode(BuildContext context): Checks whether the current theme is in dark mode.
Screen Size Information
screenSize(), screenHeight(), screenWidth(): Provides information about the device’s screen size.
Date Formatting
getFormattedDate(DateTime date, {String format = ‘dd MMM yyyy’}): Formats a DateTime object into a string with an optional custom format. List Operations
removeDuplicates<T>(List<T> list):
Removes duplicate elements from a list.
wrapWidgets(List
Sample Usage of THelperFunctions
final isDark = THelperFunctions.isDarkMode(context);baseColor: isDark ? Colors.grey[850]! : Colors.grey[300]!,
highlightColor: isDark ? Colors.grey[700]! : Colors.grey[100]!,Conclusion: Elevate Your Flutter Development
Incorporating THelperFunctions into your Flutter projects is akin to having a Swiss Army Knife for your development tasks. The class simplifies complex operations, enhances user interactions, and contributes to a more efficient and enjoyable Flutter application development experience. Experiment with these functions in your projects, and unlock the potential for accelerated, streamlined, and user-friendly Flutter applications
Stay tuned!!! I will be back with some more cool flutter and dart tutorials in the next article. I hope you liked the article. Don’t forget to follow me 😇 and give some claps 👏. And if you have any questions feel free to comment. Thank you.
Thanks a lot for reading till the end. Follow or contact me via: Email: [email protected] Github: https://github.com/reubenshumba LinkedIn: https://www.linkedin.com/in/reuben-shumba-a72aaa157/ BuyMeCoffee: https://www.buymeacoffee.com/reubenshumba REUBEN SHUMBA





