import 'package:flutter/material.dart';
import 'package:linked_scroll_controller/linked_scroll_controller.dart';
import 'table_cell.dart';

class TableBody extends StatefulWidget {
  // 所有点位 31 天的车流量日统计数据，每天有 3 个数据：车流量、早高峰、晚高峰
  Map mapTableData; // 有 31 个元素，每个元素又是一个 Map：包含车流量、早高峰、晚高峰
  final ScrollController scrollController;

  TableBody({
    @required this.scrollController,
    @required this.mapTableData,
  });

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

class _TableBodyState extends State<TableBody> {
  LinkedScrollControllerGroup _controllers;
  ScrollController _firstColumnController;
  ScrollController _restColumnsController;

  @override
  void initState() {
    super.initState();
    _controllers = LinkedScrollControllerGroup();
    _firstColumnController = _controllers.addAndGet();
    _restColumnsController = _controllers.addAndGet();
  }

  @override
  void dispose() {
    _firstColumnController.dispose();
    _restColumnsController.dispose();
    super.dispose();
  }

  List listWidth = [
    100.0,
    80.0,
    80.0,
    80.0,
  ];

  @override
  Widget build(BuildContext context) {
    return ListView(
      controller: _restColumnsController,
      physics: const ClampingScrollPhysics(),
      children: List.generate(widget.mapTableData.length, (index1) {
        // 降序生成
        int rIndex1 = widget.mapTableData.length - 1 - index1;
        String key1 = widget.mapTableData.keys.elementAt(rIndex1);
        return Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: List.generate(4, (index2) {
            String key2 = '';
            if (index2 > 0) {
              key2 = widget.mapTableData[key1].keys.elementAt(index2 - 1);
            }

            return MultiplicationTableCell(
              width: listWidth[index2],
              height: cellHeight,
              fontsize: 0 == index2 ? 14 : 16,
              edgeRight: ' ',
              text: 0 == index2
                  ? '${rIndex1 + 1}.${key1}'
                  : widget.mapTableData[key1][key2].toString(),
              //text: _text,
            );
          }),
        );
      }),
      // children: List.generate(31, (y) {
      //   return Row(
      //     mainAxisAlignment: MainAxisAlignment.center,
      //     children: List.generate(4, (x) {
      //       return MultiplicationTableCell(
      //         value: (x + 2) * (y + 2),
      //         width: listWidth[x],
      //         height: cellHeight,
      //         text: 0 == x ? '${y + 1}.${(x + 2) * (y + 2)}' : '',
      //       );
      //     }),
      //   );
      // }),
    );

    //Row(
    //children:
    //[
    // SizedBox(
    //   width: cellWidth,
    //   child: ListView(
    //     controller: _firstColumnController,
    //     physics: ClampingScrollPhysics(),
    //     children: List.generate(maxNumber - 1, (index) {
    //       return MultiplicationTableCell(
    //         //color: Colors.yellow.withOpacity(0.3),
    //         value: index + 2,
    //       );
    //     }),
    //   ),
    // ),

    // Expanded(
    //   child: SingleChildScrollView(
    //     controller: widget.scrollController,
    //     scrollDirection: Axis.horizontal,
    //     physics: const ClampingScrollPhysics(),
    //     child: SizedBox(
    //       width: (maxNumber - 1) * cellWidth,
    //       child: ListView(
    //         controller: _restColumnsController,
    //         physics: const ClampingScrollPhysics(),
    //         children: List.generate(maxNumber - 1, (y) {
    //           return Row(
    //             children: List.generate(maxNumber - 1, (x) {
    //               return MultiplicationTableCell(
    //                 value: (x + 2) * (y + 2),
    //               );
    //             }),
    //           );
    //         }),
    //       ),
    //     ),
    //   ),
    // ),
    //],
    //);
  }
}
