LCOV - code coverage report
Current view: top level - src - chip_builder.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 40 41 97.6 %
Date: 2020-03-09 19:58:44 Functions: 0 0 -

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

Generated by: LCOV version 1.14