Atualição da appbar | coreção de inserção de dados | outras optimizaçoes
This commit is contained in:
@@ -12,6 +12,9 @@ import 'widgets/tap_bounce.dart';
|
|||||||
const Color _teal = Color(0xFF2F9E94);
|
const Color _teal = Color(0xFF2F9E94);
|
||||||
const Color _pink = Color(0xFFFF55A7);
|
const Color _pink = Color(0xFFFF55A7);
|
||||||
|
|
||||||
|
/// Nomes só podem ter letras (incluindo acentuadas) e espaços — sem números.
|
||||||
|
final RegExp _namePattern = RegExp(r"^[a-zA-ZÀ-ÖØ-öø-ÿ' -]+$");
|
||||||
|
|
||||||
class HomeScreen extends StatefulWidget {
|
class HomeScreen extends StatefulWidget {
|
||||||
const HomeScreen({super.key});
|
const HomeScreen({super.key});
|
||||||
|
|
||||||
@@ -369,6 +372,9 @@ class _AuthForm extends StatelessWidget {
|
|||||||
final value = (v ?? '').trim();
|
final value = (v ?? '').trim();
|
||||||
if (value.isEmpty) return 'Informe seu nome';
|
if (value.isEmpty) return 'Informe seu nome';
|
||||||
if (value.length < 2) return 'Nome muito curto';
|
if (value.length < 2) return 'Nome muito curto';
|
||||||
|
if (!_namePattern.hasMatch(value)) {
|
||||||
|
return 'O nome não pode conter números';
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -15,9 +15,13 @@ import 'screens/settings_screen.dart';
|
|||||||
import 'screens/video_screen.dart';
|
import 'screens/video_screen.dart';
|
||||||
import 'widgets/animated_nav_icon.dart';
|
import 'widgets/animated_nav_icon.dart';
|
||||||
import 'widgets/app_dialogs.dart';
|
import 'widgets/app_dialogs.dart';
|
||||||
|
import 'widgets/app_gradients.dart';
|
||||||
import 'widgets/entrance.dart';
|
import 'widgets/entrance.dart';
|
||||||
import 'widgets/tap_bounce.dart';
|
import 'widgets/tap_bounce.dart';
|
||||||
|
|
||||||
|
/// Nomes só podem ter letras (incluindo acentuadas) e espaços — sem números.
|
||||||
|
final RegExp _namePattern = RegExp(r"^[a-zA-ZÀ-ÖØ-öø-ÿ' -]+$");
|
||||||
|
|
||||||
class LoggedHomeScreen extends StatefulWidget {
|
class LoggedHomeScreen extends StatefulWidget {
|
||||||
const LoggedHomeScreen({super.key});
|
const LoggedHomeScreen({super.key});
|
||||||
|
|
||||||
@@ -249,7 +253,13 @@ class _LoggedHomeScreenState extends State<LoggedHomeScreen>
|
|||||||
child: AppBar(
|
child: AppBar(
|
||||||
toolbarHeight: toolbarHeight,
|
toolbarHeight: toolbarHeight,
|
||||||
clipBehavior: Clip.antiAlias,
|
clipBehavior: Clip.antiAlias,
|
||||||
flexibleSpace: _index != 0
|
flexibleSpace: ClipRRect(
|
||||||
|
borderRadius: BorderRadius.vertical(
|
||||||
|
bottom: Radius.circular(_index == 0 ? 24 : 0),
|
||||||
|
),
|
||||||
|
child: Container(
|
||||||
|
decoration: const BoxDecoration(gradient: kAppBarGradient),
|
||||||
|
child: _index != 0
|
||||||
? null
|
? null
|
||||||
: Stack(
|
: Stack(
|
||||||
fit: StackFit.expand,
|
fit: StackFit.expand,
|
||||||
@@ -304,6 +314,8 @@ class _LoggedHomeScreenState extends State<LoggedHomeScreen>
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
title: Align(
|
title: Align(
|
||||||
alignment: _index == 0 ? Alignment.topLeft : Alignment.center,
|
alignment: _index == 0 ? Alignment.topLeft : Alignment.center,
|
||||||
child: _index == 0
|
child: _index == 0
|
||||||
@@ -735,70 +747,83 @@ Future<Map<String, dynamic>?> _pickChildSheet(
|
|||||||
borderRadius: BorderRadius.vertical(top: Radius.circular(24)),
|
borderRadius: BorderRadius.vertical(top: Radius.circular(24)),
|
||||||
),
|
),
|
||||||
builder: (ctx) {
|
builder: (ctx) {
|
||||||
|
final maxHeight = MediaQuery.sizeOf(ctx).height * 0.8;
|
||||||
return SafeArea(
|
return SafeArea(
|
||||||
child: Padding(
|
child: ConstrainedBox(
|
||||||
padding: const EdgeInsets.fromLTRB(18, 6, 18, 18),
|
constraints: BoxConstraints(maxHeight: maxHeight),
|
||||||
child: Column(
|
child: Padding(
|
||||||
mainAxisSize: MainAxisSize.min,
|
padding: const EdgeInsets.fromLTRB(18, 6, 18, 18),
|
||||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
child: Column(
|
||||||
children: [
|
mainAxisSize: MainAxisSize.min,
|
||||||
const Text(
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
'Para qual criança é o quiz?',
|
children: [
|
||||||
textAlign: TextAlign.center,
|
const Text(
|
||||||
style: TextStyle(
|
'Para qual criança é o quiz?',
|
||||||
fontSize: 18,
|
textAlign: TextAlign.center,
|
||||||
fontWeight: FontWeight.w900,
|
style: TextStyle(
|
||||||
color: Color(0xFFFF55A7),
|
fontSize: 18,
|
||||||
|
fontWeight: FontWeight.w900,
|
||||||
|
color: Color(0xFFFF55A7),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
const SizedBox(height: 14),
|
||||||
const SizedBox(height: 14),
|
Flexible(
|
||||||
...children.map((c) {
|
child: SingleChildScrollView(
|
||||||
final name = (c['name'] ?? '').toString();
|
child: Column(
|
||||||
final age = c['age'];
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
final label = age != null ? '$name • $age anos' : name;
|
children: children.map((c) {
|
||||||
return Padding(
|
final name = (c['name'] ?? '').toString();
|
||||||
padding: const EdgeInsets.only(bottom: 10),
|
final age = c['age'];
|
||||||
child: TapBounce(
|
final label = age != null
|
||||||
scale: 0.97,
|
? '$name • $age anos'
|
||||||
child: Material(
|
: name;
|
||||||
color: Colors.white.withValues(alpha: 0.85),
|
return Padding(
|
||||||
borderRadius: BorderRadius.circular(16),
|
padding: const EdgeInsets.only(bottom: 10),
|
||||||
child: InkWell(
|
child: TapBounce(
|
||||||
borderRadius: BorderRadius.circular(16),
|
scale: 0.97,
|
||||||
onTap: () => Navigator.of(ctx).pop(c),
|
child: Material(
|
||||||
child: Padding(
|
color: Colors.white.withValues(alpha: 0.85),
|
||||||
padding: const EdgeInsets.symmetric(
|
borderRadius: BorderRadius.circular(16),
|
||||||
horizontal: 16,
|
child: InkWell(
|
||||||
vertical: 14,
|
borderRadius: BorderRadius.circular(16),
|
||||||
),
|
onTap: () => Navigator.of(ctx).pop(c),
|
||||||
child: Row(
|
child: Padding(
|
||||||
children: [
|
padding: const EdgeInsets.symmetric(
|
||||||
Expanded(
|
horizontal: 16,
|
||||||
child: Text(
|
vertical: 14,
|
||||||
label,
|
),
|
||||||
style: const TextStyle(
|
child: Row(
|
||||||
fontWeight: FontWeight.w800,
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: Text(
|
||||||
|
label,
|
||||||
|
style: const TextStyle(
|
||||||
|
fontWeight: FontWeight.w800,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const Icon(
|
||||||
|
Icons.chevron_right_rounded,
|
||||||
|
color: teal,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
const Icon(
|
),
|
||||||
Icons.chevron_right_rounded,
|
);
|
||||||
color: teal,
|
}).toList(),
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
),
|
||||||
}),
|
const SizedBox(height: 4),
|
||||||
const SizedBox(height: 4),
|
TextButton(
|
||||||
TextButton(
|
onPressed: () => Navigator.of(ctx).pop(null),
|
||||||
onPressed: () => Navigator.of(ctx).pop(null),
|
child: const Text('Cancelar'),
|
||||||
child: const Text('Cancelar'),
|
),
|
||||||
),
|
],
|
||||||
],
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -1824,6 +1849,9 @@ class _AddChildSheetState extends State<_AddChildSheet> {
|
|||||||
final value = (v ?? '').trim();
|
final value = (v ?? '').trim();
|
||||||
if (value.isEmpty) return 'Informe o nome';
|
if (value.isEmpty) return 'Informe o nome';
|
||||||
if (value.length < 2) return 'Nome muito curto';
|
if (value.length < 2) return 'Nome muito curto';
|
||||||
|
if (!_namePattern.hasMatch(value)) {
|
||||||
|
return 'O nome não pode conter números';
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@@ -1831,13 +1859,19 @@ class _AddChildSheetState extends State<_AddChildSheet> {
|
|||||||
controller: _ageController,
|
controller: _ageController,
|
||||||
keyboardType: TextInputType.number,
|
keyboardType: TextInputType.number,
|
||||||
textInputAction: TextInputAction.next,
|
textInputAction: TextInputAction.next,
|
||||||
|
inputFormatters: [
|
||||||
|
FilteringTextInputFormatter.digitsOnly,
|
||||||
|
LengthLimitingTextInputFormatter(2),
|
||||||
|
],
|
||||||
decoration: const InputDecoration(labelText: 'Idade'),
|
decoration: const InputDecoration(labelText: 'Idade'),
|
||||||
validator: (v) {
|
validator: (v) {
|
||||||
final raw = (v ?? '').trim();
|
final raw = (v ?? '').trim();
|
||||||
if (raw.isEmpty) return 'Informe a idade';
|
if (raw.isEmpty) return 'Informe a idade';
|
||||||
final age = int.tryParse(raw);
|
final age = int.tryParse(raw);
|
||||||
if (age == null) return 'Idade inválida';
|
if (age == null) return 'Idade inválida';
|
||||||
if (age < 0 || age > 25) return 'Idade inválida';
|
if (age < 0 || age > 17) {
|
||||||
|
return 'Idade deve ser entre 0 e 17 anos';
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import 'dart:math' as math;
|
import 'dart:math' as math;
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
import 'package:lottie/lottie.dart';
|
import 'package:lottie/lottie.dart';
|
||||||
|
|
||||||
import '../screens/video_screen.dart';
|
import '../screens/video_screen.dart';
|
||||||
@@ -65,6 +66,11 @@ class QuizQuestionScreen extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _QuizQuestionScreenState extends State<QuizQuestionScreen> {
|
class _QuizQuestionScreenState extends State<QuizQuestionScreen> {
|
||||||
|
// Uma criança não tem, na prática, mais do que ~20 dentes numa arcada
|
||||||
|
// (dentição decídua + permanente em transição já é um caso extremo).
|
||||||
|
// Serve para barrar valores absurdos como "400".
|
||||||
|
static const int _maxTeethCount = 20;
|
||||||
|
|
||||||
int? _selected;
|
int? _selected;
|
||||||
TextEditingController? _numberController;
|
TextEditingController? _numberController;
|
||||||
int? _numberValue;
|
int? _numberValue;
|
||||||
@@ -89,7 +95,11 @@ class _QuizQuestionScreenState extends State<QuizQuestionScreen> {
|
|||||||
final size = MediaQuery.sizeOf(context);
|
final size = MediaQuery.sizeOf(context);
|
||||||
bool canProceed = _selected != null && !_navigating;
|
bool canProceed = _selected != null && !_navigating;
|
||||||
if (widget.answerType == QuizAnswerType.number) {
|
if (widget.answerType == QuizAnswerType.number) {
|
||||||
canProceed = _numberValue != null && _numberValue! >= 0 && !_navigating;
|
canProceed =
|
||||||
|
_numberValue != null &&
|
||||||
|
_numberValue! >= 0 &&
|
||||||
|
_numberValue! <= _maxTeethCount &&
|
||||||
|
!_navigating;
|
||||||
}
|
}
|
||||||
|
|
||||||
final bool hasSuggestedVideo =
|
final bool hasSuggestedVideo =
|
||||||
@@ -551,6 +561,10 @@ class _QuizQuestionScreenState extends State<QuizQuestionScreen> {
|
|||||||
controller: _numberController,
|
controller: _numberController,
|
||||||
keyboardType: TextInputType.number,
|
keyboardType: TextInputType.number,
|
||||||
textAlign: TextAlign.center,
|
textAlign: TextAlign.center,
|
||||||
|
inputFormatters: [
|
||||||
|
FilteringTextInputFormatter.digitsOnly,
|
||||||
|
LengthLimitingTextInputFormatter(2),
|
||||||
|
],
|
||||||
style: const TextStyle(
|
style: const TextStyle(
|
||||||
fontSize: 24,
|
fontSize: 24,
|
||||||
fontWeight: FontWeight.w900,
|
fontWeight: FontWeight.w900,
|
||||||
@@ -573,6 +587,18 @@ class _QuizQuestionScreenState extends State<QuizQuestionScreen> {
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
if (_numberValue != null && _numberValue! > _maxTeethCount) ...[
|
||||||
|
const SizedBox(height: 10),
|
||||||
|
Text(
|
||||||
|
'Um número tão alto assim não é possível.\nO máximo é $_maxTeethCount.',
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
style: const TextStyle(
|
||||||
|
color: Color(0xFFFF55A7),
|
||||||
|
fontWeight: FontWeight.w700,
|
||||||
|
fontSize: 12,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import 'dart:math' as math;
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:lottie/lottie.dart';
|
import 'package:lottie/lottie.dart';
|
||||||
|
|
||||||
|
import '../widgets/app_gradients.dart';
|
||||||
import '../widgets/entrance.dart';
|
import '../widgets/entrance.dart';
|
||||||
import '../widgets/tap_bounce.dart';
|
import '../widgets/tap_bounce.dart';
|
||||||
|
|
||||||
@@ -20,6 +21,9 @@ class CuriosidadeScreen extends StatelessWidget {
|
|||||||
backgroundColor: _teal,
|
backgroundColor: _teal,
|
||||||
foregroundColor: Colors.white,
|
foregroundColor: Colors.white,
|
||||||
elevation: 0,
|
elevation: 0,
|
||||||
|
flexibleSpace: Container(
|
||||||
|
decoration: const BoxDecoration(gradient: kAppBarGradient),
|
||||||
|
),
|
||||||
title: const Text(
|
title: const Text(
|
||||||
'Curiosidades',
|
'Curiosidades',
|
||||||
style: TextStyle(fontWeight: FontWeight.w900),
|
style: TextStyle(fontWeight: FontWeight.w900),
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
import '../widgets/app_gradients.dart';
|
||||||
|
|
||||||
class TermsScreen extends StatelessWidget {
|
class TermsScreen extends StatelessWidget {
|
||||||
const TermsScreen({super.key});
|
const TermsScreen({super.key});
|
||||||
|
|
||||||
@@ -13,6 +15,9 @@ class TermsScreen extends StatelessWidget {
|
|||||||
backgroundColor: _teal,
|
backgroundColor: _teal,
|
||||||
foregroundColor: Colors.white,
|
foregroundColor: Colors.white,
|
||||||
elevation: 0,
|
elevation: 0,
|
||||||
|
flexibleSpace: Container(
|
||||||
|
decoration: const BoxDecoration(gradient: kAppBarGradient),
|
||||||
|
),
|
||||||
title: const Text(
|
title: const Text(
|
||||||
'Termos de Serviço',
|
'Termos de Serviço',
|
||||||
style: TextStyle(fontWeight: FontWeight.w900),
|
style: TextStyle(fontWeight: FontWeight.w900),
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import 'package:lottie/lottie.dart';
|
|||||||
import 'package:video_player/video_player.dart';
|
import 'package:video_player/video_player.dart';
|
||||||
import 'package:youtube_player_flutter/youtube_player_flutter.dart';
|
import 'package:youtube_player_flutter/youtube_player_flutter.dart';
|
||||||
|
|
||||||
|
import '../widgets/app_gradients.dart';
|
||||||
import '../widgets/entrance.dart';
|
import '../widgets/entrance.dart';
|
||||||
import '../widgets/tap_bounce.dart';
|
import '../widgets/tap_bounce.dart';
|
||||||
|
|
||||||
@@ -110,8 +111,48 @@ final List<VideoData> videoList = [
|
|||||||
),
|
),
|
||||||
];
|
];
|
||||||
|
|
||||||
// Cache for video controllers to avoid re-initializing
|
// Cache de controllers de vídeo, partilhado entre qualquer ecrã que mostre
|
||||||
|
// uma prévia (grelha de vídeos e o cartão em destaque na Home). Como vários
|
||||||
|
// widgets podem apontar para o mesmo controller ao mesmo tempo, mantemos uma
|
||||||
|
// contagem de referências para só o libertar quando ninguém mais o usa.
|
||||||
final Map<String, VideoPlayerController> _videoControllerCache = {};
|
final Map<String, VideoPlayerController> _videoControllerCache = {};
|
||||||
|
final Map<String, int> _videoControllerRefCounts = {};
|
||||||
|
|
||||||
|
// Dispositivos (sobretudo emuladores) só suportam um número pequeno de
|
||||||
|
// decodificadores de vídeo/áudio simultâneos. A grelha de vídeos pode ter
|
||||||
|
// dezenas de prévias e o GridView constrói várias de uma vez ao rolar; sem
|
||||||
|
// este limite, cada thumbnail tenta abrir o seu próprio decoder e o sistema
|
||||||
|
// trava (era exatamente isto que causava o congelamento e perda de som ao
|
||||||
|
// navegar por muitos vídeos). Acima do limite, a prévia simplesmente mostra
|
||||||
|
// o ícone de play em vez do frame real do vídeo.
|
||||||
|
const int _maxConcurrentThumbnailDecoders = 1;
|
||||||
|
|
||||||
|
bool get _hasThumbnailDecoderSlot =>
|
||||||
|
_videoControllerCache.length < _maxConcurrentThumbnailDecoders;
|
||||||
|
|
||||||
|
void _releaseVideoController(String path) {
|
||||||
|
final remaining = (_videoControllerRefCounts[path] ?? 0) - 1;
|
||||||
|
if (remaining > 0) {
|
||||||
|
_videoControllerRefCounts[path] = remaining;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_videoControllerRefCounts.remove(path);
|
||||||
|
_videoControllerCache.remove(path)?.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Descarta TODOS os controllers de prévia em cache. Chamado antes de abrir
|
||||||
|
/// um player real (dialog/fullscreen), que já precisa dos seus próprios
|
||||||
|
/// decodificadores de vídeo e áudio — em dispositivos com pouca capacidade
|
||||||
|
/// (sobretudo emuladores), manter qualquer prévia viva ao mesmo tempo é
|
||||||
|
/// suficiente para esgotar os decodificadores e travar o aparelho.
|
||||||
|
void _evictAllVideoControllers() {
|
||||||
|
_videoControllerRefCounts.clear();
|
||||||
|
final controllers = _videoControllerCache.values.toList();
|
||||||
|
_videoControllerCache.clear();
|
||||||
|
for (final controller in controllers) {
|
||||||
|
controller.dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> showVideoPlayerDialog(BuildContext context, VideoData video) {
|
Future<void> showVideoPlayerDialog(BuildContext context, VideoData video) {
|
||||||
if (video.youtubeId != null) {
|
if (video.youtubeId != null) {
|
||||||
@@ -156,11 +197,9 @@ class _VideoScreenState extends State<VideoScreen> {
|
|||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_searchController.dispose();
|
_searchController.dispose();
|
||||||
// Dispose all cached controllers
|
// Não descartamos o cache global aqui: outros widgets (como o cartão em
|
||||||
for (var controller in _videoControllerCache.values) {
|
// destaque na Home) podem estar a usar os mesmos controllers. Cada
|
||||||
controller.dispose();
|
// [VideoThumbnail] liberta a sua própria referência ao ser desmontado.
|
||||||
}
|
|
||||||
_videoControllerCache.clear();
|
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,7 +219,11 @@ class _VideoScreenState extends State<VideoScreen> {
|
|||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
backgroundColor: VideoScreen._teal,
|
backgroundColor: VideoScreen._teal,
|
||||||
foregroundColor: Colors.white,
|
foregroundColor: Colors.white,
|
||||||
|
surfaceTintColor: VideoScreen._teal,
|
||||||
elevation: 0,
|
elevation: 0,
|
||||||
|
flexibleSpace: Container(
|
||||||
|
decoration: const BoxDecoration(gradient: kAppBarGradient),
|
||||||
|
),
|
||||||
title: const Text(
|
title: const Text(
|
||||||
'Videos Educativos',
|
'Videos Educativos',
|
||||||
style: TextStyle(fontWeight: FontWeight.w900),
|
style: TextStyle(fontWeight: FontWeight.w900),
|
||||||
@@ -321,9 +364,18 @@ class VideoThumbnail extends StatefulWidget {
|
|||||||
class _VideoThumbnailState extends State<VideoThumbnail> {
|
class _VideoThumbnailState extends State<VideoThumbnail> {
|
||||||
VideoPlayerController? _controller;
|
VideoPlayerController? _controller;
|
||||||
bool _isInitialized = false;
|
bool _isInitialized = false;
|
||||||
|
bool _hasOwnRef = false;
|
||||||
|
|
||||||
bool get _isYoutube => widget.video.youtubeId != null;
|
bool get _isYoutube => widget.video.youtubeId != null;
|
||||||
|
|
||||||
|
/// Verdadeiro quando outro widget descartou o controller que este
|
||||||
|
/// thumbnail estava a usar (ex.: ao abrir o player em dialog/fullscreen).
|
||||||
|
/// Nesse caso [_controller] deixou de ser o mesmo objeto guardado no
|
||||||
|
/// cache partilhado — nunca é seguro tocar num controller já disposed.
|
||||||
|
bool get _isControllerStale =>
|
||||||
|
_controller != null &&
|
||||||
|
_videoControllerCache[widget.video.videoPath] != _controller;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
@@ -332,47 +384,62 @@ class _VideoThumbnailState extends State<VideoThumbnail> {
|
|||||||
|
|
||||||
Future<void> _initializeVideo() async {
|
Future<void> _initializeVideo() async {
|
||||||
final path = widget.video.videoPath!;
|
final path = widget.video.videoPath!;
|
||||||
// Check if controller exists in cache
|
final cached = _videoControllerCache[path];
|
||||||
if (_videoControllerCache.containsKey(path)) {
|
if (cached != null) {
|
||||||
_controller = _videoControllerCache[path];
|
_controller = cached;
|
||||||
await _controller!.seekTo(const Duration(seconds: 2));
|
_videoControllerRefCounts[path] =
|
||||||
await _controller!.pause();
|
(_videoControllerRefCounts[path] ?? 0) + 1;
|
||||||
if (mounted) {
|
_hasOwnRef = true;
|
||||||
setState(() {
|
try {
|
||||||
_isInitialized = true;
|
await cached.seekTo(const Duration(seconds: 2));
|
||||||
});
|
await cached.pause();
|
||||||
|
if (mounted) setState(() => _isInitialized = true);
|
||||||
|
} catch (_) {
|
||||||
|
// O controller partilhado foi descartado por outra tela entretanto;
|
||||||
|
// larga a referência inválida e cria um novo do zero.
|
||||||
|
_releaseOwnRef();
|
||||||
|
_controller = null;
|
||||||
|
if (mounted) await _createFreshController(path);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (!_hasThumbnailDecoderSlot) {
|
||||||
|
// Já há decoders suficientes ativos noutras prévias; fica só com o
|
||||||
|
// ícone de play em vez de arriscar sobrecarregar o dispositivo.
|
||||||
|
if (mounted) setState(() => _isInitialized = false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await _createFreshController(path);
|
||||||
|
}
|
||||||
|
|
||||||
// Create new controller and cache it
|
Future<void> _createFreshController(String path) async {
|
||||||
_controller = VideoPlayerController.asset(path);
|
final controller = VideoPlayerController.asset(path);
|
||||||
|
_controller = controller;
|
||||||
try {
|
try {
|
||||||
await _controller!.initialize();
|
await controller.initialize();
|
||||||
await _controller!.seekTo(const Duration(seconds: 2));
|
await controller.seekTo(const Duration(seconds: 2));
|
||||||
await _controller!.pause();
|
await controller.pause();
|
||||||
_videoControllerCache[path] = _controller!;
|
_videoControllerCache[path] = controller;
|
||||||
if (mounted) {
|
_videoControllerRefCounts[path] =
|
||||||
setState(() {
|
(_videoControllerRefCounts[path] ?? 0) + 1;
|
||||||
_isInitialized = true;
|
_hasOwnRef = true;
|
||||||
});
|
if (mounted) setState(() => _isInitialized = true);
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (mounted) {
|
if (mounted) setState(() => _isInitialized = false);
|
||||||
setState(() {
|
}
|
||||||
_isInitialized = false;
|
}
|
||||||
});
|
|
||||||
}
|
void _releaseOwnRef() {
|
||||||
|
final path = widget.video.videoPath;
|
||||||
|
if (_hasOwnRef && path != null) {
|
||||||
|
_releaseVideoController(path);
|
||||||
|
_hasOwnRef = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
// Don't dispose cached controllers, they will be disposed when screen is disposed
|
_releaseOwnRef();
|
||||||
if (_controller != null &&
|
|
||||||
!_videoControllerCache.containsKey(widget.video.videoPath)) {
|
|
||||||
_controller!.dispose();
|
|
||||||
}
|
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -406,6 +473,18 @@ class _VideoThumbnailState extends State<VideoThumbnail> {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_isControllerStale) {
|
||||||
|
// Não mexemos no controller antigo (pode já estar disposed); larga a
|
||||||
|
// referência e agenda uma reinicialização para o próximo frame.
|
||||||
|
_hasOwnRef = false;
|
||||||
|
_controller = null;
|
||||||
|
_isInitialized = false;
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
if (mounted) _initializeVideo();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return _isInitialized && _controller != null
|
return _isInitialized && _controller != null
|
||||||
? ClipRRect(
|
? ClipRRect(
|
||||||
borderRadius: BorderRadius.circular(widget.borderRadius),
|
borderRadius: BorderRadius.circular(widget.borderRadius),
|
||||||
@@ -434,11 +513,10 @@ class _VideoButton extends StatelessWidget {
|
|||||||
final VideoData video;
|
final VideoData video;
|
||||||
|
|
||||||
void _showVideoPlayer(BuildContext context, VideoData video) {
|
void _showVideoPlayer(BuildContext context, VideoData video) {
|
||||||
if (video.youtubeId == null &&
|
if (video.youtubeId == null) {
|
||||||
_videoControllerCache.containsKey(video.videoPath)) {
|
// Liberta todos os decodificadores de prévia antes de abrir o player
|
||||||
// Dispose the cached controller to avoid codec conflict with dialog controller
|
// em dialog, que precisa dos seus próprios decoders de vídeo/áudio.
|
||||||
_videoControllerCache[video.videoPath]!.dispose();
|
_evictAllVideoControllers();
|
||||||
_videoControllerCache.remove(video.videoPath);
|
|
||||||
}
|
}
|
||||||
showVideoPlayerDialog(context, video);
|
showVideoPlayerDialog(context, video);
|
||||||
}
|
}
|
||||||
@@ -696,6 +774,15 @@ class _VideoControlsState extends State<_VideoControls> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _seekBy(Duration offset) {
|
||||||
|
final position = widget.controller.value.position;
|
||||||
|
final duration = widget.controller.value.duration;
|
||||||
|
var target = position + offset;
|
||||||
|
if (target < Duration.zero) target = Duration.zero;
|
||||||
|
if (target > duration) target = duration;
|
||||||
|
widget.controller.seekTo(target);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Container(
|
return Container(
|
||||||
@@ -723,6 +810,15 @@ class _VideoControlsState extends State<_VideoControls> {
|
|||||||
Row(
|
Row(
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
children: [
|
children: [
|
||||||
|
IconButton(
|
||||||
|
icon: const Icon(
|
||||||
|
Icons.replay_10_rounded,
|
||||||
|
color: Colors.white,
|
||||||
|
),
|
||||||
|
tooltip: 'Retroceder 10s',
|
||||||
|
onPressed: () =>
|
||||||
|
_seekBy(const Duration(seconds: -10)),
|
||||||
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: Icon(
|
icon: Icon(
|
||||||
widget.controller.value.isPlaying
|
widget.controller.value.isPlaying
|
||||||
@@ -738,16 +834,20 @@ class _VideoControlsState extends State<_VideoControls> {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
IconButton(
|
||||||
|
icon: const Icon(
|
||||||
|
Icons.forward_10_rounded,
|
||||||
|
color: Colors.white,
|
||||||
|
),
|
||||||
|
tooltip: 'Avançar 10s',
|
||||||
|
onPressed: () => _seekBy(const Duration(seconds: 10)),
|
||||||
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: const Icon(Icons.fullscreen, color: Colors.white),
|
icon: const Icon(Icons.fullscreen, color: Colors.white),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
// Dispose the cached controller to avoid codec conflict with fullscreen controller
|
// Liberta todos os decodificadores de prévia antes
|
||||||
if (_videoControllerCache.containsKey(
|
// de abrir o player em fullscreen.
|
||||||
widget.videoPath,
|
_evictAllVideoControllers();
|
||||||
)) {
|
|
||||||
_videoControllerCache[widget.videoPath]!.dispose();
|
|
||||||
_videoControllerCache.remove(widget.videoPath);
|
|
||||||
}
|
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
Navigator.of(context).push(
|
Navigator.of(context).push(
|
||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
@@ -826,6 +926,15 @@ class _FullscreenVideoPlayerState extends State<_FullscreenVideoPlayer> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _seekBy(Duration offset) {
|
||||||
|
final position = _controller.value.position;
|
||||||
|
final duration = _controller.value.duration;
|
||||||
|
var target = position + offset;
|
||||||
|
if (target < Duration.zero) target = Duration.zero;
|
||||||
|
if (target > duration) target = duration;
|
||||||
|
_controller.seekTo(target);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
|
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
|
||||||
@@ -854,20 +963,49 @@ class _FullscreenVideoPlayerState extends State<_FullscreenVideoPlayer> {
|
|||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
if (_isInitialized)
|
if (_isInitialized)
|
||||||
FloatingActionButton(
|
Row(
|
||||||
heroTag: 'play_pause',
|
mainAxisSize: MainAxisSize.min,
|
||||||
backgroundColor: VideoScreen._teal,
|
children: [
|
||||||
onPressed: () {
|
FloatingActionButton(
|
||||||
if (_controller.value.isPlaying) {
|
heroTag: 'rewind',
|
||||||
_controller.pause();
|
mini: true,
|
||||||
} else {
|
backgroundColor: VideoScreen._teal,
|
||||||
_controller.play();
|
onPressed: () => _seekBy(const Duration(seconds: -10)),
|
||||||
}
|
child: const Icon(
|
||||||
},
|
Icons.replay_10_rounded,
|
||||||
child: Icon(
|
color: Colors.white,
|
||||||
_controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
|
),
|
||||||
color: Colors.white,
|
),
|
||||||
),
|
const SizedBox(width: 16),
|
||||||
|
FloatingActionButton(
|
||||||
|
heroTag: 'play_pause',
|
||||||
|
backgroundColor: VideoScreen._teal,
|
||||||
|
onPressed: () {
|
||||||
|
if (_controller.value.isPlaying) {
|
||||||
|
_controller.pause();
|
||||||
|
} else {
|
||||||
|
_controller.play();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
child: Icon(
|
||||||
|
_controller.value.isPlaying
|
||||||
|
? Icons.pause
|
||||||
|
: Icons.play_arrow,
|
||||||
|
color: Colors.white,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 16),
|
||||||
|
FloatingActionButton(
|
||||||
|
heroTag: 'forward',
|
||||||
|
mini: true,
|
||||||
|
backgroundColor: VideoScreen._teal,
|
||||||
|
onPressed: () => _seekBy(const Duration(seconds: 10)),
|
||||||
|
child: const Icon(
|
||||||
|
Icons.forward_10_rounded,
|
||||||
|
color: Colors.white,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
if (_isInitialized) const SizedBox(height: 16),
|
if (_isInitialized) const SizedBox(height: 16),
|
||||||
FloatingActionButton(
|
FloatingActionButton(
|
||||||
|
|||||||
11
lib/widgets/app_gradients.dart
Normal file
11
lib/widgets/app_gradients.dart
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
/// Gradiente usado em todas as app bars da aplicação — um destaque em verde
|
||||||
|
/// vivo concentrado no canto superior direito, dissolvendo rapidamente para
|
||||||
|
/// o teal da marca no resto da barra.
|
||||||
|
const LinearGradient kAppBarGradient = LinearGradient(
|
||||||
|
begin: Alignment.topRight,
|
||||||
|
end: Alignment.bottomLeft,
|
||||||
|
colors: [Color(0xFF31C679), Color(0xFF2F9E94)],
|
||||||
|
stops: [0.0, 0.55],
|
||||||
|
);
|
||||||
Reference in New Issue
Block a user