import 'dart:convert'; import 'package:bus_infotainment/backend/live_information.dart'; import 'package:bus_infotainment/remaster/dashboard.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart' hide NavigationBar; import 'package:native_qr/native_qr.dart'; import 'package:shadcn_ui/shadcn_ui.dart'; class JoinGroup extends StatefulWidget { @override State createState() => _JoinGroupState(); } class _JoinGroupState extends State { Future _joinGroup(String data) async { if (data.isEmpty) { ShadToaster.of(context).show( ShadToast( title: Text("Error connecting to room"), description: Text("Nothing was found."), ) ); } if (await LiveInformation().joinRoom(data)) { Navigator.pushNamed(context, "/multi/enroute"); } else { ShadToaster.of(context).show( ShadToast( title: Text("Error connecting to room"), description: Text("The room could not be found."), ) ); } } @override Widget build(BuildContext context) { if (defaultTargetPlatform == TargetPlatform.android) { LiveInformation().p2pModule.startDiscovery(); } return Scaffold( body: Container( child: Row( children: [ Expanded( child: Container( alignment: Alignment.center, child: Row( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ Container( margin: EdgeInsets.all(20), width: 100, child: Column( mainAxisSize: MainAxisSize.min, children: [ Expanded( child: RotatedBox( quarterTurns: 3, child: Container( height: double.infinity, child: ElevatedButton( onPressed: () async { NativeQr nativeQr = NativeQr(); String? result = await nativeQr.get(); _joinGroup(result!); }, child: Text( "Join from QR code", style: TextStyle( fontSize: 20, fontWeight: FontWeight.bold, ), textAlign: TextAlign.center, ), style: ElevatedButton.styleFrom( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10) ), ) ), ), ), ), SizedBox( height: 20, ), Expanded( child: RotatedBox( quarterTurns: 3, child: Container( height: double.infinity, child: ElevatedButton( onPressed: () { }, child: Text( "Join from clipboard", style: TextStyle( fontSize: 20, fontWeight: FontWeight.bold, ), textAlign: TextAlign.center, ), style: ElevatedButton.styleFrom( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10) ), ) ), ), ), ) ], ), ), Container( width: 2, color: Colors.grey.shade300, ), Expanded( child: Stack( children: [ Positioned.fill( child: Container( alignment: Alignment.center, child: Column( mainAxisSize: MainAxisSize.min, children: [ Text( "EXPERIMENTAL", style: TextStyle( color: Colors.grey.shade700.withOpacity(0.9), fontSize: 100, fontWeight: FontWeight.bold, height: 1 ), ), Text( "Working proof of concept - Rewrite iminent", style: TextStyle( color: Colors.grey.shade700.withOpacity(0.9), fontSize: 20, fontWeight: FontWeight.bold ), ), Text( "Certain parts may not work as expected. I am aware of all issues.", style: TextStyle( color: Colors.grey.shade700.withOpacity(0.9), fontSize: 20, fontWeight: FontWeight.bold ), ) ], ), ), ), Container( margin: EdgeInsets.all(20), child: Column( mainAxisSize: MainAxisSize.max, crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Text( "Nearby devices", style: ShadTheme.of(context).textTheme.h1 ), SizedBox( width: 20, ), ElevatedButton( onPressed: () { setState(() {}); }, child: Text("Refresh"), style: ElevatedButton.styleFrom( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10) ), ) ) ], ), if (defaultTargetPlatform == TargetPlatform.android) FutureBuilder( future: LiveInformation().p2pModule.getDiscoveredDevices(), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return CircularProgressIndicator(); } if (snapshot.hasError) { return Text("Error: ${snapshot.error}"); } List peers = []; for (var peer in snapshot.data!) { print("Info: "); print(jsonEncode(peer.info.toJson())); peers.add( ElevatedButton( onPressed: () async { await LiveInformation().p2pModule.connectToDevice(peer); LiveInformation().inRoom = true; LiveInformation().connectionMethod = RoomConnectionMethod.P2P; Navigator.pushNamed(context, "/multi/enroute"); }, child: Text(peer.info.displayName), style: ElevatedButton.styleFrom( shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10) ), ) ) ); } print(peers.length); return Column( children: [ ...peers ], ); }, ) else Expanded( child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text("This feature is not available on this platform."), Text("Please use the QR code or clipboard method.") ], ) ), ) ], ), ), ], ), ) ], ), ), ), Container( width: 2, color: Colors.grey.shade300, ), RotatedBox( quarterTurns: 3, child: NavigationBar() ) ], ), ), ); } }