Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

functional_widget

a code generator to write widgets as function without loosing the benefits of classes.

widgets are cool. but classes are quite verbose:

class foo extends statelesswidget {
  final int value;
  final int value2;

  const foo({key key, this.value, this.value2}) : super(key: key);

  @override
  widget build(buildcontext context) {
    return text('$value $value2');
  }
}

so much code for something that could be done much better using a plain function:

widget foo(buildcontext context, { int value, int value2 }) {
  return text('$value $value2');
}

the problem is, using functions instead of classes is not recommended:

… or is it?


functional_widgets, is an attempt to solve this issue, using a code generator.

simply write your widget as a function, decorate it with a @swidget, and then
this library will generate a class for you to use.

as the added benefit, you also get for free the ability to inspect the parameters
passed to your widgets in the devtool

example

you write:

@swidget
widget foo(buildcontext context, int value) {
  return text('$value');
}

it generates:

class foo extends statelesswidget {
  const foo(this.value, {key key}) : super(key: key);

  final int value;

  @override
  widget build(buildcontext context) {
    return foo(context, value);
  }

  @override
  void debugfillproperties(diagnosticpropertiesbuilder properties) {
    super.debugfillproperties(properties);
    properties.add(intproperty('value', value));
  }
}

and then you use it:

runapp(
  foo(42)
);

how to use

install (builder)

there are a few separate packages you need to install:

  • functional_widget_annotation, a package containing decorators. you must
    install it as dependencies.
  • functional_widget, a code-generator that uses the decorators from the previous
    packages to generate your widget.
  • build_runner, a dependency that all applications using code-generation should have

your pubspec.yaml should look like:

dependencies:
  functional_widget_annotation: ^0.8.0

dev_dependencies:
  functional_widget: ^0.8.0
  build_runner: ^1.9.0

that’s it!

you can then start the code-generator with:

flutter pub run build_runner watch

customize the output

it is possible to customize the output of the generator by using different decorators or configuring default values in build.yaml file.

build.yaml change the default behavior of a configuration.

# build.yaml
targets:
  $default:
    builders:
      functional_widget:
        options:
          # default values:
          debugfillproperties: false
          widgettype: stateless # or 'hook'

functionalwidget decorator will override the default behavior for one specific widget.

@functionalwidget(
  debugfillproperties: true,
  widgettype: functionalwidgettype.hook,
)
widget foo() => container();

debugfillproperties override

widgets can be override debugfillproperties to display custom fields on the widget inspector. functional_widget offer to generate these bits for your, by enabling debugfillproperties option.

for this to work, it is required to add the following import:

import 'package:flutter/foundation.dart';

example:

(you write)

import 'package:flutter/foundation.dart';

@swidget
widget example(int foo, string bar) => container();

(it generates)

class example extends statelesswidget {
  const example(this.foo, this.bar, {key key}) : super(key: key);

  final int foo;

  final string bar;

  @override
  widget build(buildcontext _context) => example(foo, bar);
  @override
  void debugfillproperties(diagnosticpropertiesbuilder properties) {
    super.debugfillproperties(properties);
    properties.add(intproperty('foo', foo));
    properties.add(stringproperty('bar', bar));
  }
}

generate different type of widgets

by default, the generated widget by @functionalwidget() is a statelesswidget.

it is possible to generate a hookwidget instead (from https://github.com/rrousselgit/flutter_hooks)

there are a few ways to do so:

  • through build.yaml:
    # build.yaml
    targets:
      $default:
        builders:
          functional_widget:
            options:
              widgettype: hook
    

    then used as:

    @functionalwidget()
    widget example(int foo, string bar) => container();
    
  • with parameters on the @functionalwidget decorator:
    @functionalwidget(widgettype: functionalwidgettype.hook)
    widget example(int foo, string bar) => container();
    
  • with the shorthand @hwidget decorator:
    @hwidget
    widget example(int foo, string bar) => container();
    

in any cases, flutter_hooks must be added as a separate dependency in the pubspec.yaml

dependencies:
  flutter_hooks: # some version number

all the potential function prototypes

functional_widget will inject widget specific parameters if you ask for them.
you can potentially write any of the following:

widget foo();
widget foo(buildcontext context);
widget foo(key key);
widget foo(buildcontext context, key key);
widget foo(key key, buildcontext context);

you can then add however many arguments you like after the previously defined arguments. they will then be added to the class constructor and as a widget field:

  • positional
@swidget
widget foo(int value) => text(value.tostring());

// usage

foo(42);
  • named:
@swidget
widget foo({int value}) => text(value.tostring());

// usage

foo(value: 42);
  • a bit of everything:
@swidget
widget foo(buildcontext context, int value, { int value2 }) {
  return text('$value $value2');
}

// usage

foo(42, value2: 24);

Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Top