feat: initialize new Flutter project with core feature structure and cross-platform configuration
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:shelf/shelf.dart';
|
||||
import 'package:shelf/shelf_io.dart' as io;
|
||||
import 'package:shelf_router/shelf_router.dart';
|
||||
|
||||
class ReceiverServer {
|
||||
HttpServer? _server;
|
||||
final Function(Map<String, dynamic>) onPlayRequest;
|
||||
|
||||
ReceiverServer({required this.onPlayRequest});
|
||||
|
||||
Future<int> start() async {
|
||||
final router = Router();
|
||||
|
||||
router.post('/play', (Request request) async {
|
||||
final payload = await request.readAsString();
|
||||
try {
|
||||
final data = jsonDecode(payload) as Map<String, dynamic>;
|
||||
onPlayRequest(data);
|
||||
return Response.ok(jsonEncode({'status': 'success'}));
|
||||
} catch (e) {
|
||||
return Response.badRequest(body: jsonEncode({'error': 'Invalid JSON'}));
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/health', (Request request) {
|
||||
return Response.ok(jsonEncode({'status': 'ok'}));
|
||||
});
|
||||
|
||||
_server = await io.serve(router, InternetAddress.anyIPv4, 0); // Port 0 for auto-assign or fixed port
|
||||
print('Receiver Server running on port ${_server!.port}');
|
||||
return _server!.port;
|
||||
}
|
||||
|
||||
Future<void> stop() async {
|
||||
await _server?.close();
|
||||
_server = null;
|
||||
}
|
||||
|
||||
String? get ipAddress {
|
||||
// Note: This is a simplified way to get local IP.
|
||||
// In a real app, you might want to filter for the Wi-Fi interface.
|
||||
return 'Local IP';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../data/services/receiver_server.dart';
|
||||
import '../../../discovery/data/services/discovery_service.dart';
|
||||
|
||||
class ReceiverState {
|
||||
final bool isRunning;
|
||||
final int? port;
|
||||
final String? ipAddress;
|
||||
final Map<String, dynamic>? currentVideo;
|
||||
|
||||
ReceiverState({
|
||||
this.isRunning = false,
|
||||
this.port,
|
||||
this.ipAddress,
|
||||
this.currentVideo,
|
||||
});
|
||||
|
||||
ReceiverState copyWith({
|
||||
bool? isRunning,
|
||||
int? port,
|
||||
String? ipAddress,
|
||||
Map<String, dynamic>? currentVideo,
|
||||
}) {
|
||||
return ReceiverState(
|
||||
isRunning: isRunning ?? this.isRunning,
|
||||
port: port ?? this.port,
|
||||
ipAddress: ipAddress ?? this.ipAddress,
|
||||
currentVideo: currentVideo ?? this.currentVideo,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final receiverProvider = NotifierProvider<ReceiverNotifier, ReceiverState>(() {
|
||||
return ReceiverNotifier();
|
||||
});
|
||||
|
||||
class ReceiverNotifier extends Notifier<ReceiverState> {
|
||||
late ReceiverServer _server;
|
||||
final _discoveryService = DiscoveryService();
|
||||
|
||||
@override
|
||||
ReceiverState build() {
|
||||
_server = ReceiverServer(onPlayRequest: _handlePlayRequest);
|
||||
return ReceiverState();
|
||||
}
|
||||
|
||||
Future<void> startServer() async {
|
||||
if (state.isRunning) return;
|
||||
|
||||
final port = await _server.start();
|
||||
await _discoveryService.registerService(port);
|
||||
|
||||
state = state.copyWith(
|
||||
isRunning: true,
|
||||
port: port,
|
||||
// In a real app, use network_info_plus to get the real IP
|
||||
ipAddress: 'Checking...',
|
||||
);
|
||||
}
|
||||
|
||||
void _handlePlayRequest(Map<String, dynamic> data) {
|
||||
state = state.copyWith(currentVideo: data);
|
||||
}
|
||||
|
||||
Future<void> stopServer() async {
|
||||
await _server.stop();
|
||||
await _discoveryService.stopRegistration();
|
||||
state = ReceiverState();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_svg/flutter_svg.dart';
|
||||
import '../../../../core/theme/app_colors.dart';
|
||||
import '../providers/receiver_providers.dart';
|
||||
import 'tv_player_screen.dart';
|
||||
|
||||
|
||||
class TVHomeScreen extends ConsumerStatefulWidget {
|
||||
const TVHomeScreen({super.key});
|
||||
|
||||
@override
|
||||
ConsumerState<TVHomeScreen> createState() => _TVHomeScreenState();
|
||||
}
|
||||
|
||||
class _TVHomeScreenState extends ConsumerState<TVHomeScreen> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// Start the server as soon as the TV home opens
|
||||
Future.microtask(() => ref.read(receiverProvider.notifier).startServer());
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final receiverState = ref.watch(receiverProvider);
|
||||
|
||||
ref.listen(receiverProvider, (previous, next) {
|
||||
final video = next.currentVideo;
|
||||
if (video != null) {
|
||||
final url = video['url'] as String;
|
||||
final title = video['title'] as String?;
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => TVPlayerScreen(url: url, title: title),
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
return Scaffold(
|
||||
|
||||
body: Container(
|
||||
width: double.infinity,
|
||||
decoration: const BoxDecoration(
|
||||
gradient: AppColors.backgroundGradient,
|
||||
),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Spacer(),
|
||||
// Glowing Logo Animation Placeholder
|
||||
TweenAnimationBuilder<double>(
|
||||
tween: Tween(begin: 0.8, end: 1.0),
|
||||
duration: const Duration(seconds: 2),
|
||||
curve: Curves.easeInOut,
|
||||
builder: (context, value, child) {
|
||||
return Transform.scale(
|
||||
scale: value,
|
||||
child: Opacity(
|
||||
opacity: 0.5 + (value - 0.8) * 2.5,
|
||||
child: child,
|
||||
),
|
||||
);
|
||||
},
|
||||
onEnd: () {}, // Could loop it
|
||||
child: SvgPicture.asset(
|
||||
'assets/logo/fastwatcher-logo.svg',
|
||||
height: 180,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 48),
|
||||
Text(
|
||||
'Waiting for connection...',
|
||||
style: Theme.of(context).textTheme.displaySmall?.copyWith(
|
||||
color: Colors.white.withOpacity(0.9),
|
||||
fontWeight: FontWeight.w300,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'Open FastWatcher on your phone to start casting',
|
||||
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
|
||||
color: AppColors.textSecondary,
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
// Connection Info Footer
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(48.0),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
_InfoItem(
|
||||
label: 'DEVICE NAME',
|
||||
value: 'FastWatcher TV',
|
||||
),
|
||||
if (receiverState.isRunning)
|
||||
_InfoItem(
|
||||
label: 'SERVER PORT',
|
||||
value: '${receiverState.port}',
|
||||
),
|
||||
_InfoItem(
|
||||
label: 'IP ADDRESS',
|
||||
value: receiverState.ipAddress ?? 'Detecting...',
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _InfoItem extends StatelessWidget {
|
||||
final String label;
|
||||
final String value;
|
||||
|
||||
const _InfoItem({required this.label, required this.value});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
label,
|
||||
style: Theme.of(context).textTheme.labelSmall?.copyWith(
|
||||
color: AppColors.primary,
|
||||
letterSpacing: 2,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
value,
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontFamily: 'monospace',
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:media_kit/media_kit.dart';
|
||||
import 'package:media_kit_video/media_kit_video.dart';
|
||||
|
||||
class TVPlayerScreen extends StatefulWidget {
|
||||
final String url;
|
||||
final String? title;
|
||||
|
||||
const TVPlayerScreen({super.key, required this.url, this.title});
|
||||
|
||||
@override
|
||||
State<TVPlayerScreen> createState() => _TVPlayerScreenState();
|
||||
}
|
||||
|
||||
class _TVPlayerScreenState extends State<TVPlayerScreen> {
|
||||
late final player = Player();
|
||||
late final controller = VideoController(player);
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
player.open(Media(widget.url));
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
player.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.black,
|
||||
body: Stack(
|
||||
children: [
|
||||
Video(controller: controller),
|
||||
if (widget.title != null)
|
||||
Positioned(
|
||||
top: 40,
|
||||
left: 40,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.black54,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Text(
|
||||
widget.title!,
|
||||
style: const TextStyle(color: Colors.white, fontSize: 24),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user