目前我正在显示一个完整的列表,我的列表包含问题,我正在显示顶部有章节列表的提升按钮,当用户onPressed我想显示一个过滤的章节列表。我试了很多方法,但都不起作用。就像我想展示一个有特定类别的产品。
我的代码import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:quizzy/data_controller.dart';
import 'package:quizzy/models/chapterModel.dart';
import '../models/showQuestion.dart';
class PhyQB extends StatefulWidget {
const PhyQB({Key? key}) : super(key: key);
@override
State<PhyQB> createState() => _PhyQBState();
}
class _PhyQBState extends State<PhyQB> {
final DataController controller = Get.put(DataController());
@override
Widget build(BuildContext context) {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
controller.getPhysicsQuestionList();
});
return Scaffold(
appBar: AppBar(
title: Text('Physics Question Bank'),
),
body: GetBuilder<DataController>(
builder: (controller) => controller.PhysicsQuestionList.isEmpty
? const Center(
child: Text('😔 NO DATA FOUND (: 😔'),
)
: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: controller.PhysicsQuestionList.length,
shrinkWrap: true,
scrollDirection: Axis.horizontal,
reverse: true,
itemBuilder: (context, index) {
return ChapterModel(
controller.PhysicsQuestionList[index]);
}),
),
Expanded(
flex: 12,
child: ListView.builder(
itemCount: controller.PhysicsQuestionList.length,
shrinkWrap: true,
itemBuilder: (context, index) {
return ListTile(
title: showQuestion(
controller.PhysicsQuestionList[index]),
);
}),
),
],
),
),
);
}
}
model.dart章
import 'package:flutter/material.dart';
class ChapterModel extends StatelessWidget {
ChapterModel(this.ControllerFromCall);
var ControllerFromCall;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
child: ElevatedButton(
child: Text(ControllerFromCall.ChapterName),
onPressed: () {},
),
),
);
}
}
my question model
class Question {
final String QuestionId;
// To display Question
final String QuestionText;
final String? QuestionImageURL;
// to display Multiple choices (options)
final String AnswerA;
final String AnswerB;
final String AnswerC;
final String AnswerD;
// to Display this question was asked in which exam
final String AskedIn;
// to display subject and chapter
final String SubjectName;
final String ChapterName;
// To show a detailed explainantion of Correct Answer
final String CorrectAnswer;
Question({
required this.QuestionId,
required this.QuestionText,
required this.QuestionImageURL,
required this.AnswerA,
required this.AnswerB,
required this.AnswerC,
required this.AnswerD,
required this.AskedIn,
required this.SubjectName,
required this.ChapterName,
required this.CorrectAnswer,
});
}
数据控制器。飞镖文件
import 'package:firebase_storage/firebase_storage.dart';
import 'package:get/get.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:quizzy/models/Question_model.dart';
import 'dart:io';
import 'models/comman_dailog.dart';
class DataController extends GetxController {
final firebaseInstance = FirebaseFirestore.instance;
void onReady() {
super.onReady();
}
List<Question> PhysicsQuestionList = [];
Future<void> getPhysicsQuestionList() async {
PhysicsQuestionList = [];
try {
CommanDialog.showLoading();
final List<Question> physicsloadedQuestions = [];
var response = await firebaseInstance
.collection('QuestionList')
.where('SubjectName', isEqualTo: 'Physics')
.get();
if (response.docs.isNotEmpty) {
response.docs.forEach(
(result) {
physicsloadedQuestions.add(Question(
QuestionId: result.id,
QuestionText: result['QuestionText'],
QuestionImageURL: result['QuestionImageURL'],
AnswerA: result['AnswerA'],
AnswerB: result['AnswerB'],
AnswerC: result['AnswerC'],
AnswerD: result['AnswerD'],
AskedIn: result['AskedIn'],
SubjectName: result['SubjectName'],
ChapterName: result['ChapterName'],
CorrectAnswer: result['CorrectAnswer'],
));
},
);
}
PhysicsQuestionList.addAll(physicsloadedQuestions);
update();
CommanDialog.hideLoading();
} on FirebaseException catch (e) {
CommanDialog.hideLoading();
} catch (error) {
CommanDialog.hideLoading();
}
}
}
您的List<Question> PhysicsQuestionList = [];
应替换为RxList<Question> PhysicsQuestionList = List<Question>.empty(growable: true).obs;
问题是你需要让你的变量可观察,请参考Getx状态管理文档。