Search code examples
flutterwidgetmemberextends

Custom Widget to extends Widget or use Widget as member?


I want to create a Custom Widget, I am not sure which one is better for my purpose, does anyone give suggestion? First way:

class ListItem extends LisTile {
  final TileType tileType;
  ...
}

Second way:

class ListItem {
  final TileType tileType;
  final ListTile tile;
  ...
}

Solution

  • Strongly recommended to use composition (this has-a that) rather than inheritance (this is-a that). Otherwise, you are tightly coupled with everything the Flutter team does to change upcoming releases that isn't necessarily part of the public interface.