'How to update State variable of a class which extends State from another Stateful class file?

In my Flutter project, I have the default Flutter code in home screen to update a counter by floating action button press. Here's the code of that class-

main.dart

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

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home:  MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
   MyHomePage({Key? key, required this.title}) : super(key: key);

   String title;

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

class MyHomePageState extends State<MyHomePage> {
  int counter = 0;

  void _incrementCounter() {
    setState(() {
      counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: UpdateWidget().showButton()
      ),
    );
  }
}

Now, I just wanted to update the counter(state variable) without making it static from a new class named UpdateWidget. I have tried updating the variable like this way:

UpdateWidget.dart

    class UpdateWidget extends StatefulWidget {
  UpdateWidget({Key? key}) : super(key: key);


  @override
  _UpdateWidgetState createState() => _UpdateWidgetState();

  GlobalKey<MyHomePageState> homeKey = GlobalKey();


  Widget showButton() {
    return FlatButton(
      onPressed: () {
        MyHomePage(key: homeKey, title: 'Hello',);
        debugPrint('pressed');
        homeKey.currentState?.counter = 5;
      }, child: const Text('A'),
    );
  }

}

class _UpdateWidgetState extends State<UpdateWidget> {
  double defaultScreenWidth = 400.0;
  double defaultScreenHeight = 810.0;

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Center(
        child: Text(''),
      ),
    );
  }
}

In, UpdateWidget class, inside FlatButton onPressed block, the print message is showing but it is not updating the counter variable like the way I wanted.

So, I need a way to update this variable and show that in UI.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source