import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:syncrow_web/utils/color_manager.dart'; import 'package:syncrow_web/utils/constants/assets.dart'; class DynamicTable extends StatefulWidget { final List headers; final List> data; final BoxDecoration? headerDecoration; final BoxDecoration? cellDecoration; final Size size; final bool withCheckBox; final bool isEmpty; final void Function(bool?)? selectAll; final void Function(int, bool?)? onRowCheckboxChanged; final List? initialSelectedIds; const DynamicTable({ super.key, required this.headers, required this.data, required this.size, required this.isEmpty, required this.withCheckBox, this.headerDecoration, this.cellDecoration, this.selectAll, this.onRowCheckboxChanged, this.initialSelectedIds, }); @override _DynamicTableState createState() => _DynamicTableState(); } class _DynamicTableState extends State { late List _selected; bool _selectAll = false; @override void initState() { super.initState(); _selected = List.generate(widget.data.length, (index) { return widget.initialSelectedIds != null && widget.initialSelectedIds!.contains(widget.data[index][1]); }); _selectAll = _selected.every((element) => element == true); } void _toggleSelectAll(bool? value) { setState(() { _selectAll = value ?? false; _selected = List.filled(widget.data.length, _selectAll); if (widget.selectAll != null) { widget.selectAll!(_selectAll); } }); } void _toggleRowSelection(int index, bool? value) { setState(() { _selected[index] = value ?? false; _selectAll = _selected.every((element) => element == true); if (widget.onRowCheckboxChanged != null) { widget.onRowCheckboxChanged!(index, _selected[index]); } }); } @override Widget build(BuildContext context) { return Container( decoration: widget.cellDecoration, child: Padding( padding: const EdgeInsets.all(2.0), child: ListView( scrollDirection: Axis.horizontal, children: [ SizedBox( width: widget.size.width, child: Column( children: [ Container( decoration: widget.headerDecoration ?? BoxDecoration(color: Colors.grey[200]), child: Row( children: [ if (widget.withCheckBox) _buildSelectAllCheckbox(), ...widget.headers .map((header) => _buildTableHeaderCell(header)) .toList(), ], ), ), widget.isEmpty? Expanded( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Row( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ Column( children: [ SvgPicture.asset( Assets.emptyTable ), const SizedBox(height: 15,), Text('No Passwords',style: Theme.of(context).textTheme.bodySmall!.copyWith(color:ColorsManager.grayColor ),) ], ), ], ), ], ), ): Expanded( child: Container( color: Colors.white, child: ListView.builder( shrinkWrap: true, itemCount: widget.data.length, itemBuilder: (context, index) { final row = widget.data[index]; return Row( children: [ if (widget.withCheckBox) _buildRowCheckbox(index,widget.size.height*0.10), ...row.map((cell) => _buildTableCell(cell.toString(),widget.size.height*0.10)).toList(), ], ); }, ), ), ), ], ), ), ], ), ), ); } Widget _buildSelectAllCheckbox() { return Container( padding: const EdgeInsets.all(8.0), decoration: const BoxDecoration( border: Border.symmetric( vertical: BorderSide(color: ColorsManager.boxDivider))), child: Checkbox( value: _selectAll, onChanged: _toggleSelectAll, ), ); } Widget _buildRowCheckbox(int index,size) { return Container( padding: const EdgeInsets.all(8.0), height:size , decoration: const BoxDecoration( border: Border( bottom: BorderSide( color: ColorsManager.boxDivider, width: 1.0, ), )), alignment: Alignment.centerLeft, child: Center(child: Checkbox( value: _selected[index], onChanged: (bool? value) { _toggleRowSelection(index, value); }, ),) ); } Widget _buildTableHeaderCell(String title) { return Expanded( child: Container( decoration: const BoxDecoration( border: Border.symmetric( vertical: BorderSide(color: ColorsManager.boxDivider))), alignment: Alignment.centerLeft, child: Padding( padding: const EdgeInsets.all(8.0), child: Text(title, style: const TextStyle(fontWeight: FontWeight.w400,fontSize: 13,color: Color(0xFF999999))), ), ), ); } Widget _buildTableCell(String content,size) { Color? statusColor; switch (content) { case 'Effective': statusColor = ColorsManager.textGreen; break; case 'Expired': statusColor = ColorsManager.red; break; case 'To be effective': statusColor = ColorsManager.yaGreen; break; default: statusColor = Colors.black; // Default color } return Expanded( child: Container( height:size , padding: const EdgeInsets.all(5.0), decoration: const BoxDecoration( border: Border( bottom: BorderSide( color: ColorsManager.boxDivider, width: 1.0, ), )), alignment: Alignment.centerLeft, child: Text( content, style: TextStyle( color:statusColor, // Use the passed color or default to black fontSize: 10, fontWeight: FontWeight.w400 ), ), ), ); } }