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