skip to Main Content

Good afternoon.

Do you know if there is in Flutter something similar to the .NET Tag property or the Qt setProperty property? Basically I want to be able to assign an object, string… to any widget in Flutter.

Thank you very much.

2

Answers


  1. Chosen as BEST ANSWER

    I have found a solution that I like better. It is the Extension methods, a way to add functionalities to a library knowing nothing about its internal implementation. An example in my case:

    extension myWidget on Widget {
        static Object _data = Object();
    
        Object get tag => _data;
        set tag(Object tag) {
        _data = tag;
        }
    }
    
    Widget setTag(Widget wdg, Object tag) {
      wdg.tag = tag;
      return wdg;
    }
    

    Usage:

    Container(
              width: 500,
              child: setTag(
                  TextField(
                    obscureText: false,
                    decoration: const InputDecoration(
                      border: OutlineInputBorder(),
                      labelText: 'Nº Historia Clínica',
                    ),
                  ),
                  "###AUTOCAMPO###|HJ_07_RADIODIAGNOSTICO|HOJA_REFERENCIA|ENTEROTEXTO"
              )
    

  2. A basic example, as a followup to the comments:

    enum DataType {
      text,
      number,
      date,
      time,
      dateTime,
      boolean,
    }
    
    class FieldData<T> {
      final String table;
      final DataType type;
      final T value;
    
      FieldData({
        required this.table,
        required this.type,
        required this.value,
      });
    }
    
    class SpecialTextField extends StatelessWidget {
      const SpecialTextField({
        Key? key,
        required this.table,
        required this.type,
        required this.onChanged,
        this.controller,
        this.decoration,
      }) : super(key: key);
    
      final String table;
      final DataType type;
      final Function(FieldData) onChanged;
    
      // Here you declare TextField's properties you need
      // to use in your widget, and then pass them to TextField
      final TextEditingController? controller;
      final InputDecoration? decoration;
    
      @override
      Widget build(BuildContext context) {
        return TextField(
          controller: controller,
          decoration: decoration,
          onChanged: (value) => onChanged(
            FieldData(
              table: table,
              type: type,
              value: value,
            ),
          ),
        );
      }
    }
    

    And to use it, you can do:

    SpecialTextField(
      table: 'users',
      type: DataType.text,
      onChanged: (data) {
        print('Table: ${data.table}');
        print('Type: ${data.type.name}');
        print('Value: ${data.value}');
      },
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search