Skip to content

[RFW] Add support for widget builders  #141658

@tugorez

Description

@tugorez

Problem

A lot of widgets in flutter follow the builder pattern where the host widget or the builder provides extra data to the hosted or child widget so that it can be rendered properly. Consider for example the TweenAnimationBuilder widget. A widget like that would be impossible to support in RFW in an idiomatic way. Ideally we should be able to write

widget Foo = TweenAnimationBuilder(
  tween: {
    type: 'double',
    begin: 0,
    end: 100,
  },
  builder: (scope) => IconButton(
    iconSize: scope.size,
    icon: Icon(...),
  ),
);

and have that referenced in a local widget in the following form

   'TweenAnimationBuilder': (BuildContext context, DataSource source) {       
       return TweenAnimationBuilder(                                            
         tween: ...,                              
         duration: ...,    
         builder: (BuildContext context, double value, Widget? child) {
           final DynamicMap scope = <String, Object?>{ 
             'size': value,
           };
           final Widget widget = source.builder(['builder'], scope);
           return widget;
         },                                                                     
       );                                                                       
     },

Proposal

Add support for widget builder values. The fact that this would be a value makes the following examples valid.

widget TextBuilder = Builder(
  builder: (scope) => Text(text: scope.title);
);

widget TextBuilder = Builder(
   builder: ags.text, // where args is a builder
);

widget { text: (scope) => Text(text: scope.title)  } TextBuilder = Builder(
  builder: state.text
); 

and in theory it also supports widget builders passed as data (althought it is limited by the existing Dart API).

widget TextBuilder = Builder(
   builder: data.text, // where data.text is a builder
);

We should also be able to nest builders hence this should work.

widget TextBuilder = Builder(
   builder: (scope1) => Builder(
     builder: (scope2) => Builder(
         builder: (scope3) => Text(text: [scope1.text, scope2.text, scope3.text]),
      ), 
   ),
);

The following code should fail.

widget Foo = (scope) => Text(text: scope.text);

Do note that the following pattern, in practical terms, allows local widgets to directly pass information to a given remote widget.

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listp: rfwRemote Flutter Widgets packagepackageflutter/packages repository. See also p: labels.team-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions