Home > other >  How to Create dynamic Widget in Flutter
How to Create dynamic Widget in Flutter

Time:11-15

I am new to flutter development . I need to build a call back method which will return dynamic widget. Right now I am simple write down a static TextWidget. below is code

Widget buildArrayItemContentFromList(
  BuildContext ctx, Map<String, dynamic> data, int index) {
return Row(
  children: (this.itemBuilder as List).map<Widget>((field) {
    String value;
    if (field is Function)
      value = field(data);
    else
      value = data.containsKey(field) ? data[field].toString() : '';
    return Text(
      value,
      style: TextStyle(fontSize: 14),
    );
  }).toList(),
  mainAxisSize: MainAxisSize.max,
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
 );
}

My whole class is here

CodePudding user response:

ListView.builder(
        itemCount: field.length,
        itemBuilder: (BuildContext context,int index){
 String value;
    if (field is Function)
      value = field(data);
    else
      value = data.containsKey(field) ? data[field].toString() : '';
          return ListTile(
            title: Text(
      value,
      style: TextStyle(fontSize: 14),
    ),
            );
        }
        ),

CodePudding user response:

Create a List. Create a listView.builder with this list. In the method add the widget to the list and it's auto populated if you set it's state.

  • Related