ListView.Builder play/Pause buttom flutter

后端 未结 3 1223
醉酒成梦
醉酒成梦 2020-12-21 18:50

I am using a ListView.builder to create an audio list, I need to change the play button pause individually to an item when I select it, I have tried a bool but giving play o

相关标签:
3条回答
  • 2020-12-21 19:25
    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;
              });
            });
      }
    }
    
    
    0 讨论(0)
  • 2020-12-21 19:42

    Don't use one bool for all of your audio, you can use collections to define a bool for everything in your audioList, and when you click, change the bool for the audio.

    0 讨论(0)
  • 2020-12-21 19:52

    You may have a list of boolean to save which button is selected, then pass the bool as a parameter to the audio widget, and use the bool to change the icon.

    Also pass a callback function to change the bool list, because you have to change the list from the parent widget, so a callback function is needed.

     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.
              ),
            ),
    
    0 讨论(0)
提交回复
热议问题