Skip to content

how to save color when moving pages flutter ? #50643

Closed

Description

@rakafajar

how to make _isFavorite when you press and move pages, the right button turns red ??

I have a little trouble when maintaining the color of the button when switching pages

bool _isFavorite = false;

Method Toogle

  void _cekAuthToFavorite() {
    setState(() {
      if (isLogin) {
        if (_isFavorite) {
          _isFavorite = false;
          wishlistBloc.add(
            DeleteWishlistFromBag(codeProduct: widget.products.code),
          );
        } else {
          _isFavorite = true;
          wishlistBloc.add(
            AddWishlistToBag(codeProduct: widget.products.code),
          );
        }
      } else {
        _showBottomSheetLogin();
      }
    });
  }

UI Onpress

IconButton(
          icon: (_isFavorite
              ? Icon(
                  Icons.favorite,
                  color: Colors.red,
                )
              : Icon(
                  Icons.favorite_border,
                  color: ThemeApp.primaryColor,
                )),
          onPressed: () async {
            _cekAuthToFavorite();
            // _toogleFavorite();
          },
        ),

Model

import 'dart:convert';

import 'package:huntstreet/model/product/product.dart';

HomeWishlist wishlistFromJson(String str) =>
    HomeWishlist.fromJson(json.decode(str));

String wishlistToJson(HomeWishlist data) => json.encode(data.toJson());

class HomeWishlist {
  final Data data;

  HomeWishlist({
    this.data,
  });

  factory HomeWishlist.fromJson(Map<String, dynamic> json) => HomeWishlist(
        data: Data.fromJson(json["data"]),
      );

  Map<String, dynamic> toJson() => {
        "data": data.toJson(),
      };
}

class Data {
  final Meta meta;
  final List<Product> products;

  Data({
    this.meta,
    this.products,
  });

  factory Data.fromJson(Map<String, dynamic> json) => Data(
        meta: Meta.fromJson(json["meta"]),
        products: List<Product>.from(
            json["products"].map((x) => Product.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "meta": meta.toJson(),
        "products": List<dynamic>.from(products.map((x) => x.toJson())),
      };
}

class Meta {
  final int total;
  final int lastPage;
  final int perPage;
  final int currentPage;
  Product product;

  Meta({
    this.total,
    this.lastPage,
    this.perPage,
    this.currentPage,
  });

  factory Meta.fromJson(Map<String, dynamic> json) => Meta(
        total: json["total"],
        lastPage: json["last_page"],
        perPage: json["per_page"],
        currentPage: json["current_page"],
      );

  Map<String, dynamic> toJson() => {
        "total": total,
        "last_page": lastPage,
        "per_page": perPage,
        "current_page": currentPage,
      };
}

class Condition {
  final String gradeCode;
  final String name;
  final String description;

  Condition({
    this.gradeCode,
    this.name,
    this.description,
  });

  factory Condition.fromJson(Map<String, dynamic> json) => Condition(
        gradeCode: json["grade_code"],
        name: json["name"],
        description: json["description"],
      );

  Map<String, dynamic> toJson() => {
        "grade_code": gradeCode,
        "name": name,
        "description": description,
      };
}

class Designer {
  final String code;
  final String name;
  final dynamic description;
  final bool isActive;

  Designer({
    this.code,
    this.name,
    this.description,
    this.isActive,
  });

  factory Designer.fromJson(Map<String, dynamic> json) => Designer(
        code: json["code"],
        name: json["name"],
        description: json["description"],
        isActive: json["is_active"],
      );

  Map<String, dynamic> toJson() => {
        "code": code,
        "name": name,
        "description": description,
        "is_active": isActive,
      };
}

class DisplayImage {
  final bool isPrimary;
  final Url url;

  DisplayImage({
    this.isPrimary,
    this.url,
  });

  factory DisplayImage.fromJson(Map<String, dynamic> json) => DisplayImage(
        isPrimary: json["is_primary"],
        url: Url.fromJson(json["url"]),
      );

  Map<String, dynamic> toJson() => {
        "is_primary": isPrimary,
        "url": url.toJson(),
      };
}

class Url {
  final String smallThumb;
  final String thumb;
  final String medium;
  final String large;
  final String original;

  Url({
    this.smallThumb,
    this.thumb,
    this.medium,
    this.large,
    this.original,
  });

  factory Url.fromJson(Map<String, dynamic> json) => Url(
        smallThumb: json["small_thumb"],
        thumb: json["thumb"],
        medium: json["medium"],
        large: json["large"],
        original: json["original"] == null ? null : json["original"],
      );

  Map<String, dynamic> toJson() => {
        "small_thumb": smallThumb,
        "thumb": thumb,
        "medium": medium,
        "large": large,
        "original": original == null ? null : original,
      };
}

class ProductColorDetail {
  final ColorDetail colorDetail;

  ProductColorDetail({
    this.colorDetail,
  });

  factory ProductColorDetail.fromJson(Map<String, dynamic> json) =>
      ProductColorDetail(
        colorDetail: ColorDetail.fromJson(json["color_detail"]),
      );

  Map<String, dynamic> toJson() => {
        "color_detail": colorDetail.toJson(),
      };
}

class ColorDetail {
  final String name;
  final String hex;
  final bool isActive;

  ColorDetail({
    this.name,
    this.hex,
    this.isActive,
  });

  factory ColorDetail.fromJson(Map<String, dynamic> json) => ColorDetail(
        name: json["name"],
        hex: json["hex"],
        isActive: json["is_active"],
      );

  Map<String, dynamic> toJson() => {
        "name": name,
        "hex": hex,
        "is_active": isActive,
      };
}

class ProductType {
  final int id;
  final String code;
  final String name;
  final String urlKey;
  final bool isActive;

  ProductType({
    this.id,
    this.code,
    this.name,
    this.urlKey,
    this.isActive,
  });

  factory ProductType.fromJson(Map<String, dynamic> json) => ProductType(
        id: json["id"],
        code: json["code"],
        name: json["name"],
        urlKey: json["url_key"],
        isActive: json["is_active"],
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "code": code,
        "name": name,
        "url_key": urlKey,
        "is_active": isActive,
      };
}

class SizeClass {
  final String value;
  final bool isActive;
  final SizeGroup sizeGroup;
  final TypeSize typeSize;

  SizeClass({
    this.value,
    this.isActive,
    this.sizeGroup,
    this.typeSize,
  });

  factory SizeClass.fromJson(Map<String, dynamic> json) => SizeClass(
        value: json["value"],
        isActive: json["is_active"],
        sizeGroup: SizeGroup.fromJson(json["size_group"]),
        typeSize: TypeSize.fromJson(json["type_size"]),
      );

  Map<String, dynamic> toJson() => {
        "value": value,
        "is_active": isActive,
        "size_group": sizeGroup.toJson(),
        "type_size": typeSize.toJson(),
      };
}

class SizeGroup {
  final String name;
  final bool isActive;

  SizeGroup({
    this.name,
    this.isActive,
  });

  factory SizeGroup.fromJson(Map<String, dynamic> json) => SizeGroup(
        name: json["name"],
        isActive: json["is_active"],
      );

  Map<String, dynamic> toJson() => {
        "name": name,
        "is_active": isActive,
      };
}

class TypeSize {
  final String value;
  final bool isActive;

  TypeSize({
    this.value,
    this.isActive,
  });

  factory TypeSize.fromJson(Map<String, dynamic> json) => TypeSize(
        value: json["value"],
        isActive: json["is_active"],
      );

  Map<String, dynamic> toJson() => {
        "value": value,
        "is_active": isActive,
      };
}

class Slug {
  final String url;

  Slug({
    this.url,
  });

  factory Slug.fromJson(Map<String, dynamic> json) => Slug(
        url: json["url"],
      );

  Map<String, dynamic> toJson() => {
        "url": url,
      };
}

Activity

  1. jonahwilliams commented on Feb 12, 2020

    @jonahwilliams
    Contributor

    Questions like these are better suited for Q&A sites like StackOverflow or on one of the flutter community sites at https://flutter.dev/community

  2. github-actions commented on Aug 7, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  3. locked as resolved and limited conversation to collaborators on Aug 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions