Multiple widgets used the same GlobalKey

前端 未结 4 1967
清歌不尽
清歌不尽 2020-12-07 01:05

I\'m using Flutter. I have a simple app with 3 tabs. There is a RefreshIndicator in each tab with a ListView. The rows are built in another method. This is the code:

相关标签:
4条回答
  • 2020-12-07 01:39

    Could you create your keys "by hand" and use static/constant values? e.g. ...

    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 ...

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

    I have done this in a similar situation with some success ... especially with redux or other similar patterns ...

    0 讨论(0)
  • 2020-12-07 01:51

    Same as @babernethy above, you can have

    import 'package:flutter/widgets.dart';
    
    class JosKeys {
      static final josKeys1 = const Key('__JosKey1__');
      static final josKeys2 = const Key('__JosKey2__');
    }
    

    then do this on one of your Global Keys, make sure to have a different JosKeys.josKeys{number} for each global key

    GlobalKey _globalKey = JosKeys.josKeys1;
    
    0 讨论(0)
  • 2020-12-07 01:53

    We can solve this problem in different ways by its definition and requirement:

    Just use this way to resolve the problem

      @override
      Widget build(BuildContext context) {
          //... return
          new RefreshIndicator(key: new GlobalKey<RefreshIndicatorState>(),  ...
      }
    

    Using custom debugLabel

    Changing the key from static final to just final and adding debugLabel solved it

    class _LoginFormState extends State<yourStatefulWidget> {
      final GlobalKey<FormState> _formKey =
          new GlobalKey<FormState>(debugLabel: '_LoginFormState');
      ...
    

    Create a key with final

    Changing from static to non-static means the key is being recreated every time the widget is, so it's no longer global. This defeats its purpose.

    final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
    

    Change the key to refKey

    Suppose you using a custom widget that wrapped a Container widget. needed to pass the key from the custom widget to the container, and using a parameter key in the wrapper constructor. Ran into the same issue. fix to avoid using the word key in the wrapper constructor, changed it to refKey

    Container(
            refKey: …,
    )
    
    0 讨论(0)
  • 2020-12-07 01:56

    For those who need to continue with GlobalKey:

    import 'package:flutter/widgets.dart';
    
    class JosKeys {
      static final josKeys1 = GlobalKey();
      static final josKeys2 = GlobalKey();
    }
    

    Using as follows:

    [
        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,
          ),
          ...
        ),
    ]
    
    0 讨论(0)
提交回复
热议问题