如何禁用选项卡中的特定选项卡才能单击? [英] How can i disable a particular tab in tabbar, to be able to click?
本文介绍了如何禁用选项卡中的特定选项卡才能单击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以禁用选项卡栏中的特定选项卡?这样,除非再次启用它,否则无法单击它?感谢任何帮助,谢谢!
is there a way to disable a particular tab in the tabbar? so that it cannot be clicked unless it is enabled again? any help is appreciated, thanks!
编辑:使用吸收/忽略指针的代码不起作用:
Code with Absorb/Ignore Pointer not working:
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(),
),
);
}
}
推荐答案
此处你去:
添加列表以禁用哪个选项卡
Add list to make which tab is disabled
List<bool> _isDisabled = [false, true];
将监听器添加到 _tabController
_tabController.addListener(onTap);
onTap()
方法。如果禁用了选定的选项卡,我们将返回到先前选定的选项卡。
onTap()
method. If selected tab is disabled we will revert to previous selected tab.
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(),
),
);
}
}
这篇关于如何禁用选项卡中的特定选项卡才能单击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文