mirror of
https://bitbucket.org/myhomie/myhomie_app.git
synced 2025-12-06 09:01:20 +00:00
147 lines
4.8 KiB
Dart
147 lines
4.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/rendering.dart';
|
|
import 'package:mqtt_client/mqtt_server_client.dart';
|
|
import 'package:myhomie_app/Components/Custom_Navigation_Bar/custom_app_bar.dart';
|
|
import 'package:myhomie_app/Components/Custom_Navigation_Bar/custom_bottom_navigation_bar.dart';
|
|
import 'package:myhomie_app/Components/Custom_Navigation_Bar/custom_nav_item.dart';
|
|
import 'package:myhomie_app/Helpers/MQTTHelper.dart';
|
|
import 'package:myhomie_app/Helpers/PushNotificationService.dart';
|
|
import 'package:myhomie_app/Screens/Debug/DebugPage.dart';
|
|
import 'package:myhomie_app/Screens/Main/Automations/automations.dart';
|
|
import 'package:myhomie_app/Screens/Main/Devices/devices.dart';
|
|
import 'package:myhomie_app/app_context.dart';
|
|
import 'package:myhomie_app/constants.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
import '../../Models/homieContext.dart';
|
|
import 'Energy/energy.dart';
|
|
import 'Home/home.dart';
|
|
import 'Profile/profile.dart';
|
|
import 'Security/security.dart';
|
|
import 'index.dart';
|
|
|
|
PageController pageController = PageController(initialPage: 0);
|
|
int currentIndex = 0;
|
|
|
|
class MainPage extends StatefulWidget {
|
|
MainPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
_MainPageState createState() => _MainPageState();
|
|
}
|
|
|
|
class _MainPageState extends State<MainPage> {
|
|
bool isAlreadyInstantiated = false;
|
|
|
|
setPage() {
|
|
setState(() {
|
|
pageController.jumpToPage(currentIndex);
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final appContext = Provider.of<AppContext>(context);
|
|
HomieAppContext homieAppContext = appContext.getContext(); // TODO something if null
|
|
final MqttServerClient client = homieAppContext.clientMQTT; // TODO Add switch button or check online connexion if local broker available
|
|
//homieAppContext.clientMQTT = new MqttServerClient(homieAppContext.host.replaceAll('http://', ''),'my_homie_app_'+ Uuid().v1());
|
|
|
|
Size size = MediaQuery.of(context).size;
|
|
|
|
if (!MQTTHelper.instance.isInstantiated)
|
|
{
|
|
print("MQTT NOT INSTANTIATED");
|
|
MQTTHelper.instance.connect(appContext);
|
|
}
|
|
|
|
if (!isAlreadyInstantiated)
|
|
{
|
|
final pushNotificationService = PushNotificationService();
|
|
pushNotificationService.initialise(homieAppContext, context);
|
|
isAlreadyInstantiated = true;
|
|
}
|
|
|
|
return ChangeNotifierProvider<Index>(
|
|
create: (_) => Index(0, appContext.getContext()),
|
|
child: Scaffold(
|
|
extendBody: true,
|
|
appBar: CustomAppBar(),
|
|
body: PageView(
|
|
physics: NeverScrollableScrollPhysics(),
|
|
controller: pageController,
|
|
children: <Widget>[
|
|
HomeScreen(),
|
|
SecurityScreen(),
|
|
AutomationsScreen(),
|
|
DevicesScreen(),
|
|
EnergyScreen(),
|
|
//ProfileScreen(),
|
|
],
|
|
),
|
|
drawer: Drawer(
|
|
child: ListView(
|
|
// Important: Remove any padding from the ListView.
|
|
padding: EdgeInsets.zero,
|
|
children: [
|
|
const DrawerHeader(
|
|
decoration: BoxDecoration(
|
|
color: Colors.blue,
|
|
),
|
|
child: Text('Drawer Header'),
|
|
),
|
|
ListTile(
|
|
title: const Text('Debug screen'),
|
|
onTap: () {
|
|
Navigator.pop(context);
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) {
|
|
return DebugPage();
|
|
},
|
|
)
|
|
);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
backgroundColor: kMainColor,
|
|
onPressed: () {
|
|
/* var message = {
|
|
"userId": homieAppContext.userId,
|
|
"width": size.width,
|
|
"height": size.height,
|
|
"action": "button0"
|
|
};
|
|
|
|
const pubTopic = 'rpiZero/test';
|
|
final builder = MqttClientPayloadBuilder();
|
|
|
|
builder.addString(jsonEncode(message));
|
|
|
|
homieAppContext.clientMQTT.publishMessage(pubTopic, MqttQos.atLeastOnce, builder.payload);
|
|
|
|
var message2 = {
|
|
"state": "toggle"
|
|
};
|
|
|
|
const pubTopic2 = 'zigbee2mqtt/GU10Bureau/set';
|
|
final builder2 = MqttClientPayloadBuilder();
|
|
|
|
builder2.addString(jsonEncode(message2));
|
|
|
|
print("Send request");
|
|
|
|
homieAppContext.clientMQTT.publishMessage(pubTopic2, MqttQos.atLeastOnce, builder2.payload);
|
|
*/
|
|
},
|
|
child: CustomNavItem(setPage: setPage, icon: NavItemIcon.home, id: 0),
|
|
),
|
|
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
|
|
bottomNavigationBar: CustomBottomNavigationBar(),
|
|
),
|
|
);
|
|
}
|
|
} |