多个小部件使用相同的GlobalKey。扑

时间:2019-06-19 16:34:52

标签: flutter dart flutter-navigation

我想使用另一个小部件内的Button来点击TitledBottomNavigationBar项。
我只使用导航栏中的键,它仍然说我正在使用它Multiple widgets used the same GlobalKey.

import 'package:flutter/material.dart';
import 'package:titled_navigation_bar/titled_navigation_bar.dart';

class TesterPage extends StatefulWidget {
  @override
  _TesterPageState createState() => _TesterPageState();
}

class _TesterPageState extends State<TesterPage> {
  final GlobalKey _navigationBarKey = GlobalKey();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: TitledBottomNavigationBar(
        key: _navigationBarKey,
        onTap: (int index) {
          print(index.toString());
        },
        items: [
          TitledNavigationBarItem(title: 'Home', icon: Icons.home),
          TitledNavigationBarItem(title: 'Help', icon: Icons.help),
          TitledNavigationBarItem(title: 'Account', icon: Icons.person),
        ],
      ),
      body: Center(
        child: RaisedButton(
          child: Text("Test"),
          onPressed: () {
            final TitledBottomNavigationBar navigationBar = _navigationBarKey.currentWidget;
            navigationBar.onTap(1);
          },
        ),
      ),
    );
  }
}

它引发错误:

I/flutter ( 3573): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 3573): The following assertion was thrown building
I/flutter ( 3573): TitledBottomNavigationBar-[GlobalKey#03b8b](dependencies: [_InheritedTheme,
I/flutter ( 3573): _LocalizationsScope-[GlobalKey#4d232], MediaQuery], state: _TitledBottomNavigationBarState#4cc2f):
I/flutter ( 3573): Multiple widgets used the same GlobalKey.
I/flutter ( 3573): The key [GlobalKey#03b8b] was used by multiple widgets. The parents of those widgets were:
I/flutter ( 3573): - MediaQuery(MediaQueryData(size: Size(411.4, 868.6), devicePixelRatio: 3.5, textScaleFactor: 0.9,
I/flutter ( 3573):   platformBrightness: Brightness.dark, padding: EdgeInsets.zero, viewInsets: EdgeInsets.zero,
I/flutter ( 3573):   alwaysUse24HourFormat: false, accessibleNavigation: falsedisableAnimations: falseinvertColors:
I/flutter ( 3573):   falseboldText: false))
I/flutter ( 3573): - TitledBottomNavigationBar-[GlobalKey#03b8b](dependencies: [_InheritedTheme,
I/flutter ( 3573):   _LocalizationsScope-[GlobalKey#4d232], MediaQuery], state: _TitledBottomNavigationBarState#4cc2f)
I/flutter ( 3573): A GlobalKey can only be specified on one widget at a time in the widget tree.

0 个答案:

没有答案
相关问题