刚学flutter,对TabController怎么用很困惑,按照官网描述的,但是出现了错误,不知道怎么解决。
我只想在更改选项卡时更改标题和应用栏的前导。
final List<ChangeTitleAndLeading> _data = [
new ChangeTitleAndLeading(title: "Home", leading: Icon(Icons.home)),
new ChangeTitleAndLeading(title: "Profile", leading: Icon(Icons.person)),
new ChangeTitleAndLeading(title: "Friends", leading: Icon(Icons.people))
];
ChangeTitleAndLeading _handler;
TabController _controller;
@override
void initState() {
super.initState();
_checkEmailVerification();
_controller = TabController(vsync: this, length: 3);
_handler = _data[0];
_controller.addListener(_handleSelected);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
void _handleSelected() {
setState(() {
_handler = _data[_controller.index];
});
}
return MaterialApp(
theme: new ThemeData(
primarySwatch: Colors.teal,
),
home: new Scaffold(
appBar: new AppBar(
leading: Icon(Icons.home),
title: new Text("Home"),
bottom: new TabBar(
controller: _controller,
tabs: _tabs,
),
),
body: TabBarView(
controller: _controller,
children: _pages,
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
print('Current Index: ${_handler.title}');
}
),
class ChangeTitleAndLeading {
final String title;
final Widget leading;
ChangeTitleAndLeading({
@required this.title,
@required this.leading
}) :
assert(title != null),
assert(leading != null);
}
错误日志:
错误日志:I/flutter (19638):TabBarView 没有 TabController。I/flutter (19638):创建 TabBarView 时,必须使用“控制器”I/flutter (19638): 属性提供显式 TabController,或者必须确保 TabBarView 上方有 DefaultTabController。I/flutter (19638):在这种情况下,既没有显式控制器也没有默认控制器。══════════════════════════════════════════════════ ══════════════════════════════════════════════════
I/flutter (19638):引发了另一个异常:TabBar 没有 TabController。
当我改变 this:
leading: Icon(Icons.home),
to leading: _handler.leading,
and this:
title: new Text("Home"),
to title: new Text(_handler.title),
always return error _handler.leading
or _handler.title
was null