0

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",
        )))
}

2 Answers 2

1

try these: 1.

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

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

Sign up to request clarification or add additional context in comments.

Comments

1

try this one.

  void decrementCounter(int number) {
    _gracze -= number;
    _gracze = _gracze <= 1 ? graczeCon.text ?? '' : liczbagraczy;
  }

Also, keep in mind that, graczeCon.text is a String value. So handle it as you see fit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.