import 'package:flutter/material.dart';

class PaginatedTableWidget<T> extends StatefulWidget {
  final List<DataColumn> columns;
  final List<DataRow> rows;
  final int initialRowsPerPage;
  final Function(int, int)? onPageChanged;
   final double columnSpacing; 

  const PaginatedTableWidget({
    required this.columns,
    required this.rows,
    this.initialRowsPerPage = 5,
    this.onPageChanged,
    this.columnSpacing = 16.0, // Default column spacing
  });

  @override
  _PaginatedTableWidgetState createState() => _PaginatedTableWidgetState();
}

class _PaginatedTableWidgetState extends State<PaginatedTableWidget> {
  late int rowsPerPage;

  @override
  void initState() {
    super.initState();
    rowsPerPage = widget.initialRowsPerPage;
  }

  @override
  Widget build(BuildContext context) {
    return PaginatedDataTable(
      rowsPerPage: rowsPerPage,
      availableRowsPerPage: [5, 10, 20, 50, 100],
      onRowsPerPageChanged: (newRowsPerPage) {
        setState(() {
          rowsPerPage = newRowsPerPage ?? rowsPerPage;
        });
      },
      columns: widget.columns,
      source: _DataTableSource(
        rows: widget.rows,
      ),
      columnSpacing: widget.columnSpacing, 
    );
  }
}

class _DataTableSource extends DataTableSource {
  final List<DataRow> rows;

  _DataTableSource({required this.rows});

  @override
  DataRow? getRow(int index) {
    if (index >= rows.length) {
      return null;
    }
    return rows[index];
  }

  @override
  int get rowCount => rows.length;

  @override
  bool get isRowCountApproximate => false;

  @override
  int get selectedRowCount => 0;
}
