我使用底部导航栏,内部使用标签栏。但是选项卡栏具有交换行为。我想禁用交换行为。因此,我使用了NeverScrollableScrollPhysics
,但这显示了一个错误。错误为Multiple widgets used the same GlobalKey
。我为每个选项卡栏视图项目提供了不同的键,但是出现了相同的问题。
这是我的代码:
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),
),
],
),
),
);
}
这是错误:
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.
答案 0 :(得分:0)
我尝试运行您提供的代码段。但是,我没有遇到您发布的任何错误。似乎GitHub thread共享已解决此问题。
这是我的flutter doctor
日志
[✓] 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
代码运行没有问题。如前所述,页面滑动功能已被禁用,并且当页面转换到另一个页面时,仍然存在“交换”动画。
关于页面动画。我认为没有办法禁用TabBarView
上的过渡动画。解决方法是,您可以使用Container
来返回不同的页面,具体取决于所选的标签。我已替换了您在此示例中使用的即兴出现在页面上的TabBarView
。
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"),
),
);
}
演示