UNeverNo
UNeverNo

Reputation: 583

How to set icon size for childs on parent widget

I'm creating a widget where I want to set the the size of the icons used in the sub-widgets globally.

class ItemsContainer extends StatefulWidget {
  final List<Item> items;
  final double iconSize; //global default

  const ItemsContainer({
    @required this.items,
    this.iconSize = 56.0,
  });
}

class Item {
  final Icon icon;

  const Item ({
    @required this.icon,
  });
}

What I'd like to do is this:

for (var item in items) {
  if (item.size == null)
    item.size = iconSize;
}

The problem I face is, that I can't set the size due to the fact, that I have a const constructor.

I could clone an existing Icon and change the original size, but is there a better way to do it?

Icon _getSizedIcon(Icon icon, double size) {
  return icon.size != null ? icon :
    Icon(icon.icon,
      size: size,
      color: icon.color,
      key: icon.key,
      semanticLabel: icon.semanticLabel,
      textDirection: icon.textDirection,
    );
}

Upvotes: 0

Views: 346

Answers (1)

Tom Robinson
Tom Robinson

Reputation: 591

The IconTheme widget is what you probably want:

https://api.flutter.dev/flutter/widgets/IconTheme-class.html

Upvotes: 1

Related Questions