ListView.Builder oynat / buttom flutter'ı duraklat

Aug 19 2020

Bir ses listesi oluşturmak için ListView.builder kullanıyorum, seçtiğimde oynatma düğmesini duraklatmayı tek tek bir öğe olarak değiştirmem gerekiyor, bir bool denedim ama listedeki tüm öğeleri oynatma veya duraklatma veriyorum, birisi yapabilir buna yardım ediyor musun?

Yanıtlar

2 sleepingkit Aug 20 2020 at 00:44

Hangi düğmenin seçildiğini kaydetmek için bir boole listeniz olabilir , ardından bool'u bir parametre olarak ses parçacığına iletin ve simgeyi değiştirmek için bool'u kullanın.

Bool listesini değiştirmek için bir geri çağırma işlevi de iletin, çünkü listeyi ana pencere öğesinden değiştirmeniz gerekir, bu nedenle bir geri çağrı işlevi gerekir.

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

Liste görünümü:

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

Tüm sesleriniz için tek bir bool kullanmayın, sesListenizdeki her şey için bir bool tanımlamak için koleksiyonları kullanabilirsiniz ve tıkladığınızda, ses için bool'u değiştirebilirsiniz.