Skip to main content

Jetpack Compose - style Extensions for Flutter

Updated 3 min readMarkdown
Jetpack Compose - style Extensions for Flutter

Hello Developers! Welcome back to our tech blog. As a Jetpack Compose developer, I've always admired its clean, lean, and intuitive approach to crafting UI components. However, recently, I decided to venture into the Flutter development landscape and found myself battling with the intricacies of its widget architecture.

While Flutter provides powerful tools and flexibility with its extensive widget system, its syntax, for someone accustomed to Jetpack Compose, can be a bit jarring. The biggest hurdle I encountered was dealing with deeply nested widgets wrapped within one another, often leading to a daunting pile of closing braces.

This verbosity in code, especially when creating complex UI, can impact readability and maintenance, resulting in an awkward development experience. On the other hand, Jetpack Compose, with its declarative nature and modifier-based system, offers an elegant way to layer attributes onto composable functions, avoiding the pitfalls of nested structures.

Jetpack Compose's clean and unambiguous syntax makes reading and writing UI code a more streamlined process. For instance, to add padding to a Composable or align it within its parent, you would chain these actions using a modifier, leading to a linear and more readable code.

In Flutter, to achieve similar functionality, we typically wrap our widget within a parent widget responsible for that attribute, like Padding() or Align(). While this approach works and offers a high degree of customization, it leads to deeply nested structures and sometimes a non-intuitive reading order of the code.

But don't worry! If, like me, you're yearning for the simplicity of Jetpack Compose in your Flutter development, I've got some good news. Thanks to Dart's support for extension methods, we can introduce a Jetpack Compose-style syntax into our Flutter projects, and today, we're going to explore just that.

Let's look at an example. Consider the following code:

Text('Hello, World!')
  .padding(const EdgeInsets.all(16))
  .background(Colors.blue)
  .align(Alignment.bottomLeft)
  .expanded(2)
  .aspectRatio(1.5)
  .clickable(() {
    print('Clicked!');
  });

This looks more streamlined and intuitive, right? It's almost as if we are writing Jetpack Compose code! Now, how does this compare to the traditional Flutter style?

GestureDetector(
  onTap: () {
    print('Clicked!');
  },
  child: AspectRatio(
    aspectRatio: 1.5,
    child: Expanded(
      flex: 2,
      child: Align(
        alignment: Alignment.bottomLeft,
        child: DecoratedBox(
          decoration: BoxDecoration(color: Colors.blue),
          child: Padding(
            padding: const EdgeInsets.all(16),
            child: Text('Hello, World!'),
          ),
        ),
      ),
    ),
  ),
)

It's the same result but more nested and verbose. This extension-based method allows you to chain widget transformations, reducing unnecessary widget nesting and improving readability.

So, How to Compose-like coding style in Flutter? It's none other than Dart's extension methods. Below, you can find the definition for these some Jetpack Compose style extensions:

import 'package:flutter/material.dart';

extension WidgetModifier on Widget {
  Widget padding([EdgeInsetsGeometry value = const EdgeInsets.all(8.0)]) {
    return Padding(
      padding: value,
      child: this,
    );
  }

  Widget background(Color color) {
    return DecoratedBox(
      decoration: BoxDecoration(color: color),
      child: this,
    );
  }

  Widget align([Alignment alignment = Alignment.center]) {
    return Align(
      alignment: alignment,
      child: this,
    );
  }

  Widget expanded([int flex = 1]) {
    return Expanded(
      flex: flex,
      child: this,
    );
  }

  Widget aspectRatio([double aspectRatio = 1.0]) {
    return AspectRatio(
      aspectRatio: aspectRatio,
      child: this,
    );
  }

  Widget clickable(VoidCallback onPressed) {
    return GestureDetector(
      onTap: onPressed,
      child: this,
    );
  }
}

These extensions give our widgets additional methods like padding, background, align, expanded, aspectRatio, and clickable, which we can chain together to achieve the desired widget configuration.

As we wrap up this post, it's essential to remember that this is just one method of approaching Flutter development. Depending on your personal or team's coding style and project requirements, you may prefer the traditional style, the Jetpack Compose style extension method, or even a blend of both. The key is to write clean, maintainable code that efficiently solves the task at hand.

The power of Dart and Flutter is in its flexibility, and this is just one example of how we can mold it to suit our needs better.

Thank you for reading! Don't forget to share this post if you found it helpful. Until next time, Happy Coding!