简体   繁体   中英

How to create a dynamic ListView in Flutter?

I am attempting use TileList passed into ListView.Builder but receive the error: error: The argument type '(BuildContext, int, Items) → Card' can't be assigned to the parameter type '(BuildContext, int) → Widget'. (argument_type_not_assignable).

I have defined _makeCard and _makeTyle as a widget with no success.

import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:app_settings/app_settings.dart';
import 'package:coffee/models/settings_items.dart';

class Selections extends StatefulWidget {

  @override
  State<Selections> createState() => _Selections();
  final List<String> menu;
  Selections(this.menu);

}

class _Selections extends State<Selections> {

  List items;

  @override
  void initState() {
    items = getItems();
    super.initState();
  }

 Card _makeCard(BuildContext context, int index, Items item) {
    return Card(
      elevation: 8.0,
      margin: new EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0),
      child: Container(
        decoration: BoxDecoration(color: Color.fromRGBO(64, 75, 96, .0)),
        child: _buildMenu(context, index, item),
      ),
    );
  }


  ListTile _buildMenu(BuildContext context, int index, Items item) {
  return ListTile(
      contentPadding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
      leading: Container(
        padding: EdgeInsets.only(right: 12.0),
        decoration: new BoxDecoration(
          border: new Border(
            right: new BorderSide(width: 1.0, color: Colors.blue),
          ),
        ),
        child: Icon(
          Icons.location_on,
          color: Colors.redAccent,
        ),
      ),
      title: Text(
        item.title,
        style: TextStyle(color: Colors.blue, fontWeight: FontWeight.bold),
      ),
      onTap: () {
        AppSettings.openLocationSettings();
      },
    );
}

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListView.builder(
          itemBuilder: _makeCard,
          itemCount: 2,
          scrollDirection: Axis.vertical,
          shrinkWrap: true),
    );
  }

  List getItems() {
    return [
      Items(
          title: "Turn on Location Services!"
      ),
      Items(
          title: "My Profile"
      ),
    ];
  }


}

Two ListTIles will be displayed: one with the text "Turn on Location Services" and the other with "My Profile".

Remove the items parameter in _makeCard and retrieve it within the function instead:

Card _makeCard(BuildContext context, int index) {
  Items item = items[index];
  return Card(
    elevation: 8.0,
    margin: new EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0),
    child: Container(
      decoration: BoxDecoration(color: Color.fromRGBO(64, 75, 96, .0)),
      child: _buildMenu(context, index, item),
    ),
  );
}

OR

In build() use a higher order function for the itemBuilder parameter so the function parameters match:

return Container(
  child: ListView.builder(
    itemBuilder: (context, index) => _makeCard(context, index, items[index]),
    itemCount: 2,
    scrollDirection: Axis.vertical,
    shrinkWrap: true),
);

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