This repository has been archived on 2023-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
meincantor-app/lib/dashboard.dart

536 lines
20 KiB
Dart
Raw Normal View History

import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:meincantor/const.dart';
import 'package:meincantor/raumuebersicht.dart';
import 'package:meincantor/schulbibliothek.dart';
import 'package:meincantor/schulcomputer.dart';
import 'package:meincantor/schuelerzeitung.dart';
import 'package:meincantor/Settings/dashboard.dart';
import 'package:meincantor/main.dart';
import 'package:meincantor/networking.dart';
import 'package:meincantor/login.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'news.dart';
class Dashboard extends StatefulWidget {
const Dashboard({Key? key, this.restorationId}) : super(key: key);
final String? restorationId;
@override
State<StatefulWidget> createState() => _DashboardState();
}
Future<String> getSettingsString(String key) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? value = prefs.getString(key);
if (value == null || value.isEmpty) {
value = "";
}
return value;
}
Widget buildSettingsString(String key, TextStyle? style) {
return FutureBuilder(
future: getSettingsString(key),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data as String, style: style);
} else {
return (const Center(child: CircularProgressIndicator()));
}
});
}
class _DashboardState extends State<Dashboard> with RestorationMixin {
final RestorableInt _currentIndex = RestorableInt(0);
@override
Widget build(BuildContext context) {
final drawerElements = ListView(
children: [
UserAccountsDrawerHeader(
accountName: buildSettingsString('name', const TextStyle()),
accountEmail: buildSettingsString('user', const TextStyle()),
currentAccountPicture: FutureBuilder(
future: Future.sync(() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? user = prefs.getString("user");
if (user == null || user.isEmpty) {
user = "";
}
String? name = prefs.getString("name");
if (name == null || name.isEmpty) {
name = "";
}
Map data = {"user": user, "name": name };
return data;
}),
builder: (context, snapshot) {
if (snapshot.hasData) {
// .svg?text=${(snapshot.data! as Map)['name'][0]}
String url = "$avatarUrl/${(snapshot.data! as Map)['user']}";
return Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.scaleDown,
image: NetworkImage(url)
)
)
);
} else {
return const CircularProgressIndicator();
}
},
)
),
ListTile(
title: const Text("Einstellungen"),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const Settings()),
);
},
leading: const Icon(Icons.settings_outlined),
),
ListTile(
title: const Text("Abmelden"),
onTap: () async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('api_key', "");
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => Login()),
);
},
leading: const Icon(Icons.exit_to_app_outlined),
),
],
);
var bottomNavBarItems = [
const BottomNavigationBarItem(
icon: Icon(MdiIcons.homeOutline),
label: "Startseite",
),
const BottomNavigationBarItem(
icon: Icon(MdiIcons.timetable), label: "Vertretungsplan"),
const BottomNavigationBarItem(
icon: Icon(Icons.notifications_outlined), label: "Hinweise"),
];
return Scaffold(
appBar: AppBar(
title: const Text("GCG.MeinCantor"),
centerTitle: true,
),
body: _DashboardBottomNavView(
key: UniqueKey(), item: bottomNavBarItems[_currentIndex.value]),
drawer: Drawer(
child: drawerElements,
),
bottomNavigationBar: BottomNavigationBar(
showUnselectedLabels: false,
items: bottomNavBarItems,
currentIndex: _currentIndex.value,
onTap: (index) {
setState(() {
_currentIndex.value = index;
});
},
),
);
}
@override
String? get restorationId => widget.restorationId;
@override
void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
registerForRestoration(_currentIndex, 'bottom_navigation_tab_index');
}
}
class _DashboardBottomNavView extends StatelessWidget {
const _DashboardBottomNavView({Key? key, required this.item})
: super(key: key);
final BottomNavigationBarItem item;
@override
Widget build(BuildContext context) {
if (item.label == "Startseite") {
double _timeOfDayToDouble(TimeOfDay tod) => tod.hour + tod.minute / 60.0;
int lessonCount;
if (_timeOfDayToDouble(TimeOfDay.now()) <=
_timeOfDayToDouble(const TimeOfDay(hour: 7, minute: 30))) {
lessonCount = 1;
} else if (_timeOfDayToDouble(TimeOfDay.now()) >
_timeOfDayToDouble(const TimeOfDay(hour: 7, minute: 30)) &&
_timeOfDayToDouble(TimeOfDay.now()) <=
_timeOfDayToDouble(const TimeOfDay(hour: 8, minute: 20))) {
lessonCount = 2;
} else if (_timeOfDayToDouble(TimeOfDay.now()) >
_timeOfDayToDouble(const TimeOfDay(hour: 8, minute: 20)) &&
_timeOfDayToDouble(TimeOfDay.now()) <=
_timeOfDayToDouble(const TimeOfDay(hour: 9, minute: 25))) {
lessonCount = 3;
} else if (_timeOfDayToDouble(TimeOfDay.now()) >
_timeOfDayToDouble(const TimeOfDay(hour: 9, minute: 25)) &&
_timeOfDayToDouble(TimeOfDay.now()) <=
_timeOfDayToDouble(const TimeOfDay(hour: 10, minute: 15))) {
lessonCount = 4;
} else if (_timeOfDayToDouble(TimeOfDay.now()) >
_timeOfDayToDouble(const TimeOfDay(hour: 10, minute: 15)) &&
_timeOfDayToDouble(TimeOfDay.now()) <=
_timeOfDayToDouble(const TimeOfDay(hour: 11, minute: 30))) {
lessonCount = 5;
} else if (_timeOfDayToDouble(TimeOfDay.now()) >
_timeOfDayToDouble(const TimeOfDay(hour: 11, minute: 30)) &&
_timeOfDayToDouble(TimeOfDay.now()) <=
_timeOfDayToDouble(const TimeOfDay(hour: 12, minute: 20))) {
lessonCount = 6;
} else if (_timeOfDayToDouble(TimeOfDay.now()) >
_timeOfDayToDouble(const TimeOfDay(hour: 12, minute: 20)) &&
_timeOfDayToDouble(TimeOfDay.now()) <=
_timeOfDayToDouble(const TimeOfDay(hour: 13, minute: 30))) {
lessonCount = 7;
} else if (_timeOfDayToDouble(TimeOfDay.now()) >
_timeOfDayToDouble(const TimeOfDay(hour: 13, minute: 30)) &&
_timeOfDayToDouble(TimeOfDay.now()) <=
_timeOfDayToDouble(const TimeOfDay(hour: 14, minute: 20))) {
lessonCount = 8;
} else if (_timeOfDayToDouble(TimeOfDay.now()) >
_timeOfDayToDouble(const TimeOfDay(hour: 14, minute: 20)) &&
_timeOfDayToDouble(TimeOfDay.now()) <=
_timeOfDayToDouble(const TimeOfDay(hour: 15, minute: 10))) {
lessonCount = 9;
2021-11-19 20:12:42 +01:00
} else if (_timeOfDayToDouble(TimeOfDay.now()) >
_timeOfDayToDouble(const TimeOfDay(hour: 15, minute: 10)) &&
2021-11-19 20:12:42 +01:00
_timeOfDayToDouble(TimeOfDay.now()) <=
_timeOfDayToDouble(const TimeOfDay(hour: 16, minute: 00))) {
lessonCount = 10;
} else {
lessonCount = -1;
}
var view = SingleChildScrollView(
child: Column(children: [
Row(
mainAxisSize: MainAxisSize.max,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(20, 30, 20, 5),
child: Text(
'Hallo,',
style: GoogleFonts.robotoSlab(
fontSize: 20,
),
),
),
],
),
Row(
mainAxisSize: MainAxisSize.max,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(20, 5, 20, 20),
child: buildSettingsString(
"name",
GoogleFonts.robotoSlab(
fontSize: 28,
fontWeight: FontWeight.w800,
),
),
)
],
),
Row(
mainAxisSize: MainAxisSize.max,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(20, 10, 0, 10),
child: Text(
'Deine nächste Unterrichtsstunde:',
style: GoogleFonts.robotoSlab(
fontSize: 20,
fontWeight: FontWeight.w100,
),
),
)
],
),
Padding(
padding: const EdgeInsets.fromLTRB(20, 20, 20, 10),
child: buildTodayClassTimetableLesson(lessonCount),
),
Padding(
padding: const EdgeInsets.fromLTRB(20, 20, 20, 10),
child: Wrap(
children: [
SizedBox(
child: GestureDetector(
onTap: () async {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const SZ()),
);
},
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
child: const Padding(
padding: EdgeInsets.all(10),
child: ListTile(
title: Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
child: Icon(
MdiIcons.newspaper,
color: Palette.accent,
size: 48,
),
),
subtitle: Center(
child: Padding(
padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
child: Text(
'Schülerzeitung',
),
),
),
),
)),
),
width: 170,
),
SizedBox(
width: 170,
child: GestureDetector(
onTap: () async {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const SB()),
);
},
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
child: const Padding(
padding: EdgeInsets.all(10),
child: ListTile(
title: Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
child: Icon(
MdiIcons.libraryShelves,
color: Palette.accent,
size: 48,
),
),
subtitle: Center(
child: Padding(
padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
child: Text(
'Schulbibliothek',
),
),
),
),
)),
),
),
SizedBox(
width: 170,
child: GestureDetector(
onTap: () async {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const SC()),
);
},
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
child: const Padding(
padding: EdgeInsets.all(10),
child: ListTile(
title: Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
child: Icon(
MdiIcons.laptop,
color: Palette.accent,
size: 48,
),
),
subtitle: Center(
child: Padding(
padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
child: Text(
'Schulcomputer',
),
),
),
),
)),
),
),
SizedBox(
width: 170,
child: GestureDetector(
onTap: () async {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const RoomOverview()),
);
},
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
child: const Padding(
padding: EdgeInsets.all(10),
child: ListTile(
title: Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
child: Icon(
MdiIcons.door,
color: Palette.accent,
size: 48,
),
),
subtitle: Center(
child: Padding(
padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
child: Text(
'Raumübersicht',
),
),
),
),
)),
),
),
SizedBox(
width: 170,
child: GestureDetector(
onTap: () async {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const News()),
);
},
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
child: const Padding(
padding: EdgeInsets.all(10),
child: ListTile(
title: Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
child: Icon(
MdiIcons.newspaperVariantOutline,
color: Palette.accent,
size: 48,
),
),
subtitle: Center(
child: Padding(
padding: EdgeInsets.fromLTRB(0, 10, 0, 0),
child: Text(
'Aktuelles',
),
),
),
),
)),
),
)
],
),
),
]),
);
return view;
} else if (item.label == "Vertretungsplan") {
return LayoutBuilder(builder: (context, constraints) {
double widgetWidth = constraints.maxWidth;
int factor;
if (widgetWidth <= 600) {
factor = 1;
} else if (widgetWidth <= 1400) {
factor = 2;
} else if (widgetWidth <= 2000) {
factor = 3;
} else {
factor = 1;
}
// print(screenType);
return Center(
child: Container(
constraints: BoxConstraints(
// minHeight: 500, //minimum height
// minWidth: 300, // minimum width
//maximum height set to 100% of vertical height
maxWidth: MediaQuery.of(context).size.width / factor,
//maximum width set to 100% of width
),
child: DefaultTabController(
initialIndex: 0,
length: 3,
child: Scaffold(
appBar: AppBar(
elevation: 0,
title: const TabBar(
tabs: <Widget>[
Tab(
text: "Heute",
icon: Icon(CupertinoIcons.calendar_today),
),
Tab(
text: "Morgen",
icon: Icon(CupertinoIcons.calendar_today),
),
Tab(
text: "Neuster Plan",
icon: Icon(CupertinoIcons.calendar),
),
],
),
),
body: TabBarView(
children: <Widget>[
buildTimetable(
fetchClassTimetable(
"/${DateFormat("yyyyMMdd").format(DateTime.now())}"),
"Vertretungsplan für heute"),
buildTimetable(
fetchClassTimetable(
"/${DateFormat("yyyyMMdd").format(DateTime.now().add(const Duration(days: 1)))}"),
"Vertretungsplan für morgen"),
buildTimetable(fetchClassTimetable("/latest"),
"aktueller Vertretungsplan")
],
),
),
),
),
);
});
} else {
return const Center(child: Text("Derzeit nichts hier..."));
}
}
}