202 lines
6.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:mycore_api/api.dart';
import 'package:myhomie_app/Components/loading_common.dart';
import 'package:myhomie_app/Models/homieContext.dart';
import 'package:myhomie_app/Screens/Main/Devices/deviceDetailPage.dart';
import 'package:myhomie_app/app_context.dart';
import 'package:myhomie_app/constants.dart';
import 'package:provider/provider.dart';
class DevicesScreen extends StatefulWidget {
DevicesScreen({Key? key}) : super(key: key);
@override
_DevicesScreenState createState() => _DevicesScreenState();
}
class _DevicesScreenState extends State<DevicesScreen> {
bool isLoading = true;
//List<Message> messages;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
final appContext = Provider.of<AppContext>(context);
HomieAppContext homieAppContext = appContext.getContext();
return FutureBuilder(
future: homieAppContext.clientAPI.deviceApi!.deviceGetAll(homieAppContext.homeId!),
builder: (context, AsyncSnapshot<List<DeviceSummaryDTO>?> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
print("connectionState done");
print(snapshot);
if(snapshot.data != null) {
return interfaceElements(snapshot.data!);
} else {
return Text("No data - or error");
}
} else if (snapshot.connectionState == ConnectionState.none) {
print('ConnectionState.none');
return Text("No data");
} else {
return Container(height: size.height * 0.2, child: LoadingCommon());
}
}
);
/*if(appContext.getContext().feed != null) {
return interfaceElements();
} else {
return FutureBuilder(
future: Message.getMessages(this.messages, appContext, false, true),
builder: (context, AsyncSnapshot<List<Message>> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return interfaceElements();
} else if (snapshot.connectionState == ConnectionState.none) {
print('ConnectionState.none');
return Text("No data");
} else {
return Container(height: size.height * 0.2, child: Loading());
}
}
);
}*/
}
interfaceElements(List<DeviceSummaryDTO> devices) {
Size size = MediaQuery.of(context).size;
final appContext = Provider.of<AppContext>(context);
HomieAppContext homieAppContext = appContext.getContext();
return SingleChildScrollView(
child: RefreshIndicator(
color: Theme.of(context).primaryColor,
displacement: 20,
onRefresh: () async {
print("onRefresh");
await homieAppContext.clientAPI.deviceApi!.deviceGetAll(homieAppContext.homeId!);
},
child: Container(
height: size.height * 0.85,
child: GridView.builder(
shrinkWrap: true,
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3, childAspectRatio: 1),
itemCount: devices.length,
itemBuilder: (BuildContext context, int index) {
return InkWell(
onTap: () {
debugPrint(devices[index].toString());
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DeviceDetailPage(
deviceSummaryDTO: devices[index]
)
),
);
/*setState(() {
//selectedSection = menuDTO.sections[index];
print("Hero to room detail");
});*/
},
child: Container(
decoration: boxDecorationDevice(devices[index], false),
padding: const EdgeInsets.all(5),
margin: EdgeInsets.symmetric(vertical: 8, horizontal: 8),
child: Stack(
children: [
Center(
child: Text(
devices[index].name!,
style: new TextStyle(fontSize: kDescriptionDetailSize, color: kBackgroundSecondGrey),
textAlign: TextAlign.center,
),
),
if(devices[index].type != null)
Positioned(
top: 5,
child: Row(
children: [
Icon(
getDeviceIcon(devices[index].type!),
size: 20,
//color: devices[index].status! ? kMainColor : kBackgroundSecondGrey,
),
],
)
),
],
),
),
);
}
),
),
),
);
}
}
getDeviceIcon(DeviceType type) {
switch(type) {
case DeviceType.sensor:
return Icons.sensors;
case DeviceType.actuator:
return Icons.devices;
case DeviceType.camera:
return Icons.camera_alt_outlined;
case DeviceType.switch_:
return Icons.toggle_off_outlined;
case DeviceType.light:
return Icons.light;
case DeviceType.sound:
return Icons.volume_up;
case DeviceType.plug:
return Icons.outlet_outlined;
case DeviceType.multiplug:
return Icons.settings_input_component_outlined;
case DeviceType.thermostat:
return Icons.thermostat;
case DeviceType.valve:
return Icons.thermostat_auto;
case DeviceType.door:
return Icons.door_back_door_outlined;
case DeviceType.environment:
return Icons.sensors_sharp;
case DeviceType.motion:
return Icons.directions_run;
case DeviceType.gateway:
return Icons.dns_outlined;
case DeviceType.unknown:
return Icons.question_mark;
}
}
boxDecorationDevice(DeviceSummaryDTO deviceSummaryDTO, bool isSelected) {
return BoxDecoration(
color: kBackgroundLight,
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(20.0),
/*image: roomMainDetailDTO.imageSource != null ? new DecorationImage(
fit: BoxFit.contain,
colorFilter: !isSelected? new ColorFilter.mode(Colors.black.withOpacity(0.5), BlendMode.dstATop) : null,
image: new NetworkImage(
section.imageSource,
),
): null,*/
boxShadow: [
BoxShadow(
color: kBackgroundSecondGrey,
spreadRadius: 0.5,
blurRadius: 6,
offset: Offset(0, 1.5), // changes position of shadow
),
],
);
}