Advertisement
gspencer

Chip Theming

Jul 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.22 KB | None | 0 0
  1. class ActorFilterEntry {
  2.   const ActorFilterEntry(this.name, this.initials);
  3.   final String name;
  4.   final String initials;
  5. }
  6.  
  7. class CastFilter extends StatefulWidget {
  8.   @override
  9.   State createState() => new CastFilterState();
  10. }
  11.  
  12. class CastFilterState extends State<CastFilter> {
  13.   final List<ActorFilterEntry> _cast = <ActorFilterEntry>[
  14.     const ActorFilterEntry('Aaron Burr', 'AB'),
  15.     const ActorFilterEntry('Alexander Hamilton', 'AH'),
  16.     const ActorFilterEntry('Eliza Hamilton', 'EH'),
  17.     const ActorFilterEntry('James Madison', 'JM'),
  18.   ];
  19.   List<String> _filters = <String>[];
  20.   static const Color _selectedBackgroundColor = const Color(0x1f0000ff);
  21.   static const Color _unselectedLabelColor = const Color(0xde000000);
  22.   static const Color _selectedLabelColor = const Color(0xde0000ff);
  23.  
  24.   Iterable<Widget> get actorWidgets sync* {
  25.     ChipThemeData currentTheme = ChipTheme.of(context);
  26.     for (ActorFilterEntry actor in _cast) {
  27.       bool selected = _filters.contains(actor.name);
  28.       yield new Padding(
  29.         padding: const EdgeInsets.all(4.0),
  30.         child: ChipTheme(
  31.           data: currentTheme.copyWith(
  32.             selectedColor: _selectedBackgroundColor,
  33.             labelStyle: currentTheme.labelStyle.copyWith(
  34.               color: selected ? _selectedLabelColor : _unselectedLabelColor,
  35.             ),
  36.           ),
  37.           child: new FilterChip(
  38.             avatar: new CircleAvatar(child: new Text(actor.initials)),
  39.             label: new Text(actor.name),
  40.             selected: selected,
  41.             onSelected: (bool value) {
  42.               setState(() {
  43.                 if (value) {
  44.                   _filters.add(actor.name);
  45.                 } else {
  46.                   _filters.removeWhere((String name) {
  47.                     return name == actor.name;
  48.                   });
  49.                 }
  50.               });
  51.             },
  52.           ),
  53.         ),
  54.       );
  55.     }
  56.   }
  57.  
  58.   @override
  59.   Widget build(BuildContext context) {
  60.     return Column(
  61.       mainAxisAlignment: MainAxisAlignment.center,
  62.       children: <Widget>[
  63.         new Wrap(
  64.           children: actorWidgets.toList(),
  65.         ),
  66.         new Text('Look for: ${_filters.join(', ')}'),
  67.       ],
  68.     );
  69.   }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement