【问题标题】:Flutter: How to Fix a 'Multiple Widgets Used the Same GlobalKey' ErrorFlutter:如何修复“多个小部件使用相同的 GlobalKey”错误
【发布时间】:2021-10-13 03:44:18
【问题描述】:

问题:

当我遇到这个问题时,我正在寻找包含在按下的小部件中的单词的中间位置。

我收到错误,多个小部件使用相同的 GlobalKey,同时使用下面的最小可行代码。

我无法解决这个问题。我正在注入 GlobalKeys,它应该是独一无二的,我希望它们彼此不同。代码确实有效,但我找不到解决方案有点令人沮丧。

我尝试过的:

我已经尝试了之前在 StackOverflow 上发布的问题的几个解决方案,但无济于事,包括使用具有唯一标识符的 Globalkeys 的不同变体以及使用静态 Globalkeys 变量创建一个类:

How to Fix a 'Multiple Widgets used the same Globalkey resource 1

How to Fix a 'Multiple Widgets used the same Globalkey resource 2

How to Fix a 'Multiple Widgets used the same Globalkey resource 3

import 'package:flutter/material.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Home(),
    );
  }
}

class Home extends StatelessWidget {
  final scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: scaffoldKey,
      body: SafeArea(
        child: Row(
          mainAxisSize: MainAxisSize.max,
          children: [LeftSideWidget()],
        ),
      ),
    );
  }
}

class DisplayCircle extends StatelessWidget {
  final Color color;

  DisplayCircle({Key? key, required this.color}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 120,
      height: 120,
      clipBehavior: Clip.antiAlias,
      decoration: BoxDecoration(
        shape: BoxShape.circle,
      ),
      child: CircleAvatar(
        backgroundColor: color,
      ),
    );
  }
}

class LeftSideWidget extends StatefulWidget {
  const LeftSideWidget({
    Key? key,
  }) : super(key: key);

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

class _LeftSideWidgetState extends State<LeftSideWidget> {
  double thumbWidth = 70.0;
  var position = 0.0;

  changePosition(GlobalKey key) {
    final RenderObject? object = key.currentContext!.findRenderObject();
    final RenderBox renderBox = object as RenderBox;
    final size = object.semanticBounds;
    position = renderBox.localToGlobal(Offset.zero).dy;
    setState(() {
      position = (size.center.dy + position - (thumbWidth / 2));
      print(position.toString());
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.max,
      children: [
        Spacer(),
        WordsToPress(
          title: 'TEST LINE 1',
          key: GlobalKey(),
          onPressed: (GlobalKey key) => changePosition(key),
        ),
        Spacer(),
        WordsToPress(
          title: 'TEST LINE 2',
          key: GlobalKey(),
          onPressed: (GlobalKey key) => changePosition(key),
        ),
        Spacer(),
        WordsToPress(
          title: 'TEST LINE 3',
          key: GlobalKey(),
          onPressed: (GlobalKey key) => changePosition(key),
        ),
        Spacer(),
      ],
    );
  }
}

class WordsToPress extends StatelessWidget {
  final String title;
  final GlobalKey key;

  final Function(GlobalKey) onPressed;

  const WordsToPress(
      {required this.title, required this.key, required this.onPressed})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTapUp: (_) => onPressed(key),
      child: Container(
        key: key,
        child: RotatedBox(
          quarterTurns: -1,
          child: Text(
            title,
          ),
        ),
      ),
    );
  }
}

问题: 如何正确使用 Globalkeys 以确保每个小部件只有一个?任何帮助将不胜感激。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    在每个实例中,您对名为 WordsToPress 的 Widget 和其中的 Container 使用相同的键。

    【讨论】:

    • 我不确定你的意思?我正在向 WordsToPress 的构造函数注入一个新的 Globalkey(),该构造函数用于实例化小部件。该小部件还有一个将全局键传递给它的回调方法。我不确定这是如何导致在两个或多个不同的小部件中使用相同的全局键。我希望对此有所了解。
    • 没错,WordsToPress 正在注入新的 Globalkey,但注入的 GlobalKey 也用于 WordsToPress 小部件的构建方法中的 Container(这也是一个小部件)。首次使用 Globalkey: const WordsToPress( {required this.title, required this.key, required this.onPressed}) : super(key: key); Globalkey的第二次使用:Container(key:key,child:RotatedBox(QuarterTurns:-1,child:Text(title,),),
    • 非常感谢。出于某种原因,我原以为它只会在容器小部件中使用。事后看来,这对我来说是完全清楚的。我很感激。
    猜你喜欢
    • 2019-05-27
    • 2020-10-01
    • 2023-03-04
    • 2020-10-21
    • 1970-01-01
    • 2020-12-31
    • 2018-09-26
    • 1970-01-01
    • 2019-06-07
    相关资源
    最近更新 更多