TabController侦听器多次调用。 indexIsChanging如何工作?

时间:2020-02-16 19:28:29

标签: flutter dart

我需要知道单击了哪个选项卡。因此,我添加了SingleTickerProviderStateMixin,在我的州创建了一个TabController字段,并添加了一个侦听器(巨大的样板IMHO ...)。

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
  TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = new TabController(length: 2, vsync: this);
    _tabController.addListener(() {
      if (_tabController.indexIsChanging) {
        print('click, ${_tabController.index}');
      }
    });
  }

  @override
  Widget build(BuildContext context) {
//...
}
}

但是,每次我单击一个选项卡时,都会打印多个语句,而不是我期望的那样。为什么indexIsChanging无法正常工作?

3 个答案:

答案 0 :(得分:3)

原因来自https://github.com/flutter/flutter/issues/13848

TabController的源代码,notifyListeners()调用两次https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/material/tab_controller.dart#L162

if (duration != null) {
      _indexIsChangingCount += 1;
      notifyListeners(); // Because the value of indexIsChanging may have changed.
      _animationController
        .animateTo(_index.toDouble(), duration: duration, curve: curve)
        .whenCompleteOrCancel(() {
          _indexIsChangingCount -= 1;
          notifyListeners();
        });

解决方案:仅打印一次当前索引
https://github.com/flutter/flutter/issues/13848#issuecomment-486051402

中的代码段
@override
  void initState() {
    super.initState();
    _tabController = TabController(vsync: this, length: choices.length)
      ..addListener(() {
        if(_tabController.indexIsChanging) {
          print("tab is animating. from active (getting the index) to inactive(getting the index) ");
        }else {
          //tab is finished animating you get the current index
          //here you can get your index or run some method once.
          print(_tabController.index);
        }
      });
  }

工作演示

enter image description here

完整的测试代码

import 'package:flutter/material.dart';

class AppBarBottomSample extends StatefulWidget {
  @override
  _AppBarBottomSampleState createState() => _AppBarBottomSampleState();
}

class _AppBarBottomSampleState extends State<AppBarBottomSample>
    with SingleTickerProviderStateMixin {
  TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(vsync: this, length: choices.length)
      ..addListener(() {
        if(_tabController.indexIsChanging) {
          print("tab is animating. from active (getting the index) to inactive(getting the index) ");
        }else {
          //tab is finished animating you get the current index
          //here you can get your index or run some method once.
          print(_tabController.index);
        }
      });
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  void _nextPage(int delta) {
    final int newIndex = _tabController.index + delta;
    if (newIndex < 0 || newIndex >= _tabController.length) return;
    _tabController.animateTo(newIndex);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('AppBar Bottom Widget'),
          leading: IconButton(
            tooltip: 'Previous choice',
            icon: const Icon(Icons.arrow_back),
            onPressed: () {
              _nextPage(-1);
            },
          ),
          actions: <Widget>[
            IconButton(
              icon: const Icon(Icons.arrow_forward),
              tooltip: 'Next choice',
              onPressed: () {
                _nextPage(1);
              },
            ),
          ],
          bottom: PreferredSize(
            preferredSize: const Size.fromHeight(48.0),
            child: Theme(
              data: Theme.of(context).copyWith(accentColor: Colors.white),
              child: Container(
                height: 48.0,
                alignment: Alignment.center,
                child: TabPageSelector(controller: _tabController),
              ),
            ),
          ),
        ),
        body: TabBarView(
          controller: _tabController,
          children: choices.map((Choice choice) {
            return Padding(
              padding: const EdgeInsets.all(16.0),
              child: ChoiceCard(choice: choice),
            );
          }).toList(),
        ),
      ),
    );
  }
}

class Choice {
  const Choice({this.title, this.icon});

  final String title;
  final IconData icon;
}

const List<Choice> choices = const <Choice>[
  const Choice(title: 'CAR', icon: Icons.directions_car),
  const Choice(title: 'BICYCLE', icon: Icons.directions_bike),
  const Choice(title: 'BOAT', icon: Icons.directions_boat),
  const Choice(title: 'BUS', icon: Icons.directions_bus),
  const Choice(title: 'TRAIN', icon: Icons.directions_railway),
  const Choice(title: 'WALK', icon: Icons.directions_walk),
];

class ChoiceCard extends StatelessWidget {
  const ChoiceCard({Key key, this.choice}) : super(key: key);

  final Choice choice;

  @override
  Widget build(BuildContext context) {
    final TextStyle textStyle = Theme.of(context).textTheme.display1;
    return Card(
      color: Colors.white,
      child: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Icon(choice.icon, size: 128.0, color: textStyle.color),
            Text(choice.title, style: textStyle),
          ],
        ),
      ),
    );
  }
}

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

答案 1 :(得分:0)

我有一个简单的解决方案,定义一个布尔值来检测 Tabbar onTap(不是滑动)和一个计数变量

TabBar(
  indicatorColor: Colors.white,
  controller: _tabController,
  onTap: (index) {
     isTabOnTap = true;
  },
  tabs: [
     ...
  ],
),

_tabController.addListener(() {
  if (isTabOnTap) {
    ++a;
    if (a%2 == 0) {
      isTabOnTap = false;
      setState(() {
        selectedPage = _tabController.index;
      });
    }
  } else {
    setState(() {
      selectedPage = _tabController.index;
    });
  }
});

答案 2 :(得分:0)

为了防止调用一个方法两次, 你可以添加这个条件,

if(!controller.indexIsChanging){ //perform your operation }