That’s my main screen:
class CreateApplyScreen extends StatefulWidget {
const CreateApplyScreen({super.key});
@override
State<CreateApplyScreen> createState() => _CreateApplyScreenState();
}
class _CreateApplyScreenState extends State<CreateApplyScreen> {
final bloc = CreateApplyBloc();
@override
Widget build(BuildContext context) {
return Scaffold(
body: BlocProvider<CreateApplyBloc>(
create: (context) => bloc,
child: CustomWillPopScope(
onWillPop: false,
action: () {},
child: ValueListenableBuilder<int>(
valueListenable: currentPage,
builder: (BuildContext context, int value, Widget? child) {
return Padding(
padding: const EdgeInsets.only(left: 16.0, right: 16),
child: PageView(
controller: _pageController,
physics: const NeverScrollableScrollPhysics(),
children: [
CreditInfo(
bloc: bloc,
onCountinue: () {
onNavigate();
},
),
],
),
);
}),
),
),
);
}
My second screen
class CreditInfo extends StatelessWidget {
final CreateApplyBloc bloc;
final Function onCountinue;
const CreditInfo({super.key, required this.onCountinue, required this.bloc});
@override
Widget build(BuildContext context) {
return BlocConsumer<CreateApplyBloc, DataState>(
listener: (context, state) {},
builder: (context, state) {
if (state is CreateApplyState) {
return Column(
children: [
CustomTextField(
hintText: state.selectedMonth == 0
? AppStrings.select
: '${state.selectedMonth} ay',
onTap: () {
showSuggestedMonths(context, () {},state,bloc);
},
),
],
);
}
return Container();
});
}
}
and my bottom modal sheet:
showSuggestedMonths(BuildContext context, Function onTapYes,
CreateApplyState state, CreateApplyBloc bloc) {
showModalBottomSheet(
backgroundColor: Colors.white,
elevation: 0,
isScrollControlled: true,
context: context,
builder: (_) => SuggestedMonthsModal(
onTapYes: onTapYes, state: state, bloc: bloc, ctx: context));
}
class SuggestedMonthsModal extends StatelessWidget {
final Function onTapYes;
final BuildContext ctx;
final CreateApplyState state;
final CreateApplyBloc bloc;
const SuggestedMonthsModal(
{super.key,
required this.onTapYes,
required this.state,
required this.ctx,
required this.bloc});
@override
Widget build(BuildContext context) {
return BlocProvider<CreateApplyBloc>(
create: (context) => CreateApplyBloc(),
child: Column(
children: [
ModalViewWidget(
widget: ListView.builder(
shrinkWrap: true,
itemCount: state.suggestedMonths.length,
itemBuilder: (ctx, i) {
return Column(
children: [
InkWell(
onTap: () {
BlocProvider.of<CreateApplyBloc>(context).add(SelectMonthEvent(state.suggestedMonths[i]));
},
child: Row(
children: [
state.selectedMonth == state.suggestedMonths[i]
? SvgPicture.asset(AppImages.filledRadio)
: SvgPicture.asset(AppImages.emptyRadio),
],
),
),
],
);
}),
)
],
),
);
}
}
So when I Press
onTap: () {
BlocProvider.of<CreateApplyBloc>(context).add(SelectMonthEvent(state.suggestedMonths[i]));
},
here to update my code, nothing changes, state is not updating. It is updates only after hot reload. I think problem is somewhere with passing context.(not inside my bloc file or state) I have already tried everything, but nothing works.
2
Answers
The issue seems to be with the way you are providing your
CreateApplyBloc
to your widgets. In yourSuggestedMonthsModal
widget, you are creating a new instance ofCreateApplyBloc
which is different from the one you passed down from the CreateApplyScreen. This means that any changes in state made in this new bloc will not reflect in the old bloc.To fix this, you should pass the same instance of the bloc to your SuggestedMonthsModal widget. Here’s how you can do it:
also
one approach is to wrap the bottom sheet’s column widget with a StatefulBuilder widget.