исправить время сортировки в таблице - PullRequest
0 голосов
/ 04 августа 2020

Я пытаюсь создать свою собственную таблицу, но у меня возникли проблемы с сортировкой времени с 8:00 до 12:00. Например, у меня сначала 10 утра, а потом 8 утра, так как отсортировать время по порядку? Также зачем добавлять лишний текст в таблицу?

скриншот:

введите описание изображения здесь

модель:

class Subject {
  String CourseId;
  String CourseTime;
  String CourseDay;
  String CoursePlace;
  String Color;
  int id;

  Subject(this.CourseId, this.CourseTime, this.CourseDay, this.CoursePlace,
      this.Color);
}

полный код:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:squ_life/TimeTable/model/table_model.dart';

class TableCreator extends StatefulWidget {
  @override
  _TableCreatorState createState() => _TableCreatorState();
}

class _TableCreatorState extends State<TableCreator> {
  List<Subject> items = List();

  TextStyle _textStyle =
      TextStyle(color: Colors.white, fontSize: 18, fontFamily: 'ar');
  TextStyle _textCell = TextStyle(color: Color(0xff34495e), fontSize: 14);

  @override
  void initState() {
    super.initState();
    items.add(Subject(
        'section 11',
        'SUN 10:00-13:50',
        'null',
        'building pne',
        'null'));
items.add(Subject(
    'section 10',
    'SUN 08:00-13:50',
    'null',
    'building pne',
    'null'));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(
            'my table',
            style: _textStyle,
          ),
          backgroundColor: Color(0xff2c3e50),
          elevation: 10,
          centerTitle: true,
        ),
        body: Padding(
            padding: EdgeInsets.all(10),
            child: ListView(
              children: <Widget>[createTabel()],
            )));
  }

  Widget createTabel() {
    List<Subject> from8to10 = [];
    List<TableRow> rows = [];
    String sunday, monday, tuesday, wednesday, thursday;
    rows.clear();
    from8to10.clear();
    sunday = '';
    monday = '';
    tuesday = '';
    wednesday = '';
    thursday = '';
    void rowCreator(String start, String end, var course) {

        if (course.CourseTime.contains('SUN')) {
          sunday = sunday +
              course.CourseId.toString() +
              '\n' +
              course.CoursePlace.toString() +
              '\n';

        }
        else if (course.CourseTime.contains('MON')) {
          monday = monday +
              course.CourseId.toString() +
              '\n' +
              course.CoursePlace.toString() +
              '\n';
        }
        else if (course.CourseTime.contains('TUE')) {
          tuesday = tuesday +
              course.CourseId.toString() +
              '\n' +
              course.CoursePlace.toString() +
              '\n';
        }
        else if (course.CourseTime.contains('WED')) {
          wednesday = wednesday +
              course.CourseId.toString() +
              '\n' +
              course.CoursePlace.toString() +
              '\n';
        }
        else if (course.CourseTime.contains('THU')) {
          thursday = thursday +
              course.CourseId.toString() +
              '\n' +
              course.CoursePlace.toString() +
              '\n';
        }

      setState(() {
        print(sunday);
        rows.add(TableRow(
          decoration: BoxDecoration(
            color: Colors.white,
          ),
          children: <Widget>[
            Container(
              padding: EdgeInsets.all(10),
              child: Text(
                start + ' ' + end,
                textAlign: TextAlign.center,
                style: _textCell,
              ),
            ),
            sunday == ''
                ? Text(
                    sunday,
                    textAlign: TextAlign.center,
                    style: _textCell,
                  )
                : Text(
                    sunday,
                    textAlign: TextAlign.center,
                    style: _textCell,
                  ),
            monday == ''
                ? Text(
                    ' ',
                    textAlign: TextAlign.center,
                    style: _textCell,
                  )
                : Text(
                    monday,
                    textAlign: TextAlign.center,
                    style: _textCell,
                  ),
            tuesday == ''
                ? Text(
                    ' ',
                    textAlign: TextAlign.center,
                    style: _textCell,
                  )
                : Text(
                    tuesday,
                    textAlign: TextAlign.center,
                    style: _textCell,
                  ),
            wednesday == ''
                ? Text(
                    ' ',
                    textAlign: TextAlign.center,
                    style: _textCell,
                  )
                : Text(
                    wednesday,
                    textAlign: TextAlign.center,
                    style: _textCell,
                  ),
            thursday == ''
                ? Text(
                    ' ',
                    textAlign: TextAlign.center,
                    style: _textCell,
                  )
                : Text(
                    thursday,
                    textAlign: TextAlign.center,
                    style: _textCell,
                  ),
          ],
        ));
      });
    }

    rows.add(TableRow(
        decoration: BoxDecoration(
          color: Color(0xff34495e),
        ),
        children: [
          Container(
            padding: EdgeInsets.all(10),
            child: Text(
              'Time / Day',
              textAlign: TextAlign.center,
              style: _textStyle,
            ),
          ),
          Text(
            'Sun',
            textAlign: TextAlign.center,
            style: _textStyle,
          ),
          Text(
            'Mon',
            textAlign: TextAlign.center,
            style: _textStyle,
          ),
          Text(
            'Tue',
            textAlign: TextAlign.center,
            style: _textStyle,
          ),
          Text(
            'Wed',
            textAlign: TextAlign.center,
            style: _textStyle,
          ),
          Text(
            'Thu',
            textAlign: TextAlign.center,
            style: _textStyle,
          ),
        ]));
    items.forEach((item) {
      String startDate;
      String endDate;
      print(item.CourseTime);
      item.CourseTime = item.CourseTime.trim();
      startDate = item.CourseTime.substring(3, 9);
      endDate = item.CourseTime.substring(9, 15).replaceAll('-', '');

      return rowCreator(startDate, endDate, item);
    });

    return Table(
      children: rows,
      border: TableBorder.all(color: Color(0xff2c3e50), width: 2),
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
    );
  }
}

1 Ответ

1 голос
/ 04 августа 2020

Вы можете скопировать и вставить полный код ниже
Вы можете использовать items.sort, и вам не нужно объединять sunday
фрагмент кода

items.sort((a, b) => a.CourseTime.compareTo(b.CourseTime));
...
sunday = course.CourseId.toString() +
            '\n' +
            course.CoursePlace.toString() +
            '\n';

рабочая демонстрация

введите описание изображения здесь

полный код

import 'package:flutter/material.dart';

class TableCreator extends StatefulWidget {
  @override
  _TableCreatorState createState() => new _TableCreatorState();
}

class _TableCreatorState extends State<TableCreator> {
  List<Subject> items = new List();

  TextStyle _textStyle =
      new TextStyle(color: Colors.white, fontSize: 18, fontFamily: 'ar');
  TextStyle _textCell = new TextStyle(color: Color(0xff34495e), fontSize: 14);

  @override
  void initState() {
    super.initState();
    items.add(Subject(
        'section 11', 'SUN 10:00-13:50', 'null', 'building pne', 'null'));
    items.add(Subject(
        'section 10', 'SUN 08:00-13:50', 'null', 'building pne', 'null'));

    items.sort((a, b) => a.CourseTime.compareTo(b.CourseTime));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(
            'my table',
            style: _textStyle,
          ),
          backgroundColor: Color(0xff2c3e50),
          elevation: 10,
          centerTitle: true,
        ),
        body: Padding(
            padding: EdgeInsets.all(10),
            child: ListView(
              children: <Widget>[createTabel()],
            )));
  }

  Widget createTabel() {
    List<Subject> from8to10 = [];
    List<TableRow> rows = [];
    String sunday, monday, tuesday, wednesday, thursday;
    rows.clear();
    from8to10.clear();
    sunday = '';
    monday = '';
    tuesday = '';
    wednesday = '';
    thursday = '';
    void rowCreator(String start, String end, var course) {
      if (course.CourseTime.contains('SUN')) {
        sunday = course.CourseId.toString() +
            '\n' +
            course.CoursePlace.toString() +
            '\n';
      } else if (course.CourseTime.contains('MON')) {
        monday = monday +
            course.CourseId.toString() +
            '\n' +
            course.CoursePlace.toString() +
            '\n';
      } else if (course.CourseTime.contains('TUE')) {
        tuesday = tuesday +
            course.CourseId.toString() +
            '\n' +
            course.CoursePlace.toString() +
            '\n';
      } else if (course.CourseTime.contains('WED')) {
        wednesday = wednesday +
            course.CourseId.toString() +
            '\n' +
            course.CoursePlace.toString() +
            '\n';
      } else if (course.CourseTime.contains('THU')) {
        thursday = thursday +
            course.CourseId.toString() +
            '\n' +
            course.CoursePlace.toString() +
            '\n';
      }

      setState(() {
        print(sunday);
        rows.add(TableRow(
          decoration: BoxDecoration(
            color: Colors.white,
          ),
          children: <Widget>[
            Container(
              padding: EdgeInsets.all(10),
              child: Text(
                start + ' ' + end,
                textAlign: TextAlign.center,
                style: _textCell,
              ),
            ),
            sunday == ''
                ? Text(
                    sunday,
                    textAlign: TextAlign.center,
                    style: _textCell,
                  )
                : Text(
                    sunday,
                    textAlign: TextAlign.center,
                    style: _textCell,
                  ),
            monday == ''
                ? Text(
                    ' ',
                    textAlign: TextAlign.center,
                    style: _textCell,
                  )
                : Text(
                    monday,
                    textAlign: TextAlign.center,
                    style: _textCell,
                  ),
            tuesday == ''
                ? Text(
                    ' ',
                    textAlign: TextAlign.center,
                    style: _textCell,
                  )
                : Text(
                    tuesday,
                    textAlign: TextAlign.center,
                    style: _textCell,
                  ),
            wednesday == ''
                ? Text(
                    ' ',
                    textAlign: TextAlign.center,
                    style: _textCell,
                  )
                : Text(
                    wednesday,
                    textAlign: TextAlign.center,
                    style: _textCell,
                  ),
            thursday == ''
                ? Text(
                    ' ',
                    textAlign: TextAlign.center,
                    style: _textCell,
                  )
                : Text(
                    thursday,
                    textAlign: TextAlign.center,
                    style: _textCell,
                  ),
          ],
        ));
      });
    }

    rows.add(TableRow(
        decoration: BoxDecoration(
          color: Color(0xff34495e),
        ),
        children: [
          Container(
            padding: EdgeInsets.all(10),
            child: Text(
              'Time / Day',
              textAlign: TextAlign.center,
              style: _textStyle,
            ),
          ),
          Text(
            'Sun',
            textAlign: TextAlign.center,
            style: _textStyle,
          ),
          Text(
            'Mon',
            textAlign: TextAlign.center,
            style: _textStyle,
          ),
          Text(
            'Tue',
            textAlign: TextAlign.center,
            style: _textStyle,
          ),
          Text(
            'Wed',
            textAlign: TextAlign.center,
            style: _textStyle,
          ),
          Text(
            'Thu',
            textAlign: TextAlign.center,
            style: _textStyle,
          ),
        ]));
    items.forEach((item) {
      String startDate;
      String endDate;
      print(item.CourseTime);
      item.CourseTime = item.CourseTime.trim();
      startDate = item.CourseTime.substring(3, 9);
      endDate = item.CourseTime.substring(9, 15).replaceAll('-', '');

      return rowCreator(startDate, endDate, item);
    });

    return Table(
      children: rows,
      border: TableBorder.all(color: Color(0xff2c3e50), width: 2),
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
    );
  }
}

class Subject {
  String CourseId;
  String CourseTime;
  String CourseDay;
  String CoursePlace;
  String Color;
  int id;

  Subject(this.CourseId, this.CourseTime, this.CourseDay, this.CoursePlace,
      this.Color);
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: TableCreator(),
    );
  }
}
...