How Can Multiple Widgets Uses the Same Global Key In Flutter??

· 2 min read
How Can Multiple Widgets Uses the Same Global Key In Flutter
How Can Multiple Widgets Uses the Same Global Key In Flutter

To understand it clearly consider a case where a user is having a simple application that has three TabBar Views in it. It is also RefreshIndicator in each Tab with a ListView. So in this article, we will go through How can multiple widgets uses the same global key in Flutter??

How Can Multiple Widgets Use the Same Global Key In Flutter??

User can create his keys “by hand” and use static/constant values.

import 'package:flutter/widgets.dart';

class RIKeys {
  static final riKey1 = const Key('__RIKEY1__');
  static final riKey2 = const Key('__RIKEY2__');
  static final riKey3 = const Key('__RIKEY3__');
}

Then in TabBarView

  body: new TabBarView(
            children: [
              new RefreshIndicator(new RefreshIndicator(
// Use the Manual Static Value instead ...
                key: RIKeys.riKey1,
                onRefresh: _actualizoData,
                child: new ListView.builder(
                    padding: new EdgeInsets.only(top: 5.0),
                    itemCount: linea_reservas.length * 2,
                    itemBuilder: (BuildContext context, int position) {
                      if (position.isOdd) return new Divider();
                      final index = position ~/ 2;
                      return _buildRow(index);
                    }),
              ),

Those who want to clear with GlobalKey:

import 'package:flutter/widgets.dart';

class JosKeys {
  static final josKeys1 = GlobalKey();
  static final josKeys2 = GlobalKey();
}

Then use it below:

[
    CoachTutorialModel(
      ids: "post",
      globalKeys: JosKeys.josKeys1,
      icons: Icon(
        Icons.message,
        color: Colors.white,
        size: 90,
      ),
      ...
    ),

   CoachTutorialModel(
      ids: "post2",
      globalKeys: JosKeys.josKeys2,
      icons: Icon(
        Icons.message,
        color: Colors.white,
        size: 90,
      ),
      ...
    ),
]

Conclusion:

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portal dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge on Flutter.

Leave a Reply