如何使用文本字段颤动中的位置动态过滤列表

How to filter list dynamically using where in Text field flutter

我正在尝试使用 if/else、分组列表、地图等动态创建一个 3 层列表,但没有成功,所以我正在尝试 filter/display 使用 .where 方法列出。

问题 如何使用 .where

根据第二个字段(courseheader)的值打印字段(subjectName)的值
itemCount: allCoursesList.length,
itemBuilder: (context, index) {
  return ListTile(
    title: Text(
        '${allCoursesList[index].subjectName.where((index) => index.courseheader != 'no')}'),
    subtitle: ListTile(
      title: Text('${allCoursesList.where((index) => index.courseheader != 'no')}'),
      subtitle: ListTile(
        title: Text('${allCoursesList.where((index) => index.courseSection != 'no')}'),
      ),
    ),
  );
}

期望的输出

Biology
BIOL 101
-B101-1
-B101-2
-B101-3
BIOL 102
-B102-1
-B102-2
……
Chemistry
CHEM 101
-C101-1
….

列表来自 Table

[Courses{subjectId: 1, subjectName: Biology, courseName: BIOL 101, courseSection: B101-1, subjectHeader: yes, courseHeader: yes, section: yes,}, Courses{subjectId: 1, subjectName: Biology, courseName: BIOL 101, courseSection: B101-2, subjectHeader: no, courseHeader: no, section: yes,}, Courses{subjectId: 1, subjectName: Biology, courseName: BIOL 101, courseSection: B101-3, subjectHeader: no, courseHeader: no, section: yes,}, Courses{subjectId: 1, subjectName: Biology, courseName: BIOL 102, courseSection: B102-1, subjectHeader: no, courseHeader: yes, section: yes,}, Courses{subjectId: 1, subjectName: Biology, courseName: BIOL 102, courseSection: B102-2, subjectHeader: no, courseHeader: no, section: yes,}, Courses{subjectId: 1, subjectName: Biology, courseName: BIOL 103, courseSection: B103-1, subjectHeader: no, courseHeader: yes, section: yes,}, Courses{subjectId: 1, subjectName: Biology, courseName: BIOL 104, courseSection: B104-1, subjectHeader: no, courseHeader: yes, section: yes,}

我不熟悉你的数据结构(我想它来自 FireBase),所以我将它转换为 Map class。但是下面的代码应该仍然适用于数据结构。

data_raw.dart

const raw_data = '''
[Courses{subjectId: 1, subjectName: Biology, courseName: BIOL 101, courseSection: B101-1, subjectHeader: yes, courseHeader: yes, section: yes,}, Courses{subjectId: 1, subjectName: Biology, courseName: BIOL 101, courseSection: B101-2, subjectHeader: no, courseHeader: no, section: yes,}, Courses{subjectId: 1, subjectName: Biology, courseName: BIOL 101, courseSection: B101-3, subjectHeader: no, courseHeader: no, section: yes,}, Courses{subjectId: 1, subjectName: Biology, courseName: BIOL 102, courseSection: B102-1, subjectHeader: no, courseHeader: yes, section: yes,}, Courses{subjectId: 1, subjectName: Biology, courseName: BIOL 102, courseSection: B102-2, subjectHeader: no, courseHeader: no, section: yes,}, Courses{subjectId: 1, subjectName: Biology, courseName: BIOL 103, courseSection: B103-1, subjectHeader: no, courseHeader: yes, section: yes,}, Courses{subjectId: 1, subjectName: Biology, courseName: BIOL 104, courseSection: B104-1, subjectHeader: no, courseHeader: yes, section: yes,}, Courses{subjectId: 2, subjectName: Chemistry, courseName: CHEM 101, courseSection: C104-1, subjectHeader: yes, courseHeader: yes, section: yes,}, Courses{subjectId: 2, subjectName: Chemistry, courseName: CHEM 101, courseSection: C104-2, subjectHeader: no, courseHeader: no, section: yes,}, Courses{subjectId: 2, subjectName: Chemistry, courseName: CHEM 101, courseSection: C104-3, subjectHeader: no, courseHeader: no, section: yes,}]
''';

home_page.dart

import 'package:flutter/material.dart';
import 'package:read_json3/src/shared/data_raw.dart';


class HomePage extends StatelessWidget {
  /* ---------------------------------------------------------------------------- */
  const HomePage({Key key}) : super(key: key);
  /* ---------------------------------------------------------------------------- */
  @override
  Widget build(BuildContext context) {
    final data = getNewData();

    return Scaffold(
      appBar: AppBar(
        title: Text('Hi!'),
        centerTitle: true,
      ),
      body: ListView.builder(
        itemCount: data.length,
        itemBuilder: (context, index) => ListTile(
          title: Text(data.keys.toList()[index]),
          subtitle: getSubTitles1(data[data.keys.toList()[index]]),
        ),
      ),
    );
  }

  Widget getSubTitles1(List<Map<String, Object>> data) {
    return Column(
      children: List.generate(data.length, (idx) => ListTile(
        title: Text(data[idx].keys.toList()[0]),
        subtitle: getSubTitles2(data[idx][data[idx].keys.toList()[0]]),
      )),
    );
  }

  Widget getSubTitles2(List<String> data) {
    return Column(
      children: List.generate(data.length, (idx) => ListTile(title: Text(data[idx]))),
    );
  }

  List<Map<String,String>> getDataFormatted() {
    final regExp = RegExp(r'(\w+):\s([\d\w\-\s]+)');
    var matches = regExp.allMatches(raw_data);
    var data = List<Map<String,String>>();
    var counter = 1;
    var course = Map<String,String>();

    matches.forEach((m) {
      course[m.group(1)] = m.group(2);
      if (counter % 7 == 0) {
        data.add(Map<String,String>.from(course));
        course.clear();
      }
      counter++;
    });
    return data;
  }

  Map<String, Object> getNewData() {
    final data = getDataFormatted();
    var subjects = data.map((e) => e['subjectName']).toSet().toList();
    var newData = Map<String, Object>();
    
    subjects.forEach((s) {
      newData[s] = data.where((m) => m['subjectName'] == s).map((e) => e['courseName']).toSet().map<Map<String, Object>>((e) => {e: data.where((m) => m['subjectName'] == s && m['courseName'] == e).map((e) => e['courseSection']).toList()}).toList();
    });
    return newData;
  }
}

结果:

希望对你有所帮助