1

別のウィジェット内の 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.

4

1 に答える 1