0

I'm having trouble replacing a set of images in the carousel_slider with an action / trigger like onAccept() in my code

I currently using a draggable the slider, and my goal is I want to change the set of images when I'm dragging the image into the dragTarget by onAccept

how can I achieve that? I appreciate for your help

this is the carousel_slider code:

CarouselSlider _pilihanMakanan = CarouselSlider( 
items: _listMakanan.map((makanan) {
return Row(children: [
  Expanded(
    flex: 1,
    child: LongPressDraggable<Makanan>(
      data: makanan,
      child: Container(
        margin: EdgeInsets.symmetric(horizontal: 2),
        child: Image(
          image: AssetImage(makanan.image),
          fit: BoxFit.contain,
          // width: 50,
        ),
      ),
      feedback: Tooltip(
        message: makanan.nama,
        child: Image(
          image: AssetImage(makanan.image),
          fit: BoxFit.contain,
          width: 100,
        ),
      ),
      childWhenDragging: Container(),
    ),
  ),
]);
}).toList(),
 options: CarouselOptions(
 height: 70,
 autoPlay: false,
 enlargeCenterPage: true,
 enableInfiniteScroll: false,
 viewportFraction: 0.15,

  ),
);

and this is the slider implementation :

Container(
            margin: EdgeInsets.fromLTRB(0, 45, 119, 19),
            alignment: Alignment.center,
            child: DragTarget<Makanan>(
              onAccept: (makanan) {
                setState(() {
                  _pokok = makanan.image;
                  _targetImageUrl = makanan.image;
                  _totalKalori += makanan.kalori;
                  _listMakanan = _listLauk;  <--- this code seems not working
                });
              },
              builder: (simulation, candidateData, rejectedData) {
                return Container(
                  alignment: Alignment.center,
                  child: _targetImageUrl != ''
                      ? Image(
                          image: AssetImage(_targetImageUrl),
                          width: 50,
                        )
                      : Container(
                          child: Image(
                            image: AssetImage("assets/images/pokok.png"),
                            width: 120,
                          ),
                        ),
                );
              },
            )),

this code seems doesn't work as i thought

onAccept: (makanan) { setState(() { _listMakanan = _listLauk; });

Thank you for your attention, I really appreciate for your help

  • Judging by line 1 of your code, `CarouselSlider _pilihanMakanan = CarouselSlider(`, you are building your CarouselSlider outside of your `build` function and then inserting `_pilihanMakanan` into your widget tree. The result is that it won't get rebuilt on `setState()`. You need to replace `_pilihanMakanan` in your `build` function with the CarouselSlider code itself, that way it will get rebuilt along with your other widgets. – PatrickMahomes Jul 19 '21 at 08:56
  • wow it is working well, thank you so much – Rio Ramadhan Jul 19 '21 at 12:29

0 Answers0