This radio button inside showModalBottomSheet is only change when I close and reopen this modalBottomSheet. setState is not working properly there. Can anyone solve this and say why this is happening?
But when I put these radio out from the modal sheet it works normally.
import 'package:flutter/material.dart';
/// Flutter code sample for [showModalBottomSheet].
void main() => runApp(const BottomSheetApp());
class BottomSheetApp extends StatelessWidget {
const BottomSheetApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Bottom Sheet Sample')),
body: const BottomSheetExample(),
),
);
}
}
class BottomSheetExample extends StatefulWidget {
const BottomSheetExample({super.key});
@override
State<BottomSheetExample> createState() => _BottomSheetExampleState();
}
class _BottomSheetExampleState extends State<BottomSheetExample> {
int _selectedOption = 0;
@override
Widget build(BuildContext context) {
void handleRadioValueChanged(int? value) {
setState(() {
_selectedOption = value!;
});
}
return Center(
child: ElevatedButton(
child: const Text('showModalBottomSheet'),
onPressed: () {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return SizedBox(
height: 200,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
title: const Text("Administrative Associate"),
trailing: Radio(
value: 2,
groupValue: _selectedOption,
onChanged: handleRadioValueChanged),
),
ListTile(
title: const Text("Account Manager"),
trailing: Radio(
value: 3,
groupValue: _selectedOption,
onChanged: handleRadioValueChanged),
),
],
),
),
);
},
);
},
),
);
}
}
I want to get the changed value without closing it.
2
Answers
Add your widget inside StatefulBuilder
Updated Answer
Your widget:
Bottomsheet widget: