import 'dart:convert'; import 'package:MeinCantor/main.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:cyclop/cyclop.dart'; import 'package:MeinCantor/networking.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:MeinCantor/presets/colors.dart'; import 'package:MeinCantor/presets/subjects.dart'; import 'package:MeinCantor/presets/teachers.dart'; class PlanSettings extends StatefulWidget { const PlanSettings({Key? key}) : super(key: key); @override State createState() => _PlanSettingsState(); } Future buildPlanColors(lesson) async { SharedPreferences prefs = await SharedPreferences.getInstance(); if (!prefs.containsKey("color$lesson")) { prefs.setInt("color$lesson", colors[lesson].value ?? Colors.grey.value); } await fetchLessonList(); Color colorDeu = Color(prefs.getInt("color$lesson")!); return colorDeu; } Future> buildLessonsList() async { await fetchLessonList(); SharedPreferences prefs = await SharedPreferences.getInstance(); String lessonsJson = prefs.getString("lessons")!; List lessons = jsonDecode(lessonsJson); return lessons; } class _PlanSettingsState extends State { Set swatches = { ...Colors.primaries, ...Colors.accents, Palette.accent, Palette.primary }; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text("Plan"), centerTitle: true, ), body: ListView( padding: const EdgeInsets.fromLTRB(5, 5, 5, 5), children: [ const ListTile( title: Text("Kurse/Fächer"), leading: Icon(Icons.list_alt_outlined), ), FutureBuilder( future: buildLessonsList(), builder: (context, snapshot) { if (snapshot.hasData) { List children = []; for (var element in (snapshot.data as List)) { String subject = element['subject']; String teacher = element['teacher']; children.add( FutureBuilder( future: buildPlanColors(subject), builder: (context, snapshot) { if (snapshot.hasData) { Color color = snapshot.data as Color; return ListTile( leading: ColorButton( key: const Key('c1'), color: color, config: const ColorPickerConfig( enableEyePicker: false), onSwatchesChanged: (Set value) { swatches = value; }, size: 32, swatches: swatches, onColorChanged: (Color value) async { setState(() { color = value; }); SharedPreferences prefs = await SharedPreferences .getInstance(); prefs.setInt( "color$subject", value.value); }), title: Text(subjects[subject] ?? ""), subtitle: Text(teachers[teacher] ?? ""), ); } else { return (const LinearProgressIndicator()); } }), ); } return Column( children: children, ); } else { return (const Center(child: CircularProgressIndicator())); } }), ], )); } }