flutter 我怎样才能禁用一个特定的标签在标签栏,能够点击?

ozxc1zmp  于 2023-10-22  发布在  Flutter
关注(0)|答案(3)|浏览(146)

是否有一种方法来禁用一个特定的标签在标签栏?所以除非再次启用它,否则无法单击它?任何帮助是感激的,谢谢!
编辑:吸收/忽略指针不工作的代码:

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(),
      ),
    );
  }
}
gr8qqesn

gr8qqesn1#

给您:
添加列表以使哪个选项卡被禁用

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(),
      ),
    );
  }
}
1sbrub3j

1sbrub3j2#

它对我起作用

TabBar(
onTap: (value) {
       if ( (_tabController?.indexIsChanging ?? false) &&
              _tabController?.index == disableIndex) {
            _tabController?.index = _tabController?.previousIndex ?? 1;
          }
        })
trnvg8h3

trnvg8h33#

TabBar(
          tabs: [
            Tab(icon: Icon(Icons.directions_car)), //enabled
            Tab(icon: Icon(Icons.directions_transit), onTap: null), //disabled
            Tab(icon: Icon(Icons.directions_bike)),
          ],
        ),

相关问题