Files
syncrow-web/lib/pages/common/custom_table.dart
ashrafzarkanisala 982954be42 add table view
2024-08-24 14:03:13 +03:00

176 lines
4.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:syncrow_web/utils/color_manager.dart';
class DynamicTable extends StatefulWidget {
final List<String> headers;
final List<List<dynamic>> data;
final BoxDecoration? headerDecoration;
final BoxDecoration? cellDecoration;
final Size size;
final bool withCheckBox;
final void Function(bool?)? selectAll;
final void Function(int, bool?)? onRowCheckboxChanged;
const DynamicTable({
Key? key,
required this.headers,
required this.data,
required this.size,
required this.withCheckBox,
this.headerDecoration,
this.cellDecoration,
this.selectAll,
this.onRowCheckboxChanged,
}) : super(key: key);
@override
_DynamicTableState createState() => _DynamicTableState();
}
class _DynamicTableState extends State<DynamicTable> {
late List<bool> _selected;
bool _selectAll = false;
@override
void initState() {
super.initState();
_selected = List<bool>.filled(widget.data.length, false);
}
void _toggleSelectAll(bool? value) {
setState(() {
_selectAll = value ?? false;
_selected = List<bool>.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(10.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(),
],
),
),
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),
...row
.map((cell) =>
_buildTableCell(cell.toString()))
.toList(),
],
);
},
),
),
),
],
),
),
],
),
),
);
}
Widget _buildSelectAllCheckbox() {
return SizedBox(
width: 50,
child: Checkbox(
value: _selectAll,
onChanged: _toggleSelectAll,
),
);
}
Widget _buildRowCheckbox(int index) {
return SizedBox(
width: 50,
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,
color: ColorsManager.lightGreyColor)),
),
),
);
}
Widget _buildTableCell(String content) {
return Expanded(
child: Container(
height: 80,
padding: const EdgeInsets.all(15.0),
decoration: const BoxDecoration(
border: Border(
bottom: BorderSide(
color: ColorsManager.boxDivider,
width: 1.0,
),
)),
alignment: Alignment.centerLeft,
child: Text(
content,
style: const TextStyle(color: Colors.black, fontSize: 12),
),
),
);
}
}