All Projects → rrousselGit → Functional_widget

rrousselGit / Functional_widget

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

Programming Languages

dart
5743 projects

Projects that are alternatives of or similar to Functional widget

Easy Cg
🔧代码生成器计划迭代中... 旧版本代码生成器访问移步:https://github.com/zhaohaihao/easy-cg/tree/v1.0.1
Stars: ✭ 323 (-13.64%)
Mutual labels:  code-generator
Stacklayoutmanager
customized layoutmanager,let item pile up like stackview/类似最美有物卡片堆叠效果
Stars: ✭ 343 (-8.29%)
Mutual labels:  widget
Neogfx
Cross-platform GPU-oriented C++ application/game framework
Stars: ✭ 362 (-3.21%)
Mutual labels:  widget
Huebee
🐝 1-click color picker
Stars: ✭ 332 (-11.23%)
Mutual labels:  widget
Lumen Generators
A collection of generators for Lumen and Laravel 5.
Stars: ✭ 339 (-9.36%)
Mutual labels:  code-generator
Pickr
🎨 Flat, simple, multi-themed, responsive and hackable Color-Picker library. No dependencies, no jQuery. Compatible with all CSS Frameworks e.g. Bootstrap, Materialize. Supports alpha channel, rgba, hsla, hsva and more!
Stars: ✭ 3,759 (+905.08%)
Mutual labels:  widget
Moufette
🦨 Moufette is developer-friendly, open-source set of tools to collect users feedback. Find us on Twitter @MoufetteHQ
Stars: ✭ 303 (-18.98%)
Mutual labels:  widget
Serving
Kubernetes-based, scale-to-zero, request-driven compute
Stars: ✭ 4,238 (+1033.16%)
Mutual labels:  function
Reinforced.typings
Converts C# classes to TypeScript interfaces (and many more) within project build. 0-dependency, minimal, gluten-free
Stars: ✭ 341 (-8.82%)
Mutual labels:  code-generator
Flutter Settings Ui
Create native settings for Flutter app in a minutes.
Stars: ✭ 363 (-2.94%)
Mutual labels:  widget
Eww
ElKowar's wacky widgets
Stars: ✭ 322 (-13.9%)
Mutual labels:  widget
Flutter oktoast
a pure flutter toast library
Stars: ✭ 338 (-9.63%)
Mutual labels:  widget
Flutter radial menu
A simple animated radial menu widget for Flutter.
Stars: ✭ 359 (-4.01%)
Mutual labels:  widget
Autorest
OpenAPI (f.k.a Swagger) Specification code generator. Supports C#, PowerShell, Go, Java, Node.js, TypeScript, Python
Stars: ✭ 3,607 (+864.44%)
Mutual labels:  code-generator
Loopy
A code generator for array-based code on CPUs and GPUs
Stars: ✭ 367 (-1.87%)
Mutual labels:  code-generator
React Native Today Widget
iOS Today Widget in React Native
Stars: ✭ 320 (-14.44%)
Mutual labels:  widget
Svd2rust
Generate Rust register maps (`struct`s) from SVD files
Stars: ✭ 347 (-7.22%)
Mutual labels:  code-generator
Ctrlp Funky
A super simple function navigator for ctrlp.vim
Stars: ✭ 373 (-0.27%)
Mutual labels:  function
Javaparser
Java 1-15 Parser and Abstract Syntax Tree for Java, including preview features to Java 13
Stars: ✭ 3,972 (+962.03%)
Mutual labels:  code-generator
Direct Select Flutter
DirectSelect is a selection widget with an ethereal, full-screen modal popup displaying the available choices when the widget is interact with. https://dribbble.com/shots/3876250-DirectSelect-Dropdown-ux
Stars: ✭ 360 (-3.74%)
Mutual labels:  widget

Build pub package pub package

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);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].