是否可以禁用选项卡栏中的特定选项卡?这样,除非再次启用它,否则无法单击它?任何帮助表示感谢,谢谢!
编辑:带吸收/忽略指针的代码不起作用:
class MyTabbedPage extends StatefulWidget {
const MyTabbedPage({Key key}) : super(key: key);
@override
_MyTabbedPageState createState() => new _MyTabbedPageState();
}
class _MyTabbedPageState extends State<MyTabbedPage>
with SingleTickerProviderStateMixin {
final List<Tab> myTabs = <Widget>[
Tab(text: 'LEFT'),
AbsorbPointer(
child: Tab(text: 'RIGHT')), //not working
];
TabController _tabController;
@override
void initState() {
super.initState();
_tabController = new TabController(vsync: this, length: myTabs.length);
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
bottom: new TabBar(
controller: _tabController,
tabs: myTabs,
),
),
body: new TabBarView(
controller: _tabController,
children: myTabs.map((Tab tab) {
return new Center(child: new Text(tab.text));
}).toList(),
),
);
}
}
答案 0 :(得分:0)
您在这里:
添加列表以禁用哪个选项卡
List<bool> _isDisabled = [false, true];
将听众添加到_tabController
_tabController.addListener(onTap);
onTap()
方法。如果禁用了选定的选项卡,我们将还原到上一个选定的选项卡。
onTap() {
if (_isDisabled[_tabController.index]) {
int index = _tabController.previousIndex;
setState(() {
_tabController.index = index;
});
}
}
下面是完整的代码:
import 'package:flutter/material.dart';
class MyTabbedPage extends StatefulWidget {
const MyTabbedPage({Key key}) : super(key: key);
@override
_MyTabbedPageState createState() => _MyTabbedPageState();
}
class _MyTabbedPageState extends State<MyTabbedPage>
with SingleTickerProviderStateMixin {
List<bool> _isDisabled = [false, true];
final List<Tab> myTabs = <Tab>[
Tab(text: 'LEFT'),
Tab(text: 'RIGHT'),
];
TabController _tabController;
onTap() {
if (_isDisabled[_tabController.index]) {
int index = _tabController.previousIndex;
setState(() {
_tabController.index = index;
});
}
}
@override
void initState() {
super.initState();
_tabController = TabController(vsync: this, length: myTabs.length);
_tabController.addListener(onTap);
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
bottom: TabBar(
controller: _tabController,
tabs: myTabs,
),
),
body: TabBarView(
controller: _tabController,
children: myTabs.map((Tab tab) {
return Center(child: new Text(tab.text));
}).toList(),
),
);
}
}
答案 1 :(得分:-1)
TabBar(
tabs: [
Tab(icon: Icon(Icons.directions_car)), //enabled
Tab(icon: Icon(Icons.directions_transit), onTap: null), //disabled
Tab(icon: Icon(Icons.directions_bike)),
],
),