awesome_flutter_extensions
Eine Flutter-Erweiterung zum Entfernen von Boilerplate beim Zugriff auf übergeordnete Kontexteigenschaften
Fantastische Flutter-Erweiterungen zum Entfernen von Boilerplate
{"sdk":"flutter"}{"sdk":"flutter"}^4.0.0+1Englischer Projektschnappschuss. Aktuelle Inhalte auf GitHub.
Awesome flutter extensions to remove boilerplate
flutter pub add awesome_flutter_extensions
pubspec.yaml:dependencies:
awesome_flutter_extensions: ^1.0.0 # Use the latest version from pub.dev
Open the command palette:
Then type Pubspec assist.. until you'll get Pubspec Assist: Add/Update dependencies.
Here type the name of the package: awesome_flutter_extensions and press ENTER ↵.
First of all, let's import the extensions with:
import 'package:awesome_flutter_extensions/awesome_flutter_extensions.dart';
There are many extensions in this library. So I decided to break them up into groups. The groups currently available are:
Why this split? After creating extensions upon extensions, I admit that with context you could access an infinite number of things. That's why I decided to split them up.
To get the width of the screen, use:
// before
MediaQuery.sizeOf(context).width
// after
context.sizes.width;
All the extensions available are:
You can also add padding easily
// before
Padding(padding: EdgeInsets.symmetric(horizontal: 16))
// now
Padding(padding: 16.paddingHorizontal())
All the edge insets available are:
To get the titleLarge text style use:
// before
Theme.of(context).textTheme.titleLarge!
// after
context.textStyles.titleLarge
All the extensions available are:
You can also change the font weight directly using:
context.textStyles.titleLarge.semiBold
All the font weights available are:
You can also change the font style directly using:
context.textStyles.titleLarge.italic
All the font weights available are:
To get the primaryColor from the theme use:
// before
Theme.of(context).primaryColor
// after
context.colors.primary
You can also get the colorScheme colors using:
// before
Theme.of(context).colorScheme
// after
context.colors.scheme
All the extensions available are:
To get the appBarTheme, use:
// before
Theme.of(context).appBarTheme
// after
context.themes.appBar;
All the extensions available are:
To detect in which platform the app is running:
final isMacOS = context.platform.isMacOS
All the parameters available are:
To detect the target platform (e.g. the app is running on Web but from an iOS device):
final isIOS = context.targetPlatform.isIOS;
Various extension on String, for example:
final s = 'hello world';
print(s.capitalize()); // Hello World
final s = 'hello world';
print(s.capitalizeFirst()); // Hello world
Use it when you need to change the string later or you need to remember to translate it.
final s = 'hello world';
print(s.mock); // hello world 🧨
final s = '2.0';
print(s.isNum()); // true
final s2 = 'hi';
print(s2.isNum()); // false
All the extensions available are:
Additional extensions.
// before
import 'dart:developer' as devtools show log;
devtools.log(MyClass.toString());
// now
MyClass.log();
Do you want to separate your Column or Row with the same widget like ListView.separated?
Use separatedBy:
// before
Column(
children: [
FirstWidget(),
SizedBox(height: 8),
SecondWidget()
SizedBox(height: 8),
ThirdWidget(),
],
),
// now
Column(
children: [
FirstWidget(),
SecondWidget()
ThirdWidget(),
].separatedBy(SizedBox(height: 8)),
),
// before
final twoDays = Duration(days: 2);
// now
final twoDays = 2.days;
All available extensions are:
// before
await Future.delayed(Duration(seconds: 5));
// now
await 5.seconds.future();
To push a page you can use:
// before
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const SecondPage(),
),
);
// after
context.navigator.push(
MaterialPageRoute(
builder: (context) => const SecondPage(),
),
);
// or even shorter
context.navigator.pushMaterial(const SecondPage()),
All the extensions available are:
canPop
maybePop
pop
popUntil
push
pushMaterial
pushCupertino
popAndPushNamed
pushAndRemoveUntil
pushNamed
pushNamedAndRemoveUntil
pushReplacement
pushReplacementMaterial
pushReplacementCupertino
pushReplacementNamed
removeRoute
removeRouteBelow
replace
replaceRouteBelow
Contributions are welcomed!
Here is a curated list of how you can help:
If you enjoyed the library, like it.