-2

I want to do Alert dialog number increment (+1) number reduction (-1) number value reset (R), but it does not appear on the screen, when I save the file the value appears on the screen, how can I do this? İmage: enter image description here

serkan
  • 21
  • 1
  • 1
  • 6

2 Answers2

0

You are not using StateFull Widget and setState().
when you want to change value on screen dynamically call setState()
check the following example:

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

int currentValue = 0;

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primaryColor: Colors.indigo,
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

//use stateful widget when you want to change state
class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          //show the current value on screen
          Text(
            '$currentValue',
            textAlign: TextAlign.center,
          ),

          //button to show dialog
          ElevatedButton(
            child: Text("show Dialog "),
            onPressed: () {
              showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    return Dialog();
                  });
            },
          ),
        ],
      ),
    );
  }
}

class Dialog extends StatefulWidget {
  const Dialog({super.key});

  @override
  State<Dialog> createState() => _DialogState();
}

class _DialogState extends State<Dialog> {
  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: Text('$currentValue'),
      actions: [
        //button to increase the value of currentValue
        ElevatedButton(
            onPressed: () {
              //call setState, it will Update the state
              setState(() {
                //increment the value
                currentValue++;
              });
            },
            child: Text('Increase')),
        ElevatedButton(
            onPressed: () {
              setState(() {
                //reduce the value
                currentValue--;
              });
            },
            child: Text('Decrease')),
        ElevatedButton(
            onPressed: () {
              setState(() {
                //reduce the value
                currentValue = 0;
              });
            },
            child: Text('Reset')),
        ElevatedButton(
            onPressed: () {
              Navigator.pop(context);
            },
            child: Text('Remove Dialog')),
      ],
    );
  }
}

Saad
  • 539
  • 2
  • 19
  • First of all, thank you for your interest. I write my functions in the atomic widget, I leave my codes below, I will be glad if you tell me where I made a mistake Code: – serkan Mar 09 '23 at 11:02
  • Number increase and decrease functions : int yolcuSayisi = 0; void yolcuArtiBir() { setState(() { yolcuSayisi++; }); } void yolcuEksiBir() { setState(() { yolcuSayisi--; }); } – serkan Mar 09 '23 at 11:03
  • can you put your whole code in your question? – Saad Mar 09 '23 at 11:06
  • IconButton( color: Colors.green.shade600, iconSize: 30, onPressed: () { yolcuArtiBir(); }, icon: Icon(Icons.plus_one)), IconButton( iconSize: 30, color: Colors.red, onPressed: () { yolcuEksiBir(); – serkan Mar 09 '23 at 11:07
  • there is no problem with your functions, to show updated value in `dialog` create a separate `Stateful Widget` for Dialog – Saad Mar 09 '23 at 11:13
0

Please make your class StatelessWidget to StatefullWidget and use setState while calling onPressed()

onPressed: () { 
     setState(() { 
             //logic
     }); 
}

Hope this helps!