嗨,我一直在努力解决这个问题,但仍然不确定如何根据选择/通过的类别显示项目...
类别注释屏幕:
我尝试打印,但没有显示任何不确定如何使用以及使用什么的内容。目前,即使我有用于过滤它们的类别 ID,也会显示所有笔记。
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/notes.dart';
import '../providers/categories.dart';
import '../widgets/category_note_item.dart';
class CategoryNotesScreen extends StatelessWidget {
static const routeName = '/category-notes';
@override
Widget build(BuildContext context) {
Category routeArgs = ModalRoute.of(context).settings.arguments;
final catId = routeArgs.id;
final catTitle = routeArgs.title;
var notesData = Provider.of<Notes>(context);
print(notesData.items.where((item) => item.category.contains(catId)));
return Scaffold(
appBar: AppBar(
title: Text(catTitle),
),
body: ListView.builder(
itemCount: notesData.items.length,
itemBuilder: (ctx, i) => CategoryNoteItem(notesData.items[i]),
),
);
}
}
类别注释项小部件:
import 'package:flutter/material.dart';
import '../providers/notes.dart' as nte;
class CategoryNoteItem extends StatelessWidget {
final nte.NoteItem note;
CategoryNoteItem(this.note);
@override
Widget build(BuildContext context) {
return Card(
margin: EdgeInsets.all(10),
child: Column(
children: <Widget>[
ListTile(
title: Text('${note.question}'),
subtitle: Text('${note.answer}'),
// trailing: ,
)
],
),
);
}
}
类别提供者:
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
class Category with ChangeNotifier {
final String id;
final String title;
Category({
@required this.id,
@required this.title,
});
}
class Categories with ChangeNotifier {
List<Category> _items = [
Category(
id: 'c1',
title: 'Default',
),
Category(
id: 'c2',
title: 'French Lesson',
),
Category(
id: 'c3',
title: 'Spanish Lesson',
),
];
List<Category> get items {
return [..._items];
}}
笔记提供者:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
class NoteItem with ChangeNotifier {
final String id;
final String category;
final String question;
final String answer;
NoteItem({
@required this.id,
@required this.category,
@required this.question,
@required this.answer,
});
}
class Notes with ChangeNotifier {
List<NoteItem> _items = [
NoteItem(
id: 'n1',
category: 'c1',
question: 'q1',
answer: 'a1',
),
NoteItem(
id: 'n2',
category: 'c1',
question: 'q2',
answer: 'a2',
),
NoteItem(
id: 'n3',
category: 'c2',
question: 'q3',
answer: 'a3',
),
NoteItem(
id: 'n4',
category: 'c1',
question: 'q4',
answer: 'a4',
),
];
List<NoteItem> get items {
return [..._items];
}
}
抱歉,我对 flutter 还很陌生,还在学习中......
答案 0 :(得分:0)
经过反复试验,我发现我可以使用它来过滤来自提供者的列表:
notesData.items.where((item) => item.category.contains(catId)).toList();
并在 itembuilder 中添加:
itemBuilder: (ctx, i) => CategoryNoteItem(
notes[i].id,
notes[i].category,
notes[i].question,
notes[i].answer,
),
我只能显示所选类别中的项目。