简体   繁体   中英

How to Create dynamic Widget in Flutter

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

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.

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),
    ),
            );
        }
        ),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM