0

我无法用代码中的onAccept()之类的操作/触发器替换 carousel_slider 中的一组图像

我目前使用可拖动的滑块,我的目标是当我通过onAccept将图像拖动到dragTarget时,我想更改图像集

我怎样才能做到这一点?感谢您的帮助

这是 carousel_slider 代码:

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,

  ),
);

这是滑块的实现:

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,
                          ),
                        ),
                );
              },
            )),

这段代码似乎不像我想的那样工作

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

感谢您的关注,非常感谢您的帮助

4

0 回答 0