繁体   English   中英

Flutter Tabbar NoSuchMethodError: NoSuchMethodError

[英]Flutter Tabbar NoSuchMethodError: NoSuchMethodError

我正在使用 Flutter 构建一个应用程序,它有一个 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))));
  }

我遇到了一个非常相似的问题。 这是我修复它的方法:

我在TabBar小部件中添加了unselectedLabelColor: Colors.red,属性。 这停止了​​错误并且选项卡按预期工作。

就我而言,我已将问题追溯到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)
                    ],
                  ),
                ),
              ],
            ),
          );
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM