import 'package:data_table_2/data_table_2.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:marking_app/common/model/report/question_know_point.dart';

class KnowPointTable extends StatefulWidget {
  final List<KnowPointList> knowPointList;

  KnowPointTable(this.knowPointList, {Key? key}) : super(key: key);

  @override
  State<KnowPointTable> createState() => _KnowPointTableState();
}

class _KnowPointTableState extends State<KnowPointTable> {
  List headList = ['题号', '知识点', '正确率', '得分率'];

  DataRow _getRow(int index, [Color? color]) {
    assert(index >= 0);
    var bodyItem = widget.knowPointList[index];
    return DataRow2.byIndex(
      index: index,
      color: color != null ? MaterialStateProperty.all(color) : null,
      cells:[
        DataCell(Center(
          child: Container(
            padding: EdgeInsets.symmetric(
                vertical: 6.r, horizontal: 5.r),
            child: Text(
              bodyItem.questionNums,
              style: TextStyle(
                  fontSize: 12.sp, color: Color(0xFF262626)),
            ),
          ),
        ),),
        DataCell(Container(
          padding: EdgeInsets.symmetric(
              vertical: 6.r, horizontal: 5.r),
          child: Text(
            bodyItem.name,
            style: TextStyle(
                fontSize: 12.sp, color: Color(0xFF262626)),
          ),
        ),),
        DataCell(Center(
          child: Container(
            padding: EdgeInsets.symmetric(
                vertical: 6.r, horizontal: 5.r),
            child: Text(
              bodyItem.correctRate,
              style: TextStyle(
                  fontSize: 12.sp, color: Color(0xFF262626)),
            ),
          ),
        ),),
        DataCell( Center(
          child: Container(
            padding: EdgeInsets.symmetric(
                vertical: 6.r, horizontal: 5.r),
            child: Text(
              bodyItem.gradeCorrectRate,
              style: TextStyle(
                  fontSize: 12.sp, color: Color(0xFF262626)),
            ),
          ),
        ),),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    int fixedRows = 1;
    int fixedCols = 0;
    final ScrollController _controller = ScrollController();
    int? _sortColumnIndex;
    bool _sortAscending = true;

    return DataTable2(
        dividerThickness: 0,
        scrollController: _controller,
        columnSpacing: 0,
        horizontalMargin: 0,
        dataRowHeight:50.r,
        bottomMargin: 0,
        border: TableBorder(
            horizontalInside: BorderSide(
                width: 1, color: Color(0xFFE0E5FF), style: BorderStyle.solid),
            bottom: BorderSide(
                width: 1, color: Color(0xFFE0E5FF), style: BorderStyle.solid),
            verticalInside: BorderSide(
                width: 1, color: Color(0xFFE0E5FF), style: BorderStyle.solid)),
        headingRowColor: MaterialStateProperty.resolveWith((states) =>
            fixedCols > 0 ? Color(0xFFF0F3FF) : Colors.transparent),
        /* headingRowDecoration: BoxDecoration(
          gradient: LinearGradient(
            colors: [
              Colors.grey[400]!,
              Colors.grey[200]!,
            ],
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
          ),
        ),*/
        headingRowDecoration: BoxDecoration(color: Color(0xFFF0F3FF)),
        fixedColumnsColor: Color(0xFFF0F3FF),
        fixedCornerColor: Colors.grey[400],
        // minWidth: 70.r * headList.length,
        fixedTopRows: fixedRows,
        fixedLeftColumns: fixedCols,
        sortColumnIndex: _sortColumnIndex,
        sortAscending: _sortAscending,
        // onSelectAll: (val) => setState(() => selectAll(val)),
        columns: List.generate(headList.length, (index) {
          var item = headList[index];
          return DataColumn2(
            label: Center(
              child: Text(item,
                  style: TextStyle(fontSize: 12.sp, color: Color(0xFF505767))),
            ),
            // size: ColumnSize.S,
          fixedWidth: index == 0?60.r:(MediaQuery.of(context).size.width - 90.r)/3,
          );
        }),
        rows: List<DataRow>.generate(widget.knowPointList.length,
            (index) => _getRow(index, Colors.transparent)));
  }
}
