I have this code:
class buildApp extends StatefulWidget {
const buildApp({super.key});
@override
State<buildApp> createState() => _buildAppState();
}
class _buildAppState extends State<buildApp> {
@override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context,constraints){
if(constraints.maxWidth <= 768){
return buildPortrait(State);
}
return const Text('');
});
}
}
buildPortrait(){
...
}
The problem is that inside the buildPortrait() I need to call setState because I have a DropDownButton and a onChanged event but steState doesn't exist there. Is possible to do it? Maybe get the state and pass as variable?
Off course that I am open for news approaches...I am trying to create a portrait and lasdscape responsive app.
Thank you.
I can create all the two functions build Portrait() and build LandScape() inside the class buildApp but the code will get a mess.
as I mentioned on the comment
setState
only avaliable inside the State class. in your case, you named it as _buildAppState
class _buildAppState extends State<buildApp> {
@override
Widget build(BuildContext context) {
// widget code will be here
}
//put your function inside , so you can free to use it
buildPortrait(){
...
}
} // close bracket for _buildAppState class
if you want to separate your method, you may use this workaround:
your method in another file,
helper.dart
buildPortrait({VoidCallback? sst}){
// your code here
// every time you want to call setState, just invoke the sst
sst?.call(); // this will execute setState in the parent class
...
}
buildApp.dart
class buildApp extends StatefulWidget {
const buildApp({super.key});
@override
State<buildApp> createState() => _buildAppState();
}
class _buildAppState extends State<buildApp> {
@override
Widget build(BuildContext context) {
return Column(
children:[
ElevatedButton(
onPressed: () {
/// now you can use it and pass the params setState
buildPortrait(sst: () => setState(() {}));
},
child: Text("nasdasda")
)
}