bool 値を cubit から変更したいのですが、その方法がわかりません。
たとえば、達成したいのは、if (cubit に格納されたブール値が true) "show widget A" : "show widget B" です。
私のコード:
class ChangeBoolCubit extends Cubit<bool> {
ChangeBoolCubit() : super(false);
bool one = false;
bool two = true;
void changeValue(bool booleanToChange) {
booleanToChange = !state;
emit(booleanToChange);
}
}
意見:
class BoolView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Changing Bool')),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Center(
child: BlocBuilder<ChangeBoolCubit, bool>(
builder: (context, state) {
return (ChangeBoolCubit().one)
? Text('TRUE: ${ChangeBoolCubit().one}')
: Text('FALSE: ${ChangeBoolCubit().one};');
},
),
),
Center(
child: BlocBuilder<ChangeBoolCubit, bool>(
builder: (context, state) {
return (ChangeBoolCubit().two)
? Text('TRUE: ${ChangeBoolCubit().two}')
: Text('FALSE: ${ChangeBoolCubit().two};');
},
),
),
],),
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
FloatingActionButton(
child: const Icon(Icons.add),
onPressed: () => context.read<ChangeBoolCubit>().changeValue(ChangeBoolCubit().one),
),
const SizedBox(height: 8),
FloatingActionButton(
child: const Icon(Icons.remove),
onPressed: () => context.read<ChangeBoolCubit>().changeValue(ChangeBoolCubit().two),
),
],
),
);
}
}
おそらく些細な質問で申し訳ありませんが、Cubit/Bloc は初めてです。