You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.7 KiB
Dart
79 lines
1.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import 'package:fluffychat/pangea/course_plans/course_plan_model.dart';
|
|
import 'package:fluffychat/pangea/course_plans/course_plans_repo.dart';
|
|
|
|
class CoursePlanBuilder extends StatefulWidget {
|
|
final String? courseId;
|
|
final VoidCallback? onNotFound;
|
|
final Function(CoursePlanModel course)? onLoaded;
|
|
final Widget Function(
|
|
BuildContext context,
|
|
CoursePlanController controller,
|
|
) builder;
|
|
|
|
const CoursePlanBuilder({
|
|
super.key,
|
|
required this.courseId,
|
|
required this.builder,
|
|
this.onNotFound,
|
|
this.onLoaded,
|
|
});
|
|
|
|
@override
|
|
State<CoursePlanBuilder> createState() => CoursePlanController();
|
|
}
|
|
|
|
class CoursePlanController extends State<CoursePlanBuilder> {
|
|
bool loading = true;
|
|
Object? error;
|
|
|
|
CoursePlanModel? course;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadCourse();
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(covariant CoursePlanBuilder oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (oldWidget.courseId != widget.courseId) {
|
|
_loadCourse();
|
|
}
|
|
}
|
|
|
|
Future<void> _loadCourse() async {
|
|
if (widget.courseId == null) {
|
|
setState(() {
|
|
loading = false;
|
|
error = null;
|
|
course = null;
|
|
});
|
|
return;
|
|
}
|
|
|
|
try {
|
|
setState(() {
|
|
loading = true;
|
|
error = null;
|
|
});
|
|
|
|
course = await CoursePlansRepo.get(widget.courseId!);
|
|
|
|
widget.onLoaded?.call(course!);
|
|
} catch (e) {
|
|
widget.onNotFound?.call();
|
|
error = e;
|
|
} finally {
|
|
setState(() {
|
|
loading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => widget.builder(context, this);
|
|
}
|