1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] How to open modal sheet only once using Bloc

Discussão em 'Mobile' iniciado por Stack, Outubro 9, 2024 às 07:52.

  1. Stack

    Stack Membro Participativo

    Scenario is I want to open modal sheet when state is GetExperienceLoadedState and AppStrings.isFeedbackForFinancing == true and I am calling an API on initState method.

    Modal sheet is appearing but the issue is it is appearing multiple times. and this number of times changes when i come back to the screen. like initially it is 2, then 4, then 6 or sometimes 8 and so on.

    here is my code of bloc

    Future _getExperience(GetExperienceEvent event, emit) async {
    try {
    emit(GetExperienceLoadingState());
    final allExperience = await getExperienceUsecase.getExperience(
    languageKey: event.languageKey);
    _getExperienceList = allExperience;

    emit(GetExperienceLoadedState());

    } on InvalidInputException catch (e) {
    emit(GetExperienceErrorState(
    errorMessage: e.message ?? '',
    ));
    } catch (e) {
    emit(GetExperienceErrorState(errorMessage: e.toString()));
    }
    }


    here is my ** screen code**

    bool _isBottomSheetShown = false;

    @override
    void initState() {
    _feedbackBloc = BlocProvider.of<FeedbackBloc>(context);

    // Conditional feedback calls
    if (AppStrings.isFeedbackForFinancing == true) {
    _callFeedbackExperienceEvent();
    }
    super.initState();
    }

    Future<void> _callFeedbackExperienceEvent() async {
    _feedbackBloc = BlocProvider.of<FeedbackBloc>(context);
    WidgetsBinding.instance.addPostFrameCallback((_) {
    _feedbackBloc.add(GetExperienceEvent(languageKey: '${context.locale}'));
    });
    }

    @override
    Widget build(BuildContext context) {
    super.build(context);
    final height = context.screenHeight;
    final width = context.screenWidth;
    final readBlocContext = context.read<DashboardBloc>();
    final readRewardBlocContext = context.read<RewardsBloc>();
    final _feedbackBloc = context.read<FeedbackBloc>();
    log('isFeedbackForFinancing::: ${AppStrings.isFeedbackForFinancing} ${widget.isFromFianancing}');

    return Padding(
    padding: AppSpacing.leftRightPadding,
    child: BlocConsumer<DashboardBloc, DasbhboardState>(
    bloc: _dasboardBloc,
    listener: (context, state) {
    if (state is AccountIncomeErrorState ||
    state is AccountBalanceErrorState ||
    state is RecommendedOfferErrorState ||
    state is PointsCatActionLoadingState) {
    HelperFunctions.showToast(context.text.genericError);
    }
    },
    builder: (context, dashboardState) {
    return BlocConsumer<RewardsBloc, RewardState>(
    bloc: _rewardsBloc,
    listener: (context, state) {
    if (state is RewardErrorState) {
    HelperFunctions.showToast(context.text.genericError);
    }
    },
    builder: (context, rewardsState) {
    bool isLoading =
    dashboardState is AccountIncomeLoadingState ||
    rewardsState is RewardLoadingState ||
    _rewardsBloc is PointsCatActionLoadingState ||
    _feedbackBloc is GetFeedbacksLoadingState ||
    dashboardState is RecommendedOfferLoadingState;

    if (isLoading) {
    return _customDashboardShimmerListView();
    } else {
    // log('_feedbackBloc.customerFeedbackStatus ${_feedbackBloc.customerFeedbackStatus}');
    return CustomRefreshIndicatorWidget(
    onRefresh: _refreshData,
    child: SingleChildScrollView(
    physics: const BouncingScrollPhysics(),
    child: Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: [
    //some widget

    BlocConsumer<FeedbackBloc, FeedbackState>(
    bloc: _feedbackBloc,

    listener: (context, feedbackState) {


    if (feedbackState is GetExperienceErrorState) {
    HelperFunctions.showToast(context.text.genericError);
    }

    if (feedbackState is GetExperienceLoadedState ) {
    if (AppStrings.isFeedbackForFinancing == true && _isBottomSheetShown==false) {

    _isBottomSheetShown = true;
    _showBottomSheetWidget();
    }
    }
    },



    builder: (BuildContext context, FeedbackState feedbackState) {
    return SizedBox();
    },
    ),
    //some widget


    Future _showBottomSheetWidget() {
    final readFeedbackBlocContext = context.read<FeedbackBloc>();

    return showModalBottomSheet(
    context: context,
    isScrollControlled: true,
    backgroundColor: AppColors.kWhiteColor,
    shape: const RoundedRectangleBorder(
    borderRadius: BorderRadius.only(
    topLeft: Radius.circular(20),
    topRight: Radius.circular(20),
    ),
    ),
    builder: (context) => FeedbackModalSheetWidget(
    featureIds: readFeedbackBlocContext.financingTypeFeatureIds,
    feedbackOptionList: readFeedbackBlocContext.getExperienceList),
    ).then((result) {
    setState(() {
    AppStrings.isFeedbackForFinancing = false;
    _isBottomSheetShown = false; // Allow modal to be shown again later
    });
    });
    }


    i also added this condition _isBottomSheetShown==false if modal appears once, then it shouldn't appear again, but it is not working, i guess the problem is on listener but i couldn't identify it. please help me to fix this issue.

    Continue reading...

Compartilhe esta Página