Line data Source code
1 : import 'package:flutter/material.dart';
2 : import 'package:flutter/widgets.dart';
3 :
4 : abstract class ChipBuilder {
5 : Widget build(BuildContext context, Widget child, int index, bool active);
6 : }
7 :
8 : /// Simple badge with num inside
9 : class DefaultChipBuilder extends ChipBuilder {
10 : final Map<int, dynamic> chips;
11 : final Color textColor;
12 : final Color badgeColor;
13 : final EdgeInsets padding;
14 : final double borderRadius;
15 :
16 1 : DefaultChipBuilder(
17 : this.chips, {
18 : this.textColor,
19 : this.badgeColor,
20 : this.padding,
21 : this.borderRadius,
22 : });
23 :
24 1 : @override
25 : Widget build(_, child, i, active) {
26 2 : var chip = chips[i];
27 1 : if (chip == null || chip == '') {
28 : return child;
29 : }
30 1 : return Stack(
31 : alignment: Alignment.center,
32 2 : children: <Widget>[child, asBadge(chip)],
33 : );
34 : }
35 :
36 1 : Widget asBadge(dynamic chip) {
37 1 : if (chip is String) {
38 1 : return Positioned.fill(
39 1 : child: Align(
40 : alignment: Alignment.topRight,
41 1 : child: Container(
42 1 : margin: EdgeInsets.only(top: 10, right: 10),
43 2 : padding: padding ?? EdgeInsets.only(left: 4, right: 4),
44 1 : decoration: BoxDecoration(
45 : shape: BoxShape.rectangle,
46 1 : color: badgeColor ?? Colors.redAccent,
47 2 : borderRadius: BorderRadius.circular(borderRadius ?? 20),
48 : ),
49 1 : child: Text(
50 : chip,
51 2 : style: TextStyle(color: textColor ?? Colors.white, fontSize: 12),
52 : ),
53 : ),
54 : ),
55 : );
56 1 : } else if (chip is IconData) {
57 1 : return Positioned.fill(
58 1 : child: Align(
59 : alignment: Alignment.topRight,
60 1 : child: Container(
61 1 : margin: EdgeInsets.only(top: 10, right: 10),
62 2 : padding: padding ?? EdgeInsets.only(left: 4, right: 4),
63 2 : child: Icon(chip, color: badgeColor ?? Colors.redAccent, size: 14),
64 : ),
65 : ),
66 : );
67 1 : } else if (chip is Widget) {
68 1 : return Positioned.fill(
69 1 : child: Align(
70 : alignment: Alignment.topRight,
71 1 : child: Container(
72 1 : margin: EdgeInsets.only(top: 10, right: 10),
73 2 : padding: padding ?? EdgeInsets.only(left: 4, right: 4),
74 : child: chip,
75 : ),
76 : ),
77 : );
78 1 : } else if (chip is Color) {
79 1 : return Positioned.fill(
80 1 : child: Align(
81 : alignment: Alignment.topRight,
82 1 : child: Container(
83 1 : margin: EdgeInsets.only(top: 10, right: 10),
84 2 : padding: padding ?? EdgeInsets.only(left: 4, right: 4),
85 1 : child: Container(
86 1 : decoration: BoxDecoration(
87 : shape: BoxShape.circle,
88 1 : color: badgeColor ?? Colors.redAccent),
89 : width: 10,
90 : height: 10,
91 : ),
92 : ),
93 : ),
94 : );
95 : } else {
96 0 : return Container();
97 : }
98 : }
99 : }
|