How to share data between widgets in Flutter?

Hi,

I’m a Flutter beginner and I’m trying to develop a simple desktop app with Flutter. For this simple app, I have a file named entradas_prueba.dart in which I define a class named EntradasPrueba. The build method of this class returns a Column with various widgets, like DropdownButtons and ElevatedButtons. For example, I created a class named BotonRegEntrada, which is in a separate file named boton_reg_entrada.dart (in which I have an ElevatedButton), and I create an instance of this class to add it to the column of EntradasPrueba.

But, how can the onPressed function of the boton_reg_entrada.dart know which value was selected by the user using the DropdownButton, if they are in different files?

Here is entradas_prueba.dart:

import 'package:flutter/material.dart';
import 'package:labe_project/pruebas/lista_productos.dart';
import 'boton_reg_entrada.dart';
import 'escoge_bodega.dart';
import 'escoge_proveedor.dart';
import 'lista_stock.dart';

class EntradasPrueba extends StatelessWidget {

  var comboProveedor = new EscogeProveedor();
  var comboBodega = new EscogeBodega();
  var miListaProductos = new ListaProductos();
  var miBotonRegEntrada = new BotonRegEntrada();
  var miListaStock = new ListaStock();

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      width: 400,
      height: 600,
      decoration: BoxDecoration(
        color: Color(0xFFEEEEEE),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.max,
        children: <Widget>[
          Text("Nueva entrada"),
          comboProveedor,
          comboBodega,
          Text("Registro de productos"),
          miListaProductos,
          miBotonRegEntrada,
          miListaStock,
        ],
      ),
    );
  }
}

And the class of the button:

import 'package:flutter/material.dart';

class BotonRegEntrada extends StatefulWidget {

  //var comboProv;
  //var comboBod;

  //BotonRegEntrada(this.comboProv, this.comboBod);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _BotonRegEntrada();
  }
}

class _BotonRegEntrada extends State<BotonRegEntrada>{

  void onPressedRegistra() {
    ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text("Has registrado una entrada"),
        )
    );

    //How can the button know which value was selected in the dropdown button?
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      margin: EdgeInsets.only(
        top: 10.0,
        bottom: 10.0,
      ),

      child: ElevatedButton(
        onPressed: onPressedRegistra,
        child: const Text("Registro de producto"),
      ),
    );


  }

}

And the escoge_proveedor.dart file:

import 'package:flutter/material.dart';

class EscogeProveedor extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _EscogeProveedor();
  }
}

class _EscogeProveedor extends State<EscogeProveedor> {
  String dropdownValue = 'Proveedor';

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return DropdownButton<String>(
      value: dropdownValue,
      icon: const Icon(Icons.arrow_downward),
      elevation: 16,
      style: const TextStyle(color: Colors.deepPurple),
      underline: Container(
        height: 2,
        color: Colors.deepPurpleAccent,
      ),
      onChanged: (String? newValue) {
        setState(() {
          dropdownValue = newValue!;
        });
      },
      items: <String>['Proveedor', 'Grupo Modelo', 'Red Cola']
          .map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(value),
        );
      }).toList(),
    );
  }

}

Thanks in advance for any help!

Hi
I had the same question when I started and what you need to do is:
You should take a look at the recommended state management called PROVIDER.
Specifically, *ChangeNotifierProvider* is the widget that provides an instance of a ChangeNotifier to its descendants.

If you need a state manager, use getx, else use getters