feat(example_full): create example representing the whole plugin functional
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:nearby_service_example_full/domain/app_service.dart';
|
||||
import 'package:nearby_service_example_full/uikit/uikit.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class CheckServiceView extends StatefulWidget {
|
||||
const CheckServiceView({super.key});
|
||||
|
||||
@override
|
||||
State<CheckServiceView> createState() => _CheckServiceViewState();
|
||||
}
|
||||
|
||||
class _CheckServiceViewState extends State<CheckServiceView> {
|
||||
bool showEnableButton = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
ActionButton(
|
||||
onTap: () {
|
||||
context.read<AppService>().checkWifiService().then((value) {
|
||||
if (!value) {
|
||||
setState(() {
|
||||
showEnableButton = true;
|
||||
});
|
||||
AppShackBar.show(
|
||||
context,
|
||||
'Please enable Wi-fi',
|
||||
actionType: ActionType.warning,
|
||||
);
|
||||
}
|
||||
});
|
||||
},
|
||||
title: 'Check Wi-fi service',
|
||||
),
|
||||
if (showEnableButton)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 10.0),
|
||||
child: ActionButton(
|
||||
onTap: context.read<AppService>().openServicesSettings,
|
||||
title: 'Open settings',
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:nearby_service_example_full/domain/app_service.dart';
|
||||
import 'package:nearby_service_example_full/presentation/app.dart';
|
||||
import 'package:nearby_service_example_full/uikit/uikit.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../components/device_preview.dart';
|
||||
|
||||
class CommunicationView extends StatefulWidget {
|
||||
const CommunicationView({super.key});
|
||||
|
||||
@override
|
||||
State<CommunicationView> createState() => _CommunicationViewState();
|
||||
}
|
||||
|
||||
class _CommunicationViewState extends State<CommunicationView> {
|
||||
String message = '';
|
||||
List<PlatformFile> files = [];
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Consumer<AppService>(
|
||||
builder: (context, service, _) {
|
||||
final device = service.connectedDevice;
|
||||
if (device == null) {
|
||||
return Center(
|
||||
child: ActionButton(
|
||||
onTap: service.stopListeningAll,
|
||||
title: 'Restart',
|
||||
),
|
||||
);
|
||||
}
|
||||
final inputBorder = OutlineInputBorder(
|
||||
borderSide: const BorderSide(color: kGreenColor),
|
||||
borderRadius: BorderRadius.circular(32),
|
||||
);
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
DevicePreview(device: device, largeView: true),
|
||||
const SizedBox(height: 10),
|
||||
Flexible(
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Expanded(
|
||||
flex: 2,
|
||||
child: TextField(
|
||||
onChanged: (value) => setState(() {
|
||||
message = value;
|
||||
}),
|
||||
decoration: InputDecoration(
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 24,
|
||||
vertical: 12,
|
||||
),
|
||||
enabledBorder: inputBorder,
|
||||
border: inputBorder,
|
||||
focusedBorder: inputBorder,
|
||||
hintStyle: const TextStyle(color: kGreenColor),
|
||||
hintText: 'Enter a message',
|
||||
),
|
||||
),
|
||||
),
|
||||
Flexible(
|
||||
child: ActionButton(
|
||||
title: 'Send',
|
||||
onTap: () => service.sendMessage(message),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
const Text('OR'),
|
||||
const SizedBox(height: 10),
|
||||
Flexible(
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Expanded(
|
||||
flex: 2,
|
||||
child: ActionButton(
|
||||
type: ActionType.success,
|
||||
title: 'Choose files',
|
||||
onTap: () async {
|
||||
final result = await FilePicker.platform.pickFiles(
|
||||
allowMultiple: true,
|
||||
);
|
||||
setState(() {
|
||||
files = [...?result?.files];
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
Flexible(
|
||||
child: ActionButton(
|
||||
title: 'Send',
|
||||
onTap: () => service.sendFilesRequest([
|
||||
...files
|
||||
.map((e) => e.path)
|
||||
.where((element) => element != null)
|
||||
.cast<String>(),
|
||||
]),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
const Text('Selected files:', style: TextStyle(fontSize: 18)),
|
||||
Flexible(
|
||||
child: GridView.count(
|
||||
shrinkWrap: true,
|
||||
crossAxisCount: 2,
|
||||
mainAxisSpacing: 12,
|
||||
crossAxisSpacing: 12,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
children: [
|
||||
...files.where((element) => element.path != null).map(
|
||||
(e) => Image.file(
|
||||
File(e.path!),
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:nearby_service/nearby_service.dart';
|
||||
import 'package:nearby_service_example_full/domain/app_service.dart';
|
||||
import 'package:nearby_service_example_full/uikit/uikit.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../components/device_preview.dart';
|
||||
|
||||
class ConnectedView extends StatelessWidget {
|
||||
const ConnectedView({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Consumer<AppService>(
|
||||
builder: (context, service, _) {
|
||||
final device = service.connectedDevice;
|
||||
return device != null
|
||||
? Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
if (!device.status.isConnected)
|
||||
const Center(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(8.0),
|
||||
child: Text('Connection lost'),
|
||||
),
|
||||
)
|
||||
else if (!device.status.isConnected)
|
||||
Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const Text('Connection lost'),
|
||||
ActionButton(
|
||||
onTap: () {
|
||||
service.connect(device);
|
||||
},
|
||||
title: 'Reconnect',
|
||||
),
|
||||
],
|
||||
)
|
||||
else
|
||||
DevicePreview(device: device, largeView: true),
|
||||
const SizedBox(height: 10),
|
||||
if (service.communicationChannelState !=
|
||||
CommunicationChannelState.loading)
|
||||
ActionButton(
|
||||
title: 'Start communicate',
|
||||
onTap: () => service.startCommunicationChannel(
|
||||
listener: (event) => _listener(context, event),
|
||||
onFilesSaved: (files) => _onFileSaved(context, files),
|
||||
),
|
||||
)
|
||||
else
|
||||
Text(
|
||||
'Connecting socket.. '
|
||||
'${service.isAndroidGroupOwner != null ? service.isAndroidGroupOwner! ? "Waiting a client for connect" : "Waiting a server for connect" : "Waiting a connection"}',
|
||||
)
|
||||
],
|
||||
),
|
||||
)
|
||||
: const SizedBox();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _listener(BuildContext context, ReceivedNearbyMessage message) {
|
||||
final senderSubtitle = 'From ${message.sender.displayName} '
|
||||
'(ID: ${message.sender.id})';
|
||||
message.content.byType(
|
||||
onText: (content) {
|
||||
AppShackBar.show(
|
||||
Scaffold.of(context).context,
|
||||
content.value,
|
||||
subtitle: senderSubtitle,
|
||||
);
|
||||
},
|
||||
onFilesRequest: (content) {
|
||||
ActionDialog.show(
|
||||
context,
|
||||
title: 'Request to send ${content.files.length} files',
|
||||
subtitle: senderSubtitle,
|
||||
).then((value) {
|
||||
if (value is bool) {
|
||||
context.read<AppService>().sendFilesResponse(
|
||||
content.id,
|
||||
response: value,
|
||||
);
|
||||
}
|
||||
});
|
||||
},
|
||||
onFilesResponse: (content) {
|
||||
AppShackBar.show(
|
||||
Scaffold.of(context).context,
|
||||
content.response ? 'Request is accepted!' : 'Request was denied :(',
|
||||
subtitle: senderSubtitle,
|
||||
actionType: content.response ? ActionType.idle : ActionType.warning,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _onFileSaved(BuildContext context, ReceivedNearbyFilesPack pack) {
|
||||
final senderSubtitle = 'From ${pack.sender.displayName} '
|
||||
'(ID: ${pack.sender.id})';
|
||||
AppShackBar.show(
|
||||
Scaffold.of(context).context,
|
||||
'${pack.files.length} files saved! \n${pack.files.map((e) => e.name).join('\n')}',
|
||||
subtitle: senderSubtitle,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:nearby_service_example_full/domain/app_service.dart';
|
||||
import 'package:nearby_service_example_full/uikit/uikit.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class DiscoveryView extends StatelessWidget {
|
||||
const DiscoveryView({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
ActionButton(
|
||||
onTap: context.read<AppService>().startListeningPeers,
|
||||
title: 'Tap to get peers!',
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
ActionButton(
|
||||
type: ActionType.warning,
|
||||
onTap: context.read<AppService>().stopDiscovery,
|
||||
title: 'Stop discovery',
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:nearby_service_example_full/domain/app_service.dart';
|
||||
import 'package:nearby_service_example_full/uikit/uikit.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class IdleView extends StatefulWidget {
|
||||
const IdleView({super.key});
|
||||
|
||||
@override
|
||||
State<IdleView> createState() => _IdleViewState();
|
||||
}
|
||||
|
||||
class _IdleViewState extends State<IdleView> {
|
||||
late final controller = TextEditingController();
|
||||
bool initialized = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
|
||||
context.read<AppService>().getSavedIOSDeviceName().then((value) {
|
||||
controller.text = value;
|
||||
controller.selection = TextSelection.collapsed(offset: value.length);
|
||||
setState(() {
|
||||
initialized = true;
|
||||
});
|
||||
});
|
||||
});
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (!initialized) {
|
||||
return const Center(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
'Getting saved name...',
|
||||
style: TextStyle(fontSize: 12),
|
||||
),
|
||||
SizedBox(height: 10),
|
||||
CircularProgressIndicator.adaptive(),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
return Center(
|
||||
child: Column(
|
||||
children: [
|
||||
if (Platform.isIOS)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 10.0),
|
||||
child: TextFormField(
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Device Name',
|
||||
hintText: 'Enter the name of your device',
|
||||
),
|
||||
controller: controller,
|
||||
),
|
||||
),
|
||||
ActionButton(
|
||||
onTap: () {
|
||||
context.read<AppService>().initialize(controller.text);
|
||||
},
|
||||
title: 'Tap to start',
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:nearby_service_example_full/domain/app_service.dart';
|
||||
import 'package:nearby_service_example_full/uikit/uikit.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class PermissionsView extends StatelessWidget {
|
||||
const PermissionsView({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Consumer<AppService>(builder: (context, service, _) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
ActionButton(
|
||||
onTap: service.requestPermissions,
|
||||
title: 'Request permissions',
|
||||
),
|
||||
],
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:nearby_service_example_full/domain/app_service.dart';
|
||||
import 'package:nearby_service_example_full/domain/app_state.dart';
|
||||
import 'package:nearby_service_example_full/uikit/uikit.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class ReadyView extends StatelessWidget {
|
||||
const ReadyView({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
ActionButton(
|
||||
onTap: context.read<AppService>().discover,
|
||||
title: 'Start discover peers',
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
if (Platform.isIOS)
|
||||
ActionButton(
|
||||
onTap: () {
|
||||
context.read<AppService>().updateState(AppState.selectClientType);
|
||||
},
|
||||
title: 'Reselect client type',
|
||||
type: ActionType.warning,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:nearby_service_example_full/domain/app_service.dart';
|
||||
import 'package:nearby_service_example_full/uikit/uikit.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class SelectClientTypeView extends StatelessWidget {
|
||||
const SelectClientTypeView({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
ActionButton(
|
||||
title: 'Yes',
|
||||
onTap: () {
|
||||
context.read<AppService>().setIsBrowser(value: true);
|
||||
},
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
ActionButton(
|
||||
title: 'No',
|
||||
onTap: () {
|
||||
context.read<AppService>().setIsBrowser(value: false);
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:nearby_service_example_full/domain/app_service.dart';
|
||||
import 'package:nearby_service_example_full/uikit/uikit.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../components/device_preview.dart';
|
||||
|
||||
class StreamingPeersView extends StatelessWidget {
|
||||
const StreamingPeersView({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
ActionButton(
|
||||
type: ActionType.warning,
|
||||
onTap: context.read<AppService>().stopListeningPeers,
|
||||
title: 'Stop stream peers',
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
const _PeersBody(),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _PeersBody extends StatelessWidget {
|
||||
const _PeersBody();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Consumer<AppService>(
|
||||
builder: (context, service, _) {
|
||||
return (service.peers != null && service.peers!.isNotEmpty)
|
||||
? Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
...service.peers!.map(
|
||||
(e) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.only(bottom: 8.0),
|
||||
child: DevicePreview(device: e),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
)
|
||||
: Text(
|
||||
Platform.isAndroid || service.isIOSBrowser
|
||||
? 'No one here ('
|
||||
: "Wait until someone invites you!",
|
||||
textAlign: TextAlign.center,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
export 'check_service_view.dart';
|
||||
export 'communication_view.dart';
|
||||
export 'connected_view.dart';
|
||||
export 'discovery_view.dart';
|
||||
export 'idle_view.dart';
|
||||
export 'permissions_view.dart';
|
||||
export 'ready_view.dart';
|
||||
export 'select_client_type_view.dart';
|
||||
export 'streaming_peers_view.dart';
|
||||
Reference in New Issue
Block a user