Search code examples
flutterdartprovider

Assigning variable value's from controller flutter


Okey, we have buton which is decrementing var _gracze every time when he get pushed, if _gracze <=0 i want him to take value from controller graczeCon and put it instead of _gracze. I don't know how to take value from controller and put it into _gracze(SecGamePage is same thing as FirGamePage). pls help :c

class ZmienneClass extends ChangeNotifier {
  var _gracze;
  var _rundy;

  int get iloscGraczy {
    return _gracze;
  }

  void decrementCounter(int number) {
    _gracze -= 1;
      if (_gracze <= 1) {
        _gracze = liczbagraczy;
      }
    }
  }
}

class _InfoPageState extends State<InfoPage> {[![InfoPage, graczeCon][1]][1]
  TextEditingController graczeCon = TextEditingController();
  TextEditingController rundyCon = TextEditingController();

  int liczbagraczy = 10; //here should be value from graczeCon

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: [ChangeNotifierProvider.value(value: ZmienneClass())],
        child: Scaffold(
            backgroundColor: const Color.fromARGB(255, 84, 84, 84),
            appBar: AppBar(
              toolbarHeight: 70,]))
            body: Stack(children: [
              Column(
                children: [
                  Center(
                    child: SizedBox(
                      child: TextFormField(
                        onChanged: (gracze) {},
                        controller: graczeCon,
                    )))])
              Align(
                child: TextButton(
                  onPressed: () {
                    Provider.of<ZmienneClass>(context, listen: false)
                        .setPlayerCount(
                            liczbagraczy: int.parse(graczeCon.text));

                    Navigator.push(context,
                        MaterialPageRoute(builder: (context) => FirGamePage()));
                  })];
  }
}

class _FirGamePageState extends State<FirGamePage> {


  @override
  Widget build(BuildContext context) {
    var _gracze = Provider.of<ZmienneClass>(context).iloscGraczy;
    return Scaffold(...
        Align(
          alignment: Alignment.topLeft,
          child: Text("Osoby: $_gracze",
        )))
}

Solution

  • try these: 1.

    graczeCon.text.toString()
    
    graczeCon.value.toString()
    

    here the first solution should work coz it has in my case.