134 lines
4.5 KiB
Dart
134 lines
4.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import '../../../core/di/injection.dart';
|
|
import '../../../data/models/faq_models.dart';
|
|
import '../../../domain/repositories/faq_repository.dart';
|
|
import '../../../l10n/app_strings.dart';
|
|
import '../../../shared/widgets/pred_card.dart';
|
|
import '../../../shared/widgets/pred_text_field.dart';
|
|
import '../../welcome/utils/welcome_navigation.dart';
|
|
|
|
class FaqScreen extends StatefulWidget {
|
|
const FaqScreen({super.key, this.initialCategory});
|
|
|
|
/// Optional FAQ category slug (e.g. `game`) from `/faq?category=...`.
|
|
final String? initialCategory;
|
|
|
|
@override
|
|
State<FaqScreen> createState() => _FaqScreenState();
|
|
}
|
|
|
|
class _FaqScreenState extends State<FaqScreen> {
|
|
final _repo = sl<FaqRepository>();
|
|
final _searchController = TextEditingController();
|
|
List<FaqEntryModel> _entries = [];
|
|
bool _loading = true;
|
|
String? _error;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_load(category: widget.initialCategory);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_searchController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _load({String? search, String? category}) async {
|
|
setState(() {
|
|
_loading = true;
|
|
_error = null;
|
|
});
|
|
try {
|
|
final entries = await _repo.listEntries(
|
|
search: search,
|
|
category: category ?? widget.initialCategory,
|
|
);
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_entries = entries;
|
|
_loading = false;
|
|
});
|
|
} catch (e) {
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_error = e.toString().replaceFirst('Exception: ', '');
|
|
_loading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final s = context.strings;
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
leading: welcomeBackButton(context),
|
|
title: Text(s.faqTitle),
|
|
),
|
|
body: Column(
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(20, 16, 20, 0),
|
|
child: TextField(
|
|
controller: _searchController,
|
|
decoration: InputDecoration(
|
|
hintText: s.searchFaq,
|
|
prefixIcon: const Icon(Icons.search_rounded),
|
|
suffixIcon: IconButton(
|
|
icon: const Icon(Icons.clear_rounded),
|
|
onPressed: () {
|
|
_searchController.clear();
|
|
_load(category: widget.initialCategory);
|
|
},
|
|
),
|
|
),
|
|
onSubmitted: (q) => _load(search: q, category: widget.initialCategory),
|
|
),
|
|
),
|
|
Expanded(
|
|
child: _loading
|
|
? PredLoadingView(message: s.loading)
|
|
: _error != null
|
|
? PredErrorView(message: _error!, onRetry: () => _load())
|
|
: _entries.isEmpty
|
|
? Center(child: Text(s.noFaqResults))
|
|
: ListView.builder(
|
|
padding: const EdgeInsets.all(20),
|
|
itemCount: _entries.length,
|
|
itemBuilder: (context, index) {
|
|
final entry = _entries[index];
|
|
return Padding(
|
|
padding: const EdgeInsets.only(bottom: 10),
|
|
child: PredCard(
|
|
child: ExpansionTile(
|
|
title: Text(
|
|
entry.question,
|
|
style: const TextStyle(fontWeight: FontWeight.w600),
|
|
),
|
|
subtitle: entry.category.isNotEmpty
|
|
? Text(entry.category)
|
|
: null,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(16, 0, 16, 16),
|
|
child: Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: Text(entry.answer),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|