ListView.Builder play / Pause buttom flutter

Aug 19 2020

ฉันใช้ ListView.builder เพื่อสร้างรายการเสียงฉันต้องการเปลี่ยนปุ่มเล่นหยุดชั่วคราวเป็นรายการเมื่อฉันเลือกฉันได้ลองบูล แต่การให้เล่นหรือหยุดชั่วคราวจะเปลี่ยนรายการทั้งหมดในรายการใครบางคนสามารถ ช่วยด้วย ??

คำตอบ

2 sleepingkit Aug 20 2020 at 00:44

คุณอาจมีรายการบูลีนเพื่อบันทึกว่าปุ่มใดถูกเลือกจากนั้นส่งบูลเป็นพารามิเตอร์ไปยังวิดเจ็ตเสียงและใช้บูลเพื่อเปลี่ยนไอคอน

ส่งผ่านฟังก์ชั่นการโทรกลับเพื่อเปลี่ยนรายการบูลด้วยเนื่องจากคุณต้องเปลี่ยนรายการจากวิดเจ็ตหลักดังนั้นจึงจำเป็นต้องมีฟังก์ชันการโทรกลับ

 List<bool> audioSelectedList = List.generate(AudioList.length, (i) => false);

// This is a callback function that Audio will call when the button is clicked.
  selected(int index){
// set only one bool to be true
    setState(() {
      audioSelectedList=List.generate(AudioList.length, (i) => false);// set all to false
      audioSelectedList[index]=true;  // set the selected index to be true
    });
  }

ListView:

ListView.builder(
          itemCount: AudioList.length,
          itemBuilder: (context, index) => Audio(
            selected: selected, // pass the callback function
            index: index, // use to call selected(index)
            isSelected: audioSelectedList[index], // only one bool is true in the list which is the selected index.
          ),
        ),
4 Reign Aug 20 2020 at 00:41
import 'package:flutter/material.dart';

class Sample extends StatefulWidget {
  @override
  _SampleState createState() => _SampleState();
}

class _SampleState extends State<Sample> {
  bool isPressed = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          Column(
            children: [
              IconButton(
                  icon: Icon(isPressed
                      ? Icons.play_circle_filled
                      : Icons.pause_circle_filled),
                  onPressed: () {
                    setState(() {
                      isPressed = !isPressed;
                    });
                  }),
              IconButton(
                  icon: Icon(isPressed
                      ? Icons.play_circle_filled
                      : Icons.pause_circle_filled),
                  onPressed: () {
                    setState(() {
                      isPressed = !isPressed;
                    });
                  }),
              PlayPause(
                isPressed: isPressed,
              ),
              PlayPause(),
            ],
          )
        ],
      ),
    );
  }
}

class PlayPause extends StatefulWidget {
  const PlayPause({
    Key key,
    this.isPressed = false,
  }) : super(key: key);
  final bool isPressed;

  @override
  _PlayPauseState createState() => _PlayPauseState();
}

class _PlayPauseState extends State<PlayPause> {
  bool _isPressed;
  @override
  void initState() {
    super.initState();
    _isPressed = widget.isPressed;
  }

  @override
  Widget build(BuildContext context) {
    return IconButton(
        icon: Icon(
            _isPressed ? Icons.play_circle_filled : Icons.pause_circle_filled),
        onPressed: () {
          setState(() {
            _isPressed = !_isPressed;
          });
        });
  }
}

jack Aug 20 2020 at 02:26

อย่าใช้บูลเดียวสำหรับเสียงทั้งหมดของคุณคุณสามารถใช้คอลเลกชันเพื่อกำหนดบูลสำหรับทุกสิ่งในรายการเสียงของคุณและเมื่อคุณคลิกเปลี่ยนบูลสำหรับเสียง