import 'package:english_learning/features/auth/provider/user_provider.dart'; import 'package:english_learning/features/learning/provider/section_provider.dart'; import 'package:english_learning/features/learning/widgets/section_card.dart'; import 'package:english_learning/features/learning/modules/topics/screens/topics_list_screen.dart'; import 'package:english_learning/features/learning/widgets/section_card_loading.dart'; import 'package:flutter/material.dart'; import 'package:english_learning/core/utils/styles/theme.dart'; import 'package:provider/provider.dart'; class LearningScreen extends StatefulWidget { const LearningScreen({ super.key, }); @override State createState() => _LearningScreenState(); } class _LearningScreenState extends State { @override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) { _fetchSections(); }); } Future _fetchSections() async { final userProvider = Provider.of(context, listen: false); final token = await userProvider.getValidToken(); if (token != null) { await Provider.of(context, listen: false) .fetchSections(token); } else { print('No valid token found. User might need to log in.'); } } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: AppColors.bgSoftColor, body: SafeArea( child: Padding( padding: const EdgeInsets.only(top: 28.0, left: 16.0, right: 16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( 'Choose what you want to learn!', style: AppTextStyles.blueTextStyle.copyWith( fontSize: 18, fontWeight: FontWeight.w900, ), ), const SizedBox(height: 8), Text( 'Develop your English skills by studying the topics in each section below.', style: AppTextStyles.greyTextStyle.copyWith( fontSize: 12, fontWeight: FontWeight.w600, ), ), const SizedBox(height: 15), Expanded( child: Consumer( builder: (context, sectionProvider, _) { if (sectionProvider.isLoading) { return ListView.builder( padding: const EdgeInsets.only(top: 8), itemCount: 6, itemBuilder: (context, index) => const SectionCardLoading(), ); } else if (sectionProvider.error != null) { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( 'Error: ${sectionProvider.error}', style: AppTextStyles.greyTextStyle, ), SizedBox(height: 16), // Tambahkan tombol retry jika diperlukan ElevatedButton( onPressed: _fetchSections, child: Text('Retry'), ) ], )); } else if (sectionProvider.sections.isEmpty) { return Center( child: Text( 'No sections available', style: AppTextStyles.greyTextStyle, ), ); } else { return ListView.builder( itemCount: sectionProvider.sections.length, itemBuilder: (context, index) { final section = sectionProvider.sections[index]; return SectionCard( section: section, onTap: () => Navigator.push( context, MaterialPageRoute( builder: (context) => TopicsListScreen( sectionId: section.id, ), ), ), ); }, ); } }, ), ), ], ), ), ), ); } }