सार्वजनिक जानकारी से बना चयन मार्गदर्शन। केवल संदर्भ हेतु।
Awesome Extensions एक Flutter पैकेज है जो उपयोगी एक्सटेंशन के साथ विजेट्स को बढ़ाता है, बॉइलरप्लेट को कम करता है और आम कार्यों को सरल बनाता है। यह विभिन्न प्लेटफॉर्मों पर लेआउट, स्टाइलिंग और रिस्पॉन्सिव डिज़ाइन के लिए उपयोगी विधियाँ प्रदान करता है। मुख्य Flutter क्लासों को विस्तारित करके, यह विकासकर्ताओं को साफ और अधिक संक्षिप्त UI कोड लिखने की अनुमति देता है। यह पैकेज Android, iOS, Web, macOS, Windows और Linux को समर्थन देता है, जिससे एक समानता और दक्षता के लिए क्रॉस-प्लेटफॉर्म प्रोजेक्ट्स के लिए आदर्श है।
उपयोग
विजेट निर्माण सरलीकरण
प्रतिक्रियाशील लेआउट बनाना
पुनरावृत्ति कोड कम करना
UI विकास गति में सुधार करना
क्रॉस-प्लेटफ़ॉर्म यूआई सामंजस्य
मुख्य विशेषताएँ
रिस्पॉन्सिव डिज़ाइन सहायता कार्यक्रम
न्यूनतम बॉलरप्लेट
मूल फ्लटर विजेट्स का विस्तार करता है
Platform-agnostic
स्पष्ट यूआई कोड
आसान एकीकरण
इनके लिए उपयुक्त
Flutter शुरुआती
अनुभवी Flutter विकासकर्ता
क्रॉस-प्लेटफॉर्म एप्लिकेशन
यूआई-केंद्रित परियोजनाएँ
त्वरित प्रोटोटाइपिंग
विचार योग्य बातें
बंडल के आकार में थोड़ा वृद्धि हो सकती है
विजेट स्तरीय विस्तारों तक सीमित
Dart भाषा विशेषताओं पर निर्भर करता है
जटिल व्यापार तर्क के लिए उपयुक्त नहीं
Flutter widget वृक्ष के बारे में समझने की आवश्यकता है
मूल README
संबंधित सोर्स रिपॉज़िटरी के मूल README का स्नैपशॉट। नवीनतम सामग्री के लिए रिपॉज़िटरी देखें।
Awesome Extensions
Let's get started 💪
Go to pubspec.yaml
Add awesome_extensions and replace [version] with the latest version:
dependencies:awesome_extensions:^[version]
Click the packages get button or run flutter pub get
A comprehensive Flutter extension package that helps reduce boilerplate code and adds helpful methods to widgets, enabling you to create responsive designs with ease. This package provides extensions for widgets, themes, navigation, colors, strings, dates, and much more.
Theme Extensions
TextStyle
From the TextStyle Access properties right in the context instance.
// BeforeText('Hello World', style: Theme.of(context).textTheme.labelSmall),
Text('Hello World', style: TextStyle(color: Colors.grey, fontWeight: FontWeight.bold, fontSize: 40)),
// AfterText('Hello World', style: context.labelSmall),
// ORText('Hello World', style: context.displaySmall),
// If you want to bold text thenText('Hello World', style: context.labelSmall?.bold),
FontWeight extensions that apply font weights on TextStyle:
mostThick or w900 The most thick - FontWeight.w900
extraBold or w800 Extra-bold - FontWeight.w800
bold or w700 Bold - FontWeight.bold - FontWeight.w700
semiBold or w600 Semi-bold - FontWeight.w600
medium or w500 Medium - FontWeight.w500
regular or w400 Regular - FontWeight.w400
light or w300 Light - FontWeight.w300
extraLight or w200 Extra-light - FontWeight.w200
thin or w100 Thin, the least thick - FontWeight.w100
Similar 2021 TextStyle are:
context.displayLarge
context.displayMedium
context.displaySmall
context.headlineLarge
context.headlineMedium
context.headlineSmall
context.titleLarge
context.titleMedium
context.titleSmall
context.bodyLarge
context.bodyMedium
context.bodySmall
context.labelLarge
context.labelMedium
context.labelSmall
Text
If you don't want to use theme styles, then we have some other methods:
By calling MediaQuery.sizeOf(context), the widget will rebuild only when the size changes, avoiding unnecessary rebuilds.
context.mqSize // The same of MediaQuery.sizeOf(context)
context.mqHeight // The same of MediaQuery.sizeOf(context).height
context.mqWidth
context.mqPadding // similar to [MediaQuery.paddingOf(context)]
context.mqViewPadding
context.mqViewInsets
context.mqOrientation
context.mqAlwaysUse24HourFormat
context.mqDevicePixelRatio
context.mqPlatformBrightness
context.mqTextScaleFactor
//Check in what platform the app is runningMyPlatform.isAndroid
MyPlatform.isIOS
MyPlatform.isMacOS
MyPlatform.isWindows
MyPlatform.isLinux
MyPlatform.isFuchsia
//Check the device typeMyPlatform.isMobile
MyPlatform.isDesktop
//All platforms are supported independently in web!//You can tell if you are running inside a browser//on Windows, iOS, OSX, Android, etc.MyPlatform.isWeb
// Returns a value<T> according to the screen size// can give value for:// mobile: if the shortestSide is smaller than 600// tablet: if the shortestSide is smaller than 1200// desktop: if width is largest than 1200
context.responsiveValue<T>(
T? mobile,
T? tablet,
T? desktop,
),
// ExampleContainer(
child: context.responsiveValue(
mobile: Container(
color: Colors.yellow,
width: context.width,
height: context.height,
),
tablet: Container(
color: Colors.green,
width: context.width,
height: context.height,
),
desktop: Container(
color: Colors.black,
width: context.width,
height: context.height,
)),
)
Navigation Extensions
From the Navigator Access properties right in the context instance.
// BeforeNavigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
);
// After// for push
context.push(SecondScreen());
context.pushNamed('/home');
// for back , you can also add back result data
context.pop();
// for replace
context.pushReplacement(SecondScreen());
context.pushReplacementNamed('/home');
// popUntil
context.popUntil('/login');
// with rootNavigator
context.push(SecondScreen(), rootNavigator: true);
context.pushReplacement(SecondScreen(), rootNavigator: true);
context.popUntil('/login', rootNavigator: true);
context.routeSettings;
context.routeName;
context.routeArguments;
Focus & Keyboard Extensions
Easily manage input focus and dismiss the soft keyboard directly from BuildContext.
// Unfocus current input (dismiss keyboard)
context.unfocus();
// Hide keyboard from primary focus
context.hideKeyboard();
// Request focus for a specific FocusNode
context.requestFocus(myFocusNode);
Widget Extensions
This extension reduces boilerplate code significantly.
SizeBox
// BeforeSizedBox(
height : 20.0
)
// After// make space of 20.0 height20.0.heightBox
// for width20.0.widthBox
Sometimes, according to a condition, we want to display nothing. Usually when we can't return null, we would return something like const SizedBox() for example.
This is good, but it has some performance impacts since SizedBox creates a RenderObject. The RenderObject lives in the render tree and some computations are performed on it, even if it paints nothing on the screen.
We can do better, we can have a widget which does not create a RenderObject, while being still valid. The Nil widget is the minimal implementation for this use case. It only creates an Element and does nothing while it's building. Because the optimal way to use it, is to call const Nil(), it also comes with a nil constant that you can use everywhere (which is a const Nil()).
// Good
text != null ? Text(text) : constContainer()
// Better
text != null ? Text(text) : constSizedBox()
// BEST
text != null ? Text(text) : nil
or
if (text != null) Text(text)
SliverToBoxAdapter
Text(text).sliver;
// is same asSliverToBoxAdapter(
child: Text(text),
);
Other
Now we can just add round corners, shadows, align, and added gestures to our Widgets.
A comprehensive set of extensions for advanced Map manipulation, ensuring you write less boilerplate and more expressive code.
Safe Access
Avoid null errors with safe access methods that provide default values.
final map = {'name': 'John'};
// Beforefinal age = map.containsKey('age') ? map['age'] : 30;
// Afterfinal age = map.getOrDefault('age', 30); // Returns 30
getOrDefault(key, defaultValue): Returns the value for the given key or the defaultValue if the key is not in the map.
getOrElse(key, function): Returns the value for the given key or computes it from a function if the key is not in the map.
Filtering
Easily filter maps by keys or values.
final scores = {'math': 95, 'science': 88, 'history': 75};
// Filter by keysfinal scienceScores = scores.filterKeys((key) => key.startsWith('s')); // {'science': 88}// Filter by valuesfinal highScores = scores.filterValues((value) => value > 90); // {'math': 95}
Transformation & Merging
Transform maps into different formats or merge them safely.
// Convert to a URL query stringfinal params = {'user': 'john_doe', 'page': 2};
final query = params.toQueryString(); // "user=john_doe&page=2"// Merge two mapsfinal map1 = {'a': 1, 'b': 2};
final map2 = {'b': 3, 'c': 4};
final merged = map1.merge(map2); // {'a': 1, 'b': 3, 'c': 4}
Nested Operations
Access and manipulate nested map data with ease using dot notation.
final data = {
'user': {
'profile': {
'name': 'Jane Doe'
}
}
};
// Get a nested valuefinal name = data.getNested('user.profile.name'); // 'Jane Doe'// Set a nested value
data.setNested('user.profile.age', 28);
Date Extensions
// Date comparisons
date.isSameDate(otherDate); // bool
date.isToday; // bool
date.isYesterday; // bool
date.isTomorrow; // bool
date.isLeapYear; // bool// Date boundaries & calculations
date.daysInMonth; // int (e.g. 28, 29, 30, 31)
date.startOfDay; // DateTime at 00:00:00
date.endOfDay; // DateTime at 23:59:59
date.addDays(5); // DateTime + 5 days
date.subtractDays(2); // DateTime - 2 days// Relative time formattingDateTime.now().subtract(constDuration(minutes: 5)).timeAgo; // "5 minutes ago"
// your name => Your Name'your name'.capitalize;
// your name => Your name'your name'.capitalizeFirst;
// your name => yourname'your name'.removeAllWhitespace;
// Convert to URL-friendly slug'Hello World!'.toSlug; // "hello-world"// Remove special characters'Hello@# World!'.removeSpecialCharacters; // "Hello World"// Check valid JSON'{"key": "value"}'.isJson; // true// Match RegExp & validations'123'.isNumericOnly; // true'abc'.isAlphabetOnly; // true'Hello'.hasCapitalLetter; // true'true'.isBool; // true
Nullable String Extensions
String? text;
text.isNullOrEmpty; // true if null, empty, or whitespace only
text.isNotNullOrEmpty; // true if contains valid non-whitespace text
text.orDefault('Guest'); // returns "Guest" if null or empty
Async Extensions
An extension to help dealing with all the possible states of an AsyncSnapshot in a StreamBuilder / FutureBuilder.
Reduces boilerplate code significantly by moving each possible state into its own function.
Also you can use maybeWhen, similar to [when], but allows [data] callback to be optional.
Color Extensions
Darken
The darken method darkens the color by a specified percentage. The percentage should be between 0 and 100. By default, the color is darkened by 10%.
Color darkRed = Colors.red.darken(20); // Darkens the red color by 20%
Lighten
The lighten method lightens the color by a specified percentage. The percentage should be between 0 and 100. By default, the color is lightened by 10%.
Color lightRed = Colors.red.lighten(20); // Lightens the red color by 20%
Alpha Percentage
The alphaPercent method sets the alpha transparency using percentage values (0-100).
Also added some extra color extensions like: string hex to color, brightness, and change specific RGB color value.
Url Strategy
With a simple call of setPathUrlStrategy, your Flutter web app does not have a leading #
in the URL anymore 🚀
voidmain() {
// Here we set the URL strategy for our web app.// It is safe to call this function when running on mobile or desktop as well.setPathUrlStrategy();
runApp(MyApp());
}