Good evening everyone.
I’m structuring an app on 2 columns. In the left column I would like to insert a button that updates the contents of the right column. Although keeping all the code in a single class I can do what I have in mind, creating 2 classes (one for the left column and one for the right one) I do not understand how to call the callback or setState.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter layout demo',
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
HomePageState createState() => HomePageState();
}
class HomePageState extends State<HomePage> {
String TestoADestra = 'TestoInizialeADestra';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter layout demo',
home: Scaffold(
body: Row(
children: [
ColonnaSx(),
ColonnaDx(),
],
)),
);
}
}
class ColonnaDx extends StatefulWidget {
@override
ColonnaDxState createState() => ColonnaDxState();
}
class ColonnaDxState extends State<ColonnaDx> {
@override
Widget build(BuildContext context) {
return Expanded(
flex: 4,
child: Container(
decoration: const BoxDecoration(
color: Colors.blue,
),
child: Column(
children: [
Expanded(
child: Center(
child: Text(
'Here I would like to put other things but for the moment there is this'),
),
),
],
),
),
);
}
}
class ColonnaSx extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Expanded(
flex: 1,
child: Container(
decoration: BoxDecoration(
color: Colors.red,
),
child: Column(
children: [
Expanded(
child: Center(
child: BottoneA(),
),
),
],
),
),
);
}
}
class BottoneA extends StatelessWidget {
@override
Widget build(BuildContext context) {
return TextButton(
style: ButtonStyle(
foregroundColor:
MaterialStateProperty.all<Color>(Color.fromARGB(255, 72, 3, 201)),
),
onPressed: () {},
child: Text('Press here'),
);
}
}
I tried to insert:
- regarding "ButtonA" the callBack function to onPressed and I declared the function before the override something like:
final String TestoCheVorreiComparisse;
final Function callbackFunction;
const BottoneA({Key? key, required this.TestoCheVorreiComparisse, required this.callbackFunction}) : super(key: key);
- as for "ColumnDX" before the override something like this:
String TestoCheVorreiComparisse="Vediamo se funziona";
callback(varTestoSostitutivo){
setState(() {
TestoCheVorreiComparisse=varTestoSostitutivo;
});
}
I am told, and I understand why, that there is no callback function in "buttonA"… How can I do this? Thank you!
3
Answers
You can use Provider and their values are global so you may use them anywhere where you want and can update state event without StatefulWidget.
here is the link:Provider
Without using third party packages, you can lift your state to their first common parent and pass the state/modification functions down to both elements.
If you like to just get the tab event you can use
VoidCallback
, to pass data you need to useFunction()
callback. While the widget is nested, it you need to pass function on child. Here is the demo using both approach, But for project level you may choose state management like riverpod/bloc