我正在使用Flutter构建一个具有TabBar的应用程序,该TabBar用于按类别过滤列表视图。但是,启动TabBar时会引发以下错误:
flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following NoSuchMethodError was thrown building _TabStyle(animation: kAlwaysDismissedAnimation,
flutter: dirty, state: _AnimatedState#71498):
flutter: The method 'withAlpha' was called on null.
flutter: Receiver: null
flutter: Tried calling: withAlpha(178).....
代码最初运行良好。但是,模拟器现在根本不再渲染tapBar。相反,模拟器出现错误,指出底部溢出了99870像素
class LocationListWidget extends StatefulWidget {
final List<Location> listOfLocations;
@override
State<StatefulWidget> createState() => new LocationListView();
LocationListWidget(this.listOfLocations);
}
class LocationListView extends State<LocationListWidget>
with SingleTickerProviderStateMixin {
TabController _controller;
static const List<TopButtons> typeList = const <TopButtons>[
const TopButtons(title: "Places", icon: Icons.directions_walk),
const TopButtons(title: "Shop", icon: Icons.shop),
const TopButtons(title: "Vineyards", icon: Icons.local_drink),
const TopButtons(title: "Dining", icon: Icons.local_dining),
const TopButtons(title: "Cafes", icon: Icons.local_cafe),
const TopButtons(
title: "Stay",
icon: Icons.home,
)
];
List<Location> listOfLocations;
List<Location> fliteredlistOfLocations;
@override
void initState() {
super.initState();
listOfLocations = this.widget.listOfLocations;
fliteredlistOfLocations = new List();
_controller = new TabController(length: 5, vsync: this, initialIndex: 1);
_controller.addListener(updateList);
updateList();
}
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Location"),
bottom:
TabBar(controller: _controller, isScrollable: true, tabs: <Tab>[
Tab(text: typeList[0].title, icon: Icon(typeList[0].icon)),
Tab(text: typeList[1].title, icon: Icon(typeList[1].icon)),
Tab(text: typeList[2].title, icon: Icon(typeList[2].icon)),
Tab(text: typeList[3].title, icon: Icon(typeList[3].icon)),
Tab(text: typeList[4].title, icon: Icon(typeList[4].icon)),
]),
),
body: SafeArea(
child: ListView.builder(
itemExtent: 100.0,
padding: const EdgeInsets.all(10.0),
itemCount: fliteredlistOfLocations.length,
itemBuilder: (BuildContext ctxt, int index) =>
buildBody(ctxt, index))));
}
答案 0 :(得分:1)
我遇到了一个非常相似的问题。这是我的解决方法:
我在unselectedLabelColor: Colors.red,
小部件中添加了TabBar
属性。这停止了错误,并且选项卡按预期工作。
就我而言,我已将问题追溯到ThemeData
。我为我的应用实现了自定义主题,这就是导致此错误的原因。我已经评论了我的自定义主题,并且一切正常,没有任何错误,也不需要添加unselectedLabelColor
。
这是我的最终代码:
import 'package:flutter/material.dart'; class AgendaScreen extends StatefulWidget { @override _AgendaScreenState createState() => _AgendaScreenState(); } class _AgendaScreenState extends State with SingleTickerProviderStateMixin { TabController _tabController; @override void initState() { super.initState(); _tabController = TabController(length: 2, vsync: this); } @override Widget build(BuildContext context) => Scaffold( backgroundColor: Colors.white, appBar: AppBar( backgroundColor: Theme.of(context).primaryColor, title: Text('Agenda', style: Theme.of(context).textTheme.title), ), body: Column( children: [ Container( child: TabBar( controller: _tabController, indicatorColor: Colors.red, unselectedLabelColor: Colors.red, tabs: [ Tab( child: Text( 'Audi 01', style: TextStyle(color: Colors.black), ), ), Tab( child: Text( 'Audi 02', style: TextStyle(color: Colors.black), ), ), ], ), ), Expanded( child: TabBarView( controller: _tabController, children: [ ListView( children: [ Row( children: [], ) ], ), Icon(Icons.directions_transit) ], ), ), ], ), ); }