feat: session start screen, question context input, and G4F-style AI multi-provider fallback

This commit is contained in:
Alma Alexyz
2026-08-31 13:43:45 +05:00
parent c99e36e9ac
commit a24fdaee97
11 changed files with 857 additions and 99 deletions
+180 -41
View File
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../models/history_entry_model.dart';
import '../models/word_model.dart';
import '../services/words_repository.dart';
import '../widgets/gradient_star_badge.dart';
@@ -15,16 +16,18 @@ class HomeSearchScreen extends StatefulWidget {
class _HomeSearchScreenState extends State<HomeSearchScreen> {
final _repo = WordsRepository();
final _searchController = TextEditingController();
final _questionController = TextEditingController();
final _focusNode = FocusNode();
WordItem? _currentWord;
String? _activeHistoryId;
List<WordItem> _searchResults = [];
bool _showKeypad = true;
@override
void initState() {
super.initState();
_currentWord = _repo.getWordBySessionNumber(0) ?? _repo.allWords.first;
// Do NOT show word by default until user interacts or starts a session
_searchController.addListener(_onSearchChanged);
}
@@ -32,6 +35,7 @@ class _HomeSearchScreenState extends State<HomeSearchScreen> {
void dispose() {
_searchController.removeListener(_onSearchChanged);
_searchController.dispose();
_questionController.dispose();
_focusNode.dispose();
super.dispose();
}
@@ -49,14 +53,33 @@ class _HomeSearchScreenState extends State<HomeSearchScreen> {
setState(() {
_searchResults = results;
if (results.isNotEmpty) {
_currentWord = results.first;
_setWord(results.first, recordHistory: true);
}
});
}
void _setWord(WordItem word, {bool updateText = false}) {
Future<void> _setWord(WordItem word, {bool updateText = false, bool recordHistory = true}) async {
final question = _questionController.text.trim();
String? newHistId;
if (recordHistory) {
newHistId = DateTime.now().millisecondsSinceEpoch.toString();
final entry = HistoryEntry(
id: newHistId,
timestamp: DateTime.now(),
word: word.word,
sessionNumber: word.sessionNumber,
partOfSpeech: word.partOfSpeech,
symbolicMeaning: word.description,
readingType: 'Жребий номера',
question: question.isNotEmpty ? question : null,
);
await _repo.addHistoryEntry(entry);
}
setState(() {
_currentWord = word;
_activeHistoryId = newHistId ?? _activeHistoryId;
if (updateText) {
_searchController.text = word.sessionNumber.toString();
}
@@ -101,28 +124,35 @@ class _HomeSearchScreenState extends State<HomeSearchScreen> {
);
}
void _startNewSessionDraw() {
final rnd = _repo.getRandomWord();
_setWord(rnd, updateText: true, recordHistory: true);
}
void _resetToWelcome() {
setState(() {
_currentWord = null;
_searchController.clear();
_searchResults = [];
});
}
void _goToNext() {
if (_currentWord == null) return;
final next = _repo.getNextWord(_currentWord!.sessionNumber);
_setWord(next, updateText: true);
_setWord(next, updateText: true, recordHistory: false);
}
void _goToPrevious() {
if (_currentWord == null) return;
final prev = _repo.getPreviousWord(_currentWord!.sessionNumber);
_setWord(prev, updateText: true);
}
void _goToRandom() {
final rnd = _repo.getRandomWord();
_setWord(rnd, updateText: true);
_setWord(prev, updateText: true, recordHistory: false);
}
void _reshuffleSession() {
_repo.reshuffleSessionNumbers();
final first = _repo.getWordBySessionNumber(0) ?? _repo.allWords.first;
setState(() {
_currentWord = first;
_currentWord = null;
_searchController.clear();
_searchResults = [];
});
@@ -155,11 +185,11 @@ class _HomeSearchScreenState extends State<HomeSearchScreen> {
return Scaffold(
appBar: AppBar(
title: Row(
title: const Row(
children: [
const GradientStar(size: 24),
const SizedBox(width: 10),
const Text(
GradientStar(size: 24),
SizedBox(width: 10),
Text(
'Оракул 999',
style: TextStyle(
fontSize: 20,
@@ -170,23 +200,24 @@ class _HomeSearchScreenState extends State<HomeSearchScreen> {
],
),
actions: [
if (_currentWord != null)
IconButton(
tooltip: 'Новый вопрос / Сброс',
icon: const Icon(Icons.refresh_rounded),
onPressed: _resetToWelcome,
),
IconButton(
tooltip: 'Перетасовать числа сессии (#000..#999)',
icon: const Icon(Icons.refresh_rounded),
tooltip: 'Перетасовать числа (#000..#999)',
icon: const Icon(Icons.shuffle_rounded),
onPressed: _reshuffleSession,
),
IconButton(
tooltip: _showKeypad ? 'Скрыть цифровую панель' : 'Показать цифровую панель',
tooltip: _showKeypad ? 'Скрыть панель' : 'Показать панель',
icon: Icon(_showKeypad ? Icons.dialpad_outlined : Icons.dialpad),
onPressed: () {
setState(() => _showKeypad = !_showKeypad);
},
),
IconButton(
tooltip: 'Случайный жребий',
icon: const Icon(Icons.shuffle_rounded),
onPressed: _goToRandom,
),
],
),
body: SafeArea(
@@ -195,6 +226,45 @@ class _HomeSearchScreenState extends State<HomeSearchScreen> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Question Input Field
TextField(
controller: _questionController,
decoration: InputDecoration(
labelText: 'Ваш вопрос или намерение (необязательно)',
hintText: 'Например: В чем скрытый потенциал ситуации?',
prefixIcon: const Icon(Icons.help_outline_rounded, color: Color(0xFFFFD54F), size: 20),
suffixIcon: _questionController.text.isNotEmpty
? IconButton(
icon: const Icon(Icons.clear, size: 18),
onPressed: () => _questionController.clear(),
)
: null,
filled: true,
fillColor: isDark ? const Color(0xFF17192C) : theme.colorScheme.surfaceContainerHigh,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: BorderSide(
color: const Color(0xFFFFD54F).withValues(alpha: 0.25),
),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: BorderSide(
color: const Color(0xFFFFD54F).withValues(alpha: 0.2),
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: const BorderSide(
color: Color(0xFFFFD54F),
width: 1.5,
),
),
),
),
const SizedBox(height: 10),
// Search & Number Input Bar
TextField(
controller: _searchController,
@@ -242,7 +312,7 @@ class _HomeSearchScreenState extends State<HomeSearchScreen> {
),
),
// Search matches dropdown list if query produced text matches
// Search matches dropdown list
if (_searchResults.length > 1 && _searchController.text.isNotEmpty) ...[
const SizedBox(height: 8),
Container(
@@ -287,7 +357,7 @@ class _HomeSearchScreenState extends State<HomeSearchScreen> {
),
),
onTap: () {
_setWord(item, updateText: false);
_setWord(item, updateText: false, recordHistory: true);
},
);
},
@@ -297,8 +367,85 @@ class _HomeSearchScreenState extends State<HomeSearchScreen> {
const SizedBox(height: 14),
// Main Sacred Word Card Display
if (_currentWord != null)
// Welcome Idle State OR Active Word Card
if (_currentWord == null) ...[
// Welcome Initiation Card
Container(
padding: const EdgeInsets.all(22),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
const Color(0xFF1F223B),
const Color(0xFF131526),
],
),
borderRadius: BorderRadius.circular(24),
border: Border.all(
color: const Color(0xFFFFD54F).withValues(alpha: 0.35),
width: 1.2,
),
boxShadow: [
BoxShadow(
color: const Color(0xFFFFD54F).withValues(alpha: 0.1),
blurRadius: 16,
),
],
),
child: Column(
children: [
const GradientStar(size: 44),
const SizedBox(height: 14),
const Text(
'СВЯЩЕННЫЙ ЖРЕБИЙ ОРАКУЛА',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w900,
letterSpacing: 1.0,
color: Color(0xFFFFF9C4),
),
),
const SizedBox(height: 10),
Text(
'Сформулируйте в мыслях или запишите сверху ваш вопрос, затем нажмите кнопку или наберите номер от 000 до 999 для раскрытия символического ключа.',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 13,
height: 1.45,
color: theme.colorScheme.onSurface.withValues(alpha: 0.85),
),
),
const SizedBox(height: 18),
FilledButton.icon(
onPressed: _startNewSessionDraw,
icon: const GradientStar(size: 20),
label: const Text(
'НАЧАТЬ СЕССИЮ / БРОСИТЬ ЖРЕБИЙ',
style: TextStyle(
fontWeight: FontWeight.w900,
letterSpacing: 0.5,
),
),
style: FilledButton.styleFrom(
backgroundColor: const Color(0xFF2A2D4A),
foregroundColor: const Color(0xFFFFF9C4),
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18),
side: const BorderSide(
color: Color(0xFFFFD54F),
width: 1.2,
),
),
),
),
],
),
),
] else ...[
// Main Sacred Word Card Display
AnimatedSwitcher(
duration: const Duration(milliseconds: 300),
transitionBuilder: (child, animation) {
@@ -316,26 +463,18 @@ class _HomeSearchScreenState extends State<HomeSearchScreen> {
child: WordCardWidget(
key: ValueKey('${_currentWord!.id}_${_currentWord!.sessionNumber}'),
word: _currentWord!,
question: _questionController.text.trim(),
historyEntryId: _activeHistoryId,
onNext: _goToNext,
onPrevious: _goToPrevious,
onRandom: _goToRandom,
),
)
else
Center(
child: Padding(
padding: const EdgeInsets.all(32.0),
child: Text(
'Символ с таким номером не найден.',
textAlign: TextAlign.center,
style: TextStyle(color: theme.colorScheme.error),
),
onRandom: _startNewSessionDraw,
),
),
],
const SizedBox(height: 14),
// Interactive Numeric Keypad (1..9, C, 0, ⌫)
// Interactive Numeric Keypad
if (_showKeypad) ...[
Container(
padding: const EdgeInsets.all(12),