324 lines
12 KiB
Dart
324 lines
12 KiB
Dart
/// Help presentation: searchable FAQ list with expandable answers.
|
|
library;
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import '../../../core/di/injection.dart';
|
|
import '../../../core/errors/app_exception.dart';
|
|
import '../../../core/theme/app_colors.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_skeleton.dart';
|
|
import '../../../shared/widgets/section_error.dart';
|
|
import '../../../shared/widgets/tloto_gradient_app_bar.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;
|
|
bool _searching = false;
|
|
bool _retrying = false;
|
|
String? _error;
|
|
String? _activeSearch;
|
|
int _loadGen = 0;
|
|
Future<void>? _sameKeyInFlight;
|
|
String? _sameKey;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_searchController.addListener(() {
|
|
if (mounted) setState(() {});
|
|
});
|
|
final cached = _repo.peekLast(category: widget.initialCategory);
|
|
if (cached != null && cached.isNotEmpty) {
|
|
_entries = cached;
|
|
_loading = false;
|
|
}
|
|
_load(category: widget.initialCategory);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_searchController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _load({
|
|
String? search,
|
|
String? category,
|
|
bool force = false,
|
|
}) {
|
|
final cat = category ?? widget.initialCategory;
|
|
final key = '${cat ?? ''}|${search ?? ''}';
|
|
// Coalesce only identical in-flight queries (retry spam); new searches proceed.
|
|
if (_sameKeyInFlight != null && _sameKey == key) return _sameKeyInFlight!;
|
|
|
|
final gen = ++_loadGen;
|
|
final keepVisible = _entries.isNotEmpty;
|
|
final future = () async {
|
|
setState(() {
|
|
_activeSearch = search;
|
|
_retrying = force;
|
|
if (keepVisible) {
|
|
_searching = true;
|
|
if (!force) _error = null;
|
|
} else {
|
|
_loading = true;
|
|
if (!force) _error = null;
|
|
}
|
|
});
|
|
try {
|
|
final entries = await _repo.listEntries(
|
|
search: search,
|
|
category: cat,
|
|
force: force,
|
|
);
|
|
if (!mounted || gen != _loadGen) return;
|
|
setState(() {
|
|
_entries = entries;
|
|
_loading = false;
|
|
_searching = false;
|
|
_retrying = false;
|
|
_error = null;
|
|
});
|
|
} catch (_) {
|
|
if (!mounted || gen != _loadGen) return;
|
|
setState(() {
|
|
_loading = false;
|
|
_searching = false;
|
|
_retrying = false;
|
|
if (_entries.isEmpty) {
|
|
_error = AppException.connectionFailedMessage;
|
|
}
|
|
});
|
|
}
|
|
}();
|
|
_sameKey = key;
|
|
_sameKeyInFlight = future.whenComplete(() {
|
|
if (_sameKey == key) {
|
|
_sameKeyInFlight = null;
|
|
_sameKey = null;
|
|
}
|
|
});
|
|
return _sameKeyInFlight!;
|
|
}
|
|
|
|
void _clearSearch() {
|
|
_searchController.clear();
|
|
_load(category: widget.initialCategory, force: true);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final s = context.strings;
|
|
final isDark = Theme.of(context).brightness == Brightness.dark;
|
|
final bottomInset = MediaQuery.paddingOf(context).bottom;
|
|
final keyboardInset = MediaQuery.viewInsetsOf(context).bottom;
|
|
final muted = isDark ? AppColors.textMuted : const Color(0xFF94A3B8);
|
|
final secondary =
|
|
isDark ? AppColors.textSecondary : const Color(0xFF475569);
|
|
const physics = AlwaysScrollableScrollPhysics(
|
|
parent: BouncingScrollPhysics(),
|
|
);
|
|
final listPadding = EdgeInsets.fromLTRB(
|
|
20,
|
|
12,
|
|
20,
|
|
24 + bottomInset + keyboardInset,
|
|
);
|
|
|
|
return AnnotatedRegion<SystemUiOverlayStyle>(
|
|
value: TlotoGradientAppBar.overlayStyle,
|
|
child: Scaffold(
|
|
key: const Key('faq_screen'),
|
|
body: Column(
|
|
children: [
|
|
TlotoGradientAppBar(
|
|
title: s.faqTitle,
|
|
showBack: true,
|
|
onBack: () => popToWelcomeOrPrevious(context),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(20, 12, 20, 0),
|
|
child: PredCard(
|
|
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 2),
|
|
child: TextField(
|
|
key: const Key('faq_search'),
|
|
controller: _searchController,
|
|
textInputAction: TextInputAction.search,
|
|
decoration: InputDecoration(
|
|
hintText: s.searchFaq,
|
|
border: InputBorder.none,
|
|
enabledBorder: InputBorder.none,
|
|
focusedBorder: InputBorder.none,
|
|
prefixIcon: const Icon(Icons.search_rounded),
|
|
suffixIcon: _searchController.text.isNotEmpty
|
|
? IconButton(
|
|
icon: const Icon(Icons.clear_rounded),
|
|
onPressed: _clearSearch,
|
|
)
|
|
: null,
|
|
),
|
|
onSubmitted: (q) => _load(
|
|
search: q.trim().isEmpty ? null : q.trim(),
|
|
category: widget.initialCategory,
|
|
force: true,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
if (_searching)
|
|
const LinearProgressIndicator(
|
|
minHeight: 2,
|
|
color: AppColors.primary,
|
|
),
|
|
Expanded(
|
|
child: RefreshIndicator(
|
|
onRefresh: () => _load(
|
|
search: _activeSearch,
|
|
category: widget.initialCategory,
|
|
force: true,
|
|
),
|
|
child: _error != null && _entries.isEmpty
|
|
? ListView(
|
|
physics: physics,
|
|
padding: listPadding,
|
|
children: [
|
|
SectionError(
|
|
message: _error!,
|
|
retrying: _retrying,
|
|
onRetry: () => _load(
|
|
search: _activeSearch,
|
|
category: widget.initialCategory,
|
|
force: true,
|
|
),
|
|
),
|
|
],
|
|
)
|
|
: _loading && _entries.isEmpty
|
|
? ListView(
|
|
physics: physics,
|
|
padding: listPadding,
|
|
children: const [
|
|
PredSkeletonCard(height: 72),
|
|
SizedBox(height: 10),
|
|
PredSkeletonCard(height: 72),
|
|
SizedBox(height: 10),
|
|
PredSkeletonCard(height: 72),
|
|
],
|
|
)
|
|
: _entries.isEmpty
|
|
? ListView(
|
|
physics: physics,
|
|
padding: EdgeInsets.fromLTRB(
|
|
40,
|
|
40,
|
|
40,
|
|
40 + bottomInset,
|
|
),
|
|
children: [
|
|
Icon(
|
|
Icons.help_outline_rounded,
|
|
size: 48,
|
|
color: muted,
|
|
),
|
|
const SizedBox(height: 12),
|
|
Text(
|
|
s.noFaqResults,
|
|
textAlign: TextAlign.center,
|
|
style: Theme.of(context)
|
|
.textTheme
|
|
.bodyMedium
|
|
?.copyWith(color: secondary),
|
|
),
|
|
],
|
|
)
|
|
: ListView.separated(
|
|
physics: physics,
|
|
padding: listPadding,
|
|
itemCount: _entries.length,
|
|
separatorBuilder: (_, __) =>
|
|
const SizedBox(height: 8),
|
|
itemBuilder: (context, index) {
|
|
final entry = _entries[index];
|
|
return PredCard(
|
|
padding: EdgeInsets.zero,
|
|
child: Theme(
|
|
data: Theme.of(context).copyWith(
|
|
dividerColor: Colors.transparent,
|
|
),
|
|
child: ExpansionTile(
|
|
tilePadding: const EdgeInsets.symmetric(
|
|
horizontal: 14,
|
|
vertical: 2,
|
|
),
|
|
childrenPadding:
|
|
const EdgeInsets.fromLTRB(
|
|
14,
|
|
0,
|
|
14,
|
|
14,
|
|
),
|
|
title: Text(
|
|
entry.question,
|
|
style: Theme.of(context)
|
|
.textTheme
|
|
.titleSmall
|
|
?.copyWith(
|
|
fontWeight: FontWeight.w700,
|
|
),
|
|
),
|
|
subtitle: entry.category.isNotEmpty
|
|
? Text(
|
|
entry.category,
|
|
style: Theme.of(context)
|
|
.textTheme
|
|
.bodySmall
|
|
?.copyWith(
|
|
color: muted,
|
|
),
|
|
)
|
|
: null,
|
|
children: [
|
|
Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: Text(
|
|
entry.answer,
|
|
style: Theme.of(context)
|
|
.textTheme
|
|
.bodyMedium
|
|
?.copyWith(
|
|
color: secondary,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|