Skip to content
Snippets Groups Projects
moodpage.dart 15 KiB
Newer Older
  • Learn to ignore specific revisions
  • import 'dart:ffi';
    
    Ede Dust's avatar
    Ede Dust committed
    import 'dart:io';
    
    MagicalSpyto's avatar
    MagicalSpyto committed
    import 'package:ambient/firebase.dart';
    
    import 'package:ambient/services/spotify.dart';
    
    MagicalSpyto's avatar
    MagicalSpyto committed
    
    
    import 'main.dart';
    
    Ede Dust's avatar
    Ede Dust committed
    import 'package:vector_math/vector_math.dart';
    
    Ede Dust's avatar
    Ede Dust committed
    import 'package:image_picker/image_picker.dart';
    
    import 'package:flutter/material.dart';
    
    Ede Dust's avatar
    Ede Dust committed
    import 'package:palette_generator/palette_generator.dart';
    
    import 'package:flutter_color_models/flutter_color_models.dart';
    
    Ede Dust's avatar
    Ede Dust committed
    
    class StateMoodPage extends StatefulWidget {
      const StateMoodPage({super.key});
    
      @override
      State<StateMoodPage> createState() => MoodPage();
    }
    
    
    enum Moods { none, happy, sad ,angry, noble, clean, courageous, fresh, compassionn, simple, dramatic}
    
    Ede Dust's avatar
    Ede Dust committed
    
    extension MoodsExtention on Moods {
      Color get color {
        switch (this) {
          case Moods.none:
            return Color.fromARGB(255, 75, 75, 75);
          case Moods.happy:
    
            return Color.fromARGB(255, 255, 255, 0);
    
    Ede Dust's avatar
    Ede Dust committed
          case Moods.sad:
    
            return Color.fromARGB(255, 0, 0, 255);
          case Moods.angry:
            return Color.fromARGB(255, 255, 0, 0);
          case Moods.noble:
            return Color.fromARGB(255, 127, 69, 216);
          case Moods.clean:
            return Color.fromARGB(255, 255, 255, 255);
          case Moods.courageous:
            return Color.fromARGB(255, 255, 165, 0);
          case Moods.fresh:
            return Color.fromARGB(255, 0, 255, 0);
          case Moods.compassionn:
            return Color.fromARGB(255, 255, 70, 253);
          case Moods.simple:
            return Color.fromARGB(255, 103, 64, 45);
          case Moods.dramatic:
            return Color.fromARGB(255, 0, 0, 0);
    
    Ede Dust's avatar
    Ede Dust committed
        }
      }
    }
    
    
    Ede Dust's avatar
    Ede Dust committed
    class MoodPage extends State<StateMoodPage> {
      final ImagePicker _picker = ImagePicker();
      late PaletteGenerator paletteGenerator;
    
    MagicalSpyto's avatar
    MagicalSpyto committed
      Moods? currentMood = selectedMoodAsEnum;
    
    Ede Dust's avatar
    Ede Dust committed
      String imagePath = "";
    
    
    MagicalSpyto's avatar
    MagicalSpyto committed
      //besorgt ein image file aus der galarie
    
    Ede Dust's avatar
    Ede Dust committed
      _getFromGallery() async {
        try {
          XFile? pickedFile = await _picker.pickImage(
            source: ImageSource.gallery,
          );
          if (pickedFile != null) {
    
            musicCangable = true;
    
    Ede Dust's avatar
    Ede Dust committed
            imagePath = pickedFile.path;
            setState(() {
    
              usesPicture = true;
              currentMoodsImage = File(imagePath);
    
    Ede Dust's avatar
    Ede Dust committed
            });
          }
        } catch (e) {
          print("could not load image: ");
          print(e);
        }
      }
    
    MagicalSpyto's avatar
    MagicalSpyto committed
      //sendet den befehl music zum passenden genre rauszusuchen
    
    MagicalSpyto's avatar
    MagicalSpyto committed
      _changeMusic() async {
    
        Spotify spotify = Spotify();
    
        //code zum musik ändern
    
    MagicalSpyto's avatar
    MagicalSpyto committed
        String hexcode = selectedMood.red.toRadixString(16) + selectedMood.green.toRadixString(16) + selectedMood.blue.toRadixString(16);
    
    MagicalSpyto's avatar
    MagicalSpyto committed
        print(hexcode);
    
    MagicalSpyto's avatar
    MagicalSpyto committed
        String genre = await changeFirebase().getGenreByHex(hexcode);
        print("genre = " + genre);
    
        spotify.playGenre(genre);
    
    MagicalSpyto's avatar
    MagicalSpyto committed
      //vergleicht zwei farben auf ähnlichkeit
    
    Ede Dust's avatar
    Ede Dust committed
      _getColorDistance(Color c1, Color c2) {
    
        LabColor labC1 = LabColor.fromColor(c1);
        LabColor labC2 = LabColor.fromColor(c2);
    
    
    Ede Dust's avatar
    Ede Dust committed
        double distance = 0;
    
    
        print("Lightness bild: " + labC1.lightness.toDouble().toString());
        print("Lightness referenz: " + labC2.lightness.toDouble().toString());
    
        Vector3 v1 = Vector3(labC1.lightness.toDouble(), labC1.a.toDouble(), labC1.b.toDouble());
        Vector3 v2 = Vector3(labC2.lightness.toDouble(), labC2.a.toDouble(), labC2.b.toDouble());
        distance = v1.distanceTo(v2).toDouble();
    
    Ede Dust's avatar
    Ede Dust committed
        return distance;
      }
    
      _determineMoodToMatch(Color col) {
    
        double distance = 1000000.0;
    
    Ede Dust's avatar
    Ede Dust committed
        Moods newMood = Moods.none;
    
        if(musicCangable){
          for (var value in Moods.values) {
            double colorDistance = _getColorDistance(col, value.color);
            if (colorDistance < distance &&
                value != Moods.none) {
              distance = colorDistance;
              newMood = value;
            }
          }
          _updateColorPalete(col);
    
    MagicalSpyto's avatar
    MagicalSpyto committed
          selectedMoodAsEnum = newMood;
    
          selectedMood = newMood.color;
          _changeMusic();
          musicCangable = false;
          return newMood;
        }else{
          for (var value in Moods.values) {
            double colorDistance = _getColorDistance(selectedMood, value.color);
            if (colorDistance < distance &&
                value != Moods.none) {
              distance = colorDistance;
              newMood = value;
            }
    
    Ede Dust's avatar
    Ede Dust committed
          }
    
          musicCangable = false;
          return newMood;
    
    Ede Dust's avatar
    Ede Dust committed
        }
    
      }
      _updateColorPalete(Color col){
        if(!usesPicture){
    
    MagicalSpyto's avatar
    MagicalSpyto committed
          if(col.value != Color.fromARGB(255, 0, 0, 0).value){
            col = ligten(col, 0.2);
          }
    
          primaryColor = col;
        }
    
    MagicalSpyto's avatar
    MagicalSpyto committed
        backGroundColor = Color.fromARGB(col.alpha - 230, col.red, col.green, col.blue);
    
        primaryColor = col;
    
    MagicalSpyto's avatar
    MagicalSpyto committed
        onPrimary = darken(col, 0.5);
    
    MagicalSpyto's avatar
    MagicalSpyto committed
        Color shiftedColor = ligten(col,0.1);;
    
    
        primaryContainer = shiftedColor;
    
    MagicalSpyto's avatar
    MagicalSpyto committed
        onPrimaryContainer = darken(shiftedColor, 0.5);
    
    Ede Dust's avatar
    Ede Dust committed
      }
    
    MagicalSpyto's avatar
    MagicalSpyto committed
      //returend die liste mit den moods
    
      _getMoodList(){
    
    Ede Dust's avatar
    Ede Dust committed
        return Column(children: <Widget>[
    
    
              RadioListTile<Moods>(
                title: const Text(
                  "Sad",
                  style: TextStyle(
                      color: Color.fromARGB(255, 0, 0, 0),
                  ),
                ),
                tileColor: backGroundColor,
                activeColor: primaryColor,
                value: Moods.sad,
                groupValue: currentMood,
                onChanged: (Moods? value) {
                  setState(() {
                    usesPicture = false;
    
    MagicalSpyto's avatar
    MagicalSpyto committed
                    musicCangable = true;
    
                    primaryColor = Moods.sad.color;
                    _updateColorPalete(primaryColor);
                    currentMood = value;
                  });
                },
              ),
              RadioListTile<Moods>(
                title: const Text(
                  "Angry",
                  style: TextStyle(
                      color: Color.fromARGB(255, 0, 0, 0),
                  ),
                ),
                tileColor: backGroundColor,
                activeColor: primaryColor,
                value: Moods.angry,
                groupValue: currentMood,
                onChanged: (Moods? value) {
                  setState(() {
                    usesPicture = false;
    
    MagicalSpyto's avatar
    MagicalSpyto committed
                    musicCangable = true;
    
                    primaryColor = Moods.angry.color;
                    _updateColorPalete(primaryColor);
                    currentMood = value;
                  });
                },
              ),
              RadioListTile<Moods>(
                title: const Text(
                  "Noble",
                  style: TextStyle(
                      color: Color.fromARGB(255, 0, 0, 0),
                  ),
                ),
                tileColor: backGroundColor,
                activeColor: primaryColor,
                value: Moods.noble,
                groupValue: currentMood,
                onChanged: (Moods? value) {
                  setState(() {
                    usesPicture = false;
    
    MagicalSpyto's avatar
    MagicalSpyto committed
                    musicCangable = true;
    
                    primaryColor = Moods.noble.color;
                    _updateColorPalete(primaryColor);
                    currentMood = value;
                  });
                },
              ),
              RadioListTile<Moods>(
                title: const Text(
                  "Clean",
                  style: TextStyle(
                      color: Color.fromARGB(255, 0, 0, 0),
                  ),
                ),
                tileColor: backGroundColor,
                activeColor: primaryColor,
                value: Moods.clean,
                groupValue: currentMood,
                onChanged: (Moods? value) {
                  setState(() {
                    usesPicture = false;
    
    MagicalSpyto's avatar
    MagicalSpyto committed
                    musicCangable = true;
    
                    primaryColor = Moods.clean.color;
                    _updateColorPalete(primaryColor);
                    currentMood = value;
                  });
                },
              ),
    
    MagicalSpyto's avatar
    MagicalSpyto committed
    
    
    Ede Dust's avatar
    Ede Dust committed
          RadioListTile<Moods>(
    
            title: const Text(
              "Happy",
              style: TextStyle(
                color: Color.fromARGB(255, 0, 0, 0),
              ),
            ),
            tileColor: backGroundColor,
            activeColor: primaryColor,
    
    Ede Dust's avatar
    Ede Dust committed
            value: Moods.happy,
            groupValue: currentMood,
            onChanged: (Moods? value) {
              setState(() {
    
                usesPicture = false;
    
    MagicalSpyto's avatar
    MagicalSpyto committed
                musicCangable = true;
    
                primaryColor = Moods.happy.color;
                _updateColorPalete(primaryColor);
    
    Ede Dust's avatar
    Ede Dust committed
                currentMood = value;
              });
            },
          ),
    
              RadioListTile<Moods>(
                title: const Text(
                  "Courageous",
                  style: TextStyle(
                      color: Color.fromARGB(255, 0, 0, 0),
                  ),
                ),
                tileColor: backGroundColor,
                activeColor: primaryColor,
                value: Moods.courageous,
                groupValue: currentMood,
                onChanged: (Moods? value) {
                  setState(() {
                    usesPicture = false;
    
    MagicalSpyto's avatar
    MagicalSpyto committed
                    musicCangable = true;
    
                    primaryColor = Moods.courageous.color;
                    _updateColorPalete(primaryColor);
                    currentMood = value;
                  });
                },
              ),
              RadioListTile<Moods>(
                title: const Text(
                  "Fresh",
                  style: TextStyle(
                      color: Color.fromARGB(255, 0, 0, 0),
                  ),
                ),
                tileColor: backGroundColor,
                activeColor: primaryColor,
                value: Moods.fresh,
                groupValue: currentMood,
                onChanged: (Moods? value) {
                  setState(() {
                    usesPicture = false;
    
    MagicalSpyto's avatar
    MagicalSpyto committed
                    musicCangable = true;
    
                    primaryColor = Moods.fresh.color;
                    _updateColorPalete(primaryColor);
                    currentMood = value;
                  });
                },
              ),
              RadioListTile<Moods>(
                title: const Text(
                  "Compassionn",
                  style: TextStyle(
                      color: Color.fromARGB(255, 0, 0, 0),
                  ),
                ),
                tileColor: backGroundColor,
                activeColor: primaryColor,
                value: Moods.compassionn,
                groupValue: currentMood,
                onChanged: (Moods? value) {
                  setState(() {
                    usesPicture = false;
    
    MagicalSpyto's avatar
    MagicalSpyto committed
                    musicCangable = true;
    
                    primaryColor = Moods.compassionn.color;
                    _updateColorPalete(primaryColor);
                    currentMood = value;
                  });
                },
              ),
              RadioListTile<Moods>(
                title: const Text(
                  "Simple",
                  style: TextStyle(
                      color: Color.fromARGB(255, 0, 0, 0),
                  ),
                ),
                tileColor: backGroundColor,
                activeColor: primaryColor,
                value: Moods.simple,
                groupValue: currentMood,
                onChanged: (Moods? value) {
                  setState(() {
                    usesPicture = false;
    
    MagicalSpyto's avatar
    MagicalSpyto committed
                    musicCangable = true;
    
                    primaryColor = Moods.simple.color;
                    _updateColorPalete(primaryColor);
                    currentMood = value;
                  });
                },
              ),
              RadioListTile<Moods>(
                title: const Text(
                  "Dramatic",
                  style: TextStyle(
                      color: Color.fromARGB(255, 0, 0, 0),
                  ),
                ),
                tileColor: backGroundColor,
                activeColor: primaryColor,
                value: Moods.dramatic,
                groupValue: currentMood,
                onChanged: (Moods? value) {
                  setState(() {
                    usesPicture = false;
    
    MagicalSpyto's avatar
    MagicalSpyto committed
                    musicCangable = true;
    
                    primaryColor = Moods.dramatic.color;
                    _updateColorPalete(primaryColor);
                    currentMood = value;
                  });
                },
              ),
            ]);
    
    Ede Dust's avatar
    Ede Dust committed
      }
    
    MagicalSpyto's avatar
    MagicalSpyto committed
      // setzt das bild(falls vorhanden) in einem container ein der sich and die bildschirmgröße anpasst
    
    Ede Dust's avatar
    Ede Dust committed
      _getContainerTodisplay() {
        BoxDecoration deco = new BoxDecoration();
        var image;
        image = _getImageTodisplay();
    
        if (usesPicture) {
    
    Ede Dust's avatar
    Ede Dust committed
          deco = BoxDecoration(
            image: DecorationImage(
              fit: BoxFit.fitWidth,
              image: image,
            ),
          );
        } else {
    
          deco = BoxDecoration(
            color: primaryColor,
    
    Ede Dust's avatar
    Ede Dust committed
          );
        }
    
        return GestureDetector(
          onDoubleTap: (){
            _getFromGallery();
          },
          child: Container(
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.width * (9.0 / 16.0),
            decoration: deco,
          ),
    
    Ede Dust's avatar
    Ede Dust committed
        );
      }
    
    MagicalSpyto's avatar
    MagicalSpyto committed
      //generiert ein image aus dem imagefile
    
    Ede Dust's avatar
    Ede Dust committed
      _getImageTodisplay() {
        var imageToShow;
        try {
    
          imageToShow = FileImage(currentMoodsImage);
    
    Ede Dust's avatar
    Ede Dust committed
          return imageToShow;
        } catch (e) {
          return null;
        }
      }
    
    MagicalSpyto's avatar
    MagicalSpyto committed
      // generiert einen paletegenerator vom aktiellen image
    
    Ede Dust's avatar
    Ede Dust committed
      Future<PaletteGenerator> _updatePaletteGenerator() async {
        if (imagePath != "") {
          paletteGenerator = await PaletteGenerator.fromImageProvider(
            _getImageTodisplay(),
          );
        }
        return paletteGenerator;
      }
    
    MagicalSpyto's avatar
    MagicalSpyto committed
      //gibt dem widged den colorindicator zur momentan generierten Primärfarbe und die Moodliste mit dem aktuell auszuwählenden Mood zurück
      _getCollorIndicatorAndMoodList() {
    
    Ede Dust's avatar
    Ede Dust committed
        return FutureBuilder<PaletteGenerator>(
            future: _updatePaletteGenerator(), // async work
            builder:
                (BuildContext context, AsyncSnapshot<PaletteGenerator> snapshot) {
              switch (snapshot.connectionState) {
                case ConnectionState.waiting:
    
    Ede Dust's avatar
    Ede Dust committed
                  return const Center(child: CircularProgressIndicator());
    
    Ede Dust's avatar
    Ede Dust committed
                default:
    
                  if (snapshot.hasError && usesPicture) {
                    currentMood = _determineMoodToMatch(primaryColor);
                    return Column(
                        children: <Widget>[
                          PaletteSquare( color: primaryColor),
                          _getMoodList(),
                        ]);
                  } else if(snapshot.hasError) {
                    return Column(
                        children: <Widget>[
                          PaletteSquare( color: primaryColor),
                          _getMoodList(),
                        ]);
                  } else if (usesPicture){
                    Color? colorToUse = snapshot.data!.vibrantColor?.color;
                    colorToUse ??= snapshot.data!.dominantColor?.color;
                    currentMood = _determineMoodToMatch(colorToUse!);
                    return Column(
                      children: <Widget>[
                        PaletteSquare(color: colorToUse),
                        _getMoodList(),
                      ]);
                  }else{
                    return Column(
                        children: <Widget>[
                          PaletteSquare(color: primaryColor),
                          _getMoodList(),
                        ]);
    
    Ede Dust's avatar
    Ede Dust committed
                  }
              }
            });
      }
    
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
    
    Ede Dust's avatar
    Ede Dust committed
          body: Center(
    
            child: SingleChildScrollView(
              child: Column(
                children: <Widget>[
                  _getContainerTodisplay(),
    
    MagicalSpyto's avatar
    MagicalSpyto committed
                  _getCollorIndicatorAndMoodList(),
    
                  //_getMoodList(),
                ],
              ),
    
    Ede Dust's avatar
    Ede Dust committed
            ),
          ),
        );
      }
    }
    
    @immutable
    class PaletteSquare extends StatelessWidget {
      const PaletteSquare({
        required this.color,
        super.key,
      });
    
      final Color color;
    
    Ede Dust's avatar
    Ede Dust committed
      @override
      Widget build(BuildContext context) {
        Widget square = Container(
          decoration: BoxDecoration(
              color: color, border: Border.all(width: 1.0, color: color)),
    
          width: MediaQuery.of(context).size.width,
    
    Ede Dust's avatar
    Ede Dust committed
          height: 40,
    
    Ede Dust's avatar
    Ede Dust committed
        return square;