navigate to the second page of the bottom navigation bar and update only that page without affecting the other pages

34 Views Asked by At

the first page is named home. there i have a list of items when i click on one of them i navigate to a details page like this :

Navigator.push(
                                        context,
                                        MaterialPageRoute(
                                            builder: (context) => AudioPage(
                                                  snap: documentSnapshot,
                                                  id: documentSnapshot.id,
                                                )));

at the bottom 'AudioPage' page there is a play button widget which stores the doc id into a provider

Provider.of<AudiobookProvider>(context, listen: false)
                        .setSelectedAudiobookId(widget.id);

and then i want to pop until the first page and navigate to the second page of the bottom navigation bar and update only that page without affecting the other pages. because the second page contains an audio player which i want to reinitiate so i dont have multiple audios playing

late AudioPlayer _audioPlayer;
  final _playlist = ConcatenatingAudioSource(children: []);
  Stream<PositionData> get _positionDataStream =>
      Rx.combineLatest3<Duration, Duration, Duration?, PositionData>(
          _audioPlayer.positionStream,
          _audioPlayer.bufferedPositionStream,
          _audioPlayer.durationStream,
          (position, bufferedPosition, duration) => PositionData(
              position, bufferedPosition, duration ?? Duration.zero));
  Map<String, dynamic>? documentData;
  Future<void> fetchDocument(String id) async {
    try {
      DocumentSnapshot documentSnapshot =
          await FirebaseFirestore.instance.collection('Audios').doc(id).get();

      if (documentSnapshot.exists) {
        setState(() {
          documentData = documentSnapshot.data() as Map<String, dynamic>;
        });
      } else {
        print('Document does not exist');
      }
    } catch (e) {
      print('Error fetching document: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    final audiobookProvider =
        Provider.of<AudiobookProvider>(context).selectedAudiobookId;
    fetchDocument(audiobookProvider);
    print(documentData.toString());

    if (documentData != null) {
      _audioPlayer = AudioPlayer()
        ..setUrl(documentData!['Chapters'][0].toString());
      return Scaffold(
          backgroundColor: background,
          appBar: AppBar(
            toolbarHeight: 80,
            backgroundColor: Color.fromRGBO(12, 16, 21, 1),
            bottomOpacity: 0.0,
            elevation: 0.0,
            centerTitle: true,
            scrolledUnderElevation: 0,
            leadingWidth: 70,
            leading: IconButton(
              icon: Icon(
                Icons.keyboard_arrow_down_rounded,
                size: 28,
              ),
              onPressed: () {
                print(documentData.toString());
              },
            ),
            title: Text(
              nowplaying,
              style: TextStyle(
                  color: textColors,
                  fontSize: titleS,
                  fontWeight: FontWeight.bold),
            ),
          ),
          body: Container(
            padding:
                const EdgeInsets.only(top: 60, left: 20, right: 20, bottom: 20),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  child: Column(
                    children: [
                      StreamBuilder<PositionData>(
                          stream: _positionDataStream,
                          builder: (context, snapshot) {
                            final positionData = snapshot.data;
                            return ProgressBar(
                              baseBarColor: searchbar2,
                              bufferedBarColor: searchbar2,
                              progressBarColor: textColors,
                              thumbColor: textColors,
                              barHeight: 4,
                              thumbRadius: 5,
                              timeLabelLocation: TimeLabelLocation.below,
                              timeLabelPadding: 3,
                              timeLabelType: TimeLabelType.remainingTime,
                              progress: positionData?.position ?? Duration.zero,
                              buffered: positionData?.bufferedPosition ??
                                  Duration.zero,
                              total: positionData?.duration ?? Duration.zero,
                              onSeek: _audioPlayer.seek,
                            );
                          }),
                      Controls(audioplayer: _audioPlayer),
                    ],
                  ),
                )
              ],
            ),
          ));
    } else {
      _audioPlayer = AudioPlayer()
        ..setUrl(this is a default url so i dont get an error i will change it later or find a way to display an empty player that wait for a valid url);
      return Scaffold(
          backgroundColor: background,
          appBar: AppBar(
            toolbarHeight: 80,
            backgroundColor: Color.fromRGBO(12, 16, 21, 1),
            bottomOpacity: 0.0,
            elevation: 0.0,
            centerTitle: true,
            scrolledUnderElevation: 0,
            leadingWidth: 70,
            leading: IconButton(
              icon: Icon(
                Icons.keyboard_arrow_down_rounded,
                size: 28,
              ),
              onPressed: () {
                print(audiobookProvider);
              },
            ),
            title: Text(
              nowplaying,
              style: TextStyle(
                  color: textColors,
                  fontSize: titleS,
                  fontWeight: FontWeight.bold),
            ),
          ),
          body: Container(
            padding:
                const EdgeInsets.only(top: 60, left: 20, right: 20, bottom: 20),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  child: Column(
                    children: [
                      StreamBuilder<PositionData>(
                          stream: _positionDataStream,
                          builder: (context, snapshot) {
                            final positionData = snapshot.data;
                            return ProgressBar(
                              baseBarColor: searchbar2,
                              bufferedBarColor: searchbar2,
                              progressBarColor: textColors,
                              thumbColor: textColors,
                              barHeight: 4,
                              thumbRadius: 5,
                              timeLabelLocation: TimeLabelLocation.below,
                              timeLabelPadding: 3,
                              timeLabelType: TimeLabelType.remainingTime,
                              progress: positionData?.position ?? Duration.zero,
                              buffered: positionData?.bufferedPosition ??
                                  Duration.zero,
                              total: positionData?.duration ?? Duration.zero,
                              onSeek: _audioPlayer.seek,
                            );
                          }),
                      Controls(audioplayer: _audioPlayer),
                    ],
                  ),
                )
              ],
            ),
          ));
    }
  }
}
0

There are 0 best solutions below