GlobalKey Using child acess In Flutter

GlobalKey Using child acess In Flutter

GlobalKey Using child acess In Flutter

GlobalKey_Access.dart

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomeApp(),
    );
  }
}

class HomeApp extends StatelessWidget {
  final GlobalKey<_HomePageState> globalKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text('Global Acess'),
          ),
          body: Column(mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              IconButton(
                icon: Icon(Icons.help),
                onPressed: () {
                  globalKey.currentState.methodA();
                },
              ),
              HomePage(key: globalKey),
            ],
          )),
    );
  }
}

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

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  String name = "Hi";
  @override
  Widget build(BuildContext context) {
    return Text(name);
  }

  void methodA() {
    setState(() {
      name = "Welcome";
    });
  }
}

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in