Flutter: AnimatedList удалить статичный виджет

Я пытаюсь удалить элемент из AnimatedList

предмет о StatfulWidgt

но когда я пытаюсь удалить его

SizeTransition не работает в нем

хотя я поставил для него уникальный ключ

так может кто-нибудь сказать мне, в чем проблема?

Список

  Widget recipes(List<MadeRecipe> list, MyDatabase db) {
    return Container(
      child: AnimatedList(
        key: listKey,
        initialItemCount: list == null ? 0 : list.length,
        padding: EdgeInsets.all(10),
        itemBuilder: (context, position, animation) {
          Reciepie rec = Reciepie(
              date: DateTime.now(),
              id: list[position].id,
              isFavourite: false,
              category: list[position].category,
              desc: list[position].desc,
              image: list[position].image,
              isMade: true,
              link: list[position].link,
              name: list[position].name,
              notes: list[position].notes,
              photo: list[position].photo,
              quanities: list[position].quanities,
              slug: list[position].slug,
              steps: list[position].steps);

          return _itemBuilder(
            key: UniqueKey(),
            animation: animation,
            context: context,
            db: db,
            list: list,
            position: position,
            rec: rec,
          );
        },
      ),
    );
  }

и _itemBuilder это

  Widget _itemBuilder(
      {@required BuildContext context,
      @required Animation animation,
      @required Reciepie rec,
      @required MyDatabase db,
      @required int position,
      @required List list,
      @required Key key,
      bool deleteParams = false}) {
    return SizeTransition(
      sizeFactor: animation,
      axis: Axis.horizontal,
      key: key,
      child: GestureDetector(
        onTap: deleteParams == true
            ? null
            : () {
                Navigator.push(
                    context,
                    PageTransition(
                        type: PageTransitionType.downToUp,
                        child: RecipePage(
                          recipe: rec,
                        )));
              },
        child: MadeItem(
          onTap: () {
            listKey.currentState.removeItem(position, (context, animation) {
              return _itemBuilder(
                  key: key,
                  animation: animation,
                  context: context,
                  db: db,
                  list: list,
                  position: position,
                  rec: rec,
                  deleteParams: true);
            });
            // db.removeMadeRecipe(madeRecipe: list[position]);
          },
          recipe: rec,
          db: db,
        ),
      ),
    );
  }

и MadeItem это

class MadeItem extends StatefulWidget {
  final Reciepie recipe;
  final MyDatabase db;
  final Function onTap;
  MadeItem({@required this.recipe, @required this.db, @required this.onTap , });
  _MadeItemState createState() => _MadeItemState();
}

class _MadeItemState extends State<MadeItem> {
  bool isFavourite;
  bool isMade;
  @override
  void initState() {
    isFavourite = false;
    isMade = true;
    super.initState();
    getFavourite();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 150,
      child: Card(
        color: Theme.of(context).accentColor,
        elevation: 4,
        clipBehavior: Clip.antiAliasWithSaveLayer,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(20),
        ),
        child: Row(
          children: <Widget>[
            Expanded(
              flex: 1,
              child: getTitleColumn(),
            ),
            cardImage()
          ],
        ),
      ),
    );
  }

  Widget getTitleColumn() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Padding(
          padding: const EdgeInsets.only(top: 8, right: 4, left: 4),
          child: Text(
            widget.recipe.name,
            overflow: TextOverflow.ellipsis,
            textAlign: TextAlign.start,
            style: TextStyle(
              color: Colors.white,
              fontSize: 15,
            ),
            maxLines: 2,
          ),
        ),
        Expanded(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 4),
            child: Text(
              widget.recipe.desc,
              overflow: TextOverflow.ellipsis,
              textAlign: TextAlign.start,
              style: TextStyle(
                letterSpacing: .2,
                color: Colors.white,
                fontSize: 12,
              ),
              maxLines: 2,
            ),
          ),
        ),
        getChoicesRow()
      ],
    );
  }

  Widget getChoicesRow() {
    return Builder(
      builder: (context) => Container(
        decoration: BoxDecoration(
          color: Color(0xFFD14567),
        ),
        child: Row(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            Expanded(
              child: singleChoice(
                  isTrue: isMade,
                  text: "جربتها",
                  icon: Icons.check,
                  onTap: widget.onTap),
            ),
            Expanded(
              child: singleChoice(
                  text: "تقييم",
                  icon: Icons.star,
                  onTap: () {
                    rateRecipe(context);
                  }),
            ),
            Expanded(
              child: singleChoice(
                  isTrue: isFavourite,
                  text: "تفضيل",
                  icon: Icons.favorite,
                  onTap: () {
                    favouriteRecipe();
                  }),
            ),
          ],
        ),
      ),
    );
  }

  Widget singleChoice(
      {bool isTrue = false, String text, IconData icon, Function onTap}) {
    return Material(
      type: MaterialType.transparency,
      child: InkWell(
        splashColor: Colors.yellow.withAlpha(130),
        borderRadius: BorderRadius.all(Radius.circular(25)),
        onTap: onTap,
        child: Padding(
          padding: const EdgeInsets.only(top: 10.0),
          child: Column(
            children: <Widget>[
              Icon(
                icon,
                color: isTrue ? Colors.yellow : Colors.black,
                size: 25,
              ),
              Text(
                text,
                style: TextStyle(color: Colors.white, fontSize: 10),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget cardImage() {
    return Expanded(
      flex: 1,
      child: Container(
        child: "${kImageURL}/${widget.recipe.photo}".contains("null")
            ? Icon(
                Icons.error,
                color: Colors.white,
                size: 30,
              )
            : CachedNetworkImage(
                fit: BoxFit.cover,
                height: double.infinity,
                width: double.infinity,
                imageUrl: "${kImageURL}/${widget.recipe.photo}",
                placeholder: (context, url) => SpinKitDualRing(
                  color: Colors.white,
                  size: 50,
                ),
                errorWidget: (context, url, error) => Icon(
                  Icons.error,
                  color: Colors.white,
                  size: 50,
                ),
              ),
      ),
    );
  }

  iTriedIt() {
    MadeRecipe made = MadeRecipe(
        date: DateTime.now(),
        id: widget.recipe.id,
        isFavourite: widget.recipe.isFavourite,
        category: widget.recipe.category,
        desc: widget.recipe.desc,
        image: widget.recipe.image,
        isMade: true,
        link: widget.recipe.link,
        name: widget.recipe.name,
        notes: widget.recipe.notes,
        photo: widget.recipe.photo,
        quanities: widget.recipe.quanities,
        slug: widget.recipe.slug,
        steps: widget.recipe.steps);

    if (isMade == false) {
      widget.db.insertMadeRecipe(madeRecipe: made).then((onValue) {
        setState(() {
          isMade = made.isMade;
        });
      });
    } else {
      widget.db.removeMadeRecipe(madeRecipe: made).then((onValue) {
        setState(() {
          isMade = false;
        });
      });
    }
  }

  rateRecipe(BuildContext context) {
    Scaffold.of(context).showSnackBar(SnackBar(
      content: Text("قريبا"),
    ));
  }

  favouriteRecipe() {
    Favourit fav = Favourit(
        date: DateTime.now(),
        id: widget.recipe.id,
        isFavourite: true,
        category: widget.recipe.category,
        desc: widget.recipe.desc,
        image: widget.recipe.image,
        isMade: true,
        link: widget.recipe.link,
        name: widget.recipe.name,
        notes: widget.recipe.notes,
        photo: widget.recipe.photo,
        quanities: widget.recipe.quanities,
        slug: widget.recipe.slug,
        steps: widget.recipe.steps);

    if (isFavourite == false) {
      widget.db.insertFavouriteRecipe(favourite: fav).then((onValue) {
        setState(() {
          isFavourite = fav.isFavourite;
        });
      });
    } else {
      widget.db.removeFavouriteRecipe(favourit: fav).then((onValue) {
        setState(() {
          isFavourite = false;
        });
      });
    }
  }

  void getFavourite() {
    widget.db.getFavouriteById(widget.recipe.id).then((Favourit fav) {
      if (fav != null) {
        setState(() {
          isFavourite = true;
        });
      } else {
        setState(() {
          isFavourite = false;
        });
      }
    });
  }
}

извините, я знаю, что мой код не чистый, но я все еще начинающий:D спасибо

0 ответов

Другие вопросы по тегам