วิธีที่จะปรับปรุงสูงปุ่มหัวเรื่องบนได้กดดันโดยเฉพาะอย่างยิ่งดัชนีของ ListView.สร้าง

0

คำถาม

นี่คือตอบสนองรหัสผม

bool isAddedToCart = false;
return ListView.builder(
........
 ElevatedButton (
                        child: isAddedToCart? Text('Added to cart') : Text('Add to cart'),
                         style: ElevatedButton.styleFrom(
                           primary: Constants.primaryColor,
                           onPrimary: Constants.appColor
                         ),
                         onPressed: () async{
                           setState(() {
                             isAddedToCart = !isAddedToCart;
                           });
                           
                         },
                      
                       ),

ปัญหาก็คือ,ถ้าฉันคลิกบนที่สูงปุ่มข้อความของปุ่มจะต้องเปลี่ยนอยู่ที่ดัชนีเท่านั้น แต่มันเป็นการเปลี่ยนในสิ่งดัชนีซึ่งอยู่ใน listview.งานก่อสร้างได้

เรื่องหนึ่งมีทางออกเรื่องนี้นั่นเดียวที่ปุ่มเลือกดัชนีจะต้องปรับปรุงกับเปลี่ยนชื่อ

flutter
2021-11-24 05:13:26
2

คำตอบที่ดีที่สุด

2

คุณต้องการให้เก็บธง isAddedToCart สำหรับแต่ละดัชนี คุณสามารถประสบความสำเร็จในส่วมันโดยใช้ Map. บางอย่างเหมือนนี้:

// class variable scope.
Map<int, bool> isAddedToCartMap = {};

จากนั้นใช้มันในของวิดเจ็ต:

ElevatedButton (
    // if isAddedToCartMap[index] not found, use false as default value.
    child: isAddedToCartMap[index]??false ? Text('Added to cart') : Text('Add to cart'),
     style: ElevatedButton.styleFrom(
       primary: Constants.primaryColor,
       onPrimary: Constants.appColor
     ),
     onPressed: () async{
       setState(() {
         isAddedToCartMap[index] = !isAddedToCartMap[index]??false;
       });
       
     },
  
   ),
2021-11-24 05:24:43

มันทำงานสบายดีฉันมี dout..วิธีที่จะปรับปรุงเดียวกันเพื่อให้'เพิ่มรถ'อีกครั้ง ขอบคุณสำหรับตอบกลับ@ישו אוהב אותך
H ă ɤ í
1

รายการทั้งหมดก็ขึ้นอยู่กับ isAddedToCart แต่คุณต้องการจะจัดเก็บรายการที่เลือกกับแยกออกกำลังทำดัชนี

List<int> _selected_item = List();

 ElevatedButton(
              child: _selected_item.contains(index)
                  ? Text('Added to cart')
                  : Text('Add to cart'),
              style: ElevatedButton.styleFrom(),
              onPressed: () async {
                setState(() {
                  // remove or add index to _selected_item
                  if (_selected_item.contains(index))
                    _selected_item.remove(index);
                  else
                    _selected_item.add(index);
                  print(index);
                });
              },
            )

สมบูรณ์ด้วยรหัสต้นทางของ

ListView.builder(
          itemCount: 5,
          itemBuilder: (context, index) {
            return ElevatedButton(
              child: _selected_item.contains(index)
                  ? Text('Added to cart')
                  : Text('Add to cart'),
              style: ElevatedButton.styleFrom(),
              onPressed: () async {
                setState(() {
                  // remove or add index to _selected_item
                  if (_selected_item.contains(index))
                    _selected_item.remove(index);
                  else
                    _selected_item.add(index);
                  print(index);
                });
              },
            );
          })
2021-11-24 06:01:37

@GH คุณสามารถลองนี้ฉันหวังว่ามันจะทำงานให้คุณ
Jahidul Islam

นั่นเป็นซุปเปอร์ thankyou.
H ă ɤ í

มันกำลังได้รับค่าปริยายเมื่อมันเป็นการเรียกใหม่. เราสามารถทำมันโดยใช้ตัวอย่างถ้าใช่มันเกิดขึ้นได้ยังไงกัน?
H ă ɤ í

คุณควรจะเปิดดูตำแหน่งเชื่อมโยงนี้ stackoverflow.com/questions/57380673/...
Jahidul Islam

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่