简体   繁体   中英

How to fix Multiple widgets used the same GlobalKey?

I am using bottom navigation bar and inside I am using tab bar. But the tab bar have swapping behavior. I want to disable swapping behavior. So, I used NeverScrollableScrollPhysics But this showing an error. The error is Multiple widgets used the same GlobalKey . I gave different keys for each tab bar view item but the same problem comes. Here is my code:

Widget build(BuildContext context) {
return new Scaffold(
  key: _homeScaffoldKey,
  body: new TabBarView(
    physics: NeverScrollableScrollPhysics(),
    children: <Widget>[
      new page1(),
      new page2(),
      new page3(),
      new page4(),
    ],
    controller: tabController,
  ),

  bottomNavigationBar: new Material(
    color: Colors.blue,
    child: new TabBar(
      isScrollable: true,
      indicatorColor: Color.fromRGBO(255, 25, 255, 0.0),
      controller: tabController,
      tabs: <Widget>[
        new Tab(
          icon: new Icon(Icons.accessibility),
        ),
        new Tab(
          icon: new Icon(Icons.accessibility),
        ),
        new Tab(
          icon: new Icon(Icons.accessibility),
        ),
        new Tab(
          new Icon(Icons.accessibility),
        ),
      ],
    ),
  ),

);
}

Here is error:

I/flutter (26947): Another exception was thrown: NoSuchMethodError: The method 'dispose' was called on null.
I/flutter (26947): Another exception was thrown: Multiple widgets used the same GlobalKey.
I/flutter (26947): Another exception was thrown: Multiple widgets used the same GlobalKey.
I/flutter (26947): Another exception was thrown: NoSuchMethodError: The method 'dispose' was called on null.

I tried running the code snippet you've provided. However, I didn't encounter any errors from the one you've posted. This seems to have been resolved per the GitHub thread shared.

Here's my flutter doctor logs

[✓] Flutter (Channel stable, 1.22.2, on Mac OS X 10.15.7 19H2, locale en-PH)
    • Flutter version 1.22.2
    • Framework revision 84f3d28555 (8 days ago), 2020-10-15 16:26:19 -0700
    • Engine revision b8752bbfff
    • Dart version 2.10.2

The code runs without issues. Swiping of pages is disabled as you've mentioned, and there's still a "swapping" animation when the pages transitions to a different page.

As for the page animations. I don't think there's a way to disable the transition animation on TabBarView . As a workaround, you can use a Container that'll return different pages depending on the tab selected. I've replaced the TabBarView that you've used in this sample and improvised on the pages.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  @override
  void initState() {
    super.initState();
    tabController = TabController(length: 4, vsync: this);
  }

  var _homeScaffoldKey = Key("Scaffold Key");
  var tabController;
  var currentPage = 0;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      key: _homeScaffoldKey,
      body: _getCustomContainer(),
      bottomNavigationBar: new Material(
        color: Colors.blue,
        child: new TabBar(
          isScrollable: true,
          indicatorColor: Color.fromRGBO(255, 25, 255, 0.0),
          controller: tabController,
          onTap: (value) {
            setState(() {
              currentPage = value;
            });
          },
          tabs: <Widget>[
            new Tab(
              icon: new Icon(Icons.accessibility),
            ),
            new Tab(
              icon: new Icon(Icons.accessibility),
            ),
            new Tab(
              icon: new Icon(Icons.accessibility),
            ),
            new Tab(
              icon: new Icon(Icons.accessibility),
            ),
          ],
        ),
      ),
    );
  }

  _getCustomContainer() {
    switch (currentPage) {
      case 0:
        return page1();
      case 1:
        return page2();
      case 2:
        return page3();
      case 3:
        return page4();
    }
  }

  page1() => Container(
        color: Colors.redAccent,
        child: Center(
          child: Text("Page 1"),
        ),
      );
  page2() => Container(
        color: Colors.greenAccent,
        child: Center(
          child: Text("Page 2"),
        ),
      );
  page3() => Container(
        color: Colors.blueAccent,
        child: Center(
          child: Text("Page 3"),
        ),
      );
  page4() => Container(
        color: Colors.yellowAccent,
        child: Center(
          child: Text("Page 4"),
        ),
      );
}

Demo

演示

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM