Flutter Short BUT Gold’s

Veli Bacık
Flutter Community
Published in
4 min readJan 3, 2021

Sometimes, big steps aren’t that big; actually, you should think just simple, and it will come to your hands at last.

We will talk more extensions use case scenario. This exists in many other languages. It’s the main goal, add more options for easy to use. I have more product in apple and google store so more using in every project and I’ve improved my extension at every project.

Let’s write extensions together 🙏

Context Extension

Context Extension Samples

Context is the most using object in the flutter, you will see probably wherever in code. We can add main features on the context and we access features everywhere while having context.

Generally

I think which more using for my coding life and firstly write main features on context.

  • Media query needs to calculate device size operation or contains more device metrics.
  • Theme properties using while draw text style, container or any widget design.
  • Especially when doing form screen you may need keyboard state so you call just context and do everything now.

Look at this sample code:

Maybe we create some extension with build context. You want to look all context extension, look at this page. Let’s write a few samples together:

  • Padding Extension
    We most use padding on screens and add this extension on context and we call in this way: Padding(padding: context.paddingHigh)
extension PaddingExtension on BuildContext {
EdgeInsets get paddingHigh => EdgeInsets.all(highValue);
EdgeInsets get horizontalPaddingLow => EdgeInsets.symmetric(horizontal: lowValue);
}
  • Empty Box
    Sometimes need an empty box between the two widget, use the this way: Column(children:[xWidget,context.emptySizedWidthBoxLow,yWidget])
extension SizedBoxExtension on BuildContext {
Widget get emptySizedWidthBoxLow => SpaceSizedWidthBox(width: 0.03);
Widget get emptySizedHeightBoxLow => SpaceSizedHeightBox(height: 0.01);
}

And I mostly use this extension recently.

  • Navigation Extension
    And I mostly use this extension recently so this very useful application routing

String Extension

The string is a primitive type but we’ll gain power so many features very easy use. This variable could be called everywhere but more use string validation, launch(open website or application), format & mask value and etc.

  • This hex string value to colour, just calls “FF45X2”.color.
  • Validator string needs from formfield widget, you can use “isValidEmail” to the end.
  • You need string value to trigger in mail application context so call “launchEmail” like WhatsApp share.
  • String value need mask and unmask options from text field widget so you can call input formatted constants so you would mask on filed and if you want to unmask value call phoneFormatValue to the end.

I prepared more string extension on my package. You can look at other string extension features.

Let’s look at example code:

Future Extension

This one of my favourites extension. I need a widget from the future completed, you could be a future extension. (like service calls etc.)

It’s a very easy way to manage future result from your widget. This manages success, error and loading case so you can central usage on the app.

Let’s look at the example code:

You create a base to build function and doesn’t need to declare loading, not found and error widget for every request.

Future Extension Sample

Widget Extension

I will have added new features in this widget extension so now has just visible extension. Visible need doesn’t data found or other user scenarios.

extension WidgetExtension on Widget { 
Widget toVisible(bool val) => val ? this : SizedBox(height: 1);
}

So let's look at the sample:

Image Extension

Image Rotation Sample

This extension just works rotate features on the image. Rotation need in a project because service back to reverse image and I was created this.

This main goal rotates to image base coordinates on the superficial.

Let’s look at the example code:

Finally, it’s everything okay just now :). You can use this extension by pieces or I create Kartal package. Kartal contains this extension and more features.

Thanks for reading 🙋‍♂️I hope will be a healthy year.
We will see new articles ☘️

Follow us on Twitter: https://www.twitter.com/FlutterComm

--

--

Veli Bacık
Flutter Community

We always change the world, so just want it. [OLD]Google Developer Expert Flutter & Dart, Gamer, Work More!