Flutter Tabs 抛出空指针异常 -> Material.of(context).color 计算结果为 null



我正在按照以下示例explesiona_panels_demo.dart在此处找到的flutter画廊中提供的。我的目标是在扩展面板主体中引入塔栏和塔布维尤。截至目前,这正在破裂,因为在Tabs.dart中抛出了无效的指针例外。这在_tabbarstate内部,如果要查看我要在第251行中调用新的booktabs((的演示,它正在与widget.indicatorColor进行比较。

if (color.value == Material.of(context).color.value)

shere材料of(context(。彩色为null

要合并代码,我将标签放在一个状态的小部件中。当我将小部件放在图库示例中定义的可折叠机构小部件中时,就将 * getter" value"丢弃在null *上。目前,我对如何解决这个问题感到茫然。任何建议将不胜感激。谢谢!

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
class BookTabs extends StatefulWidget {
   @override
  _BookTabs createState() => new _BookTabs();
}
class _BookTabs extends State<BookTabs> with 
SingleTickerProviderStateMixin {
TabController _tabController;
@override
void initState() {
 super.initState();
 _tabController = new TabController(vsync: this, length: 2);
}
@override
Widget build(BuildContext context) {
 return new Column(
   children: <Widget>[
    new TabBar(
        controller: _tabController,
        indicatorColor: Colors.blue,
        labelStyle: new TextStyle(color: Colors.blue),
        labelColor: Colors.blue,
        unselectedLabelColor: Colors.indigo,             
        tabs: [
          new Tab(text: 'Books'),
          new Tab(text: 'Authors')
        ]
    ),
    new Expanded(
        child: new TabBarView(
            controller: _tabController,
            children: [
              new SafeArea(
                  top: false,
                  bottom: false,
                  child:
                  new Text("Books", style: Theme
                      .of(context)
                      .textTheme
                      .subhead)),
              new SafeArea(
                  top: false,
                  bottom: false,
                  child: new Text("Authors", style: Theme
                      .of(context)
                      .textTheme
                      .subhead))
            ])
    )
   ],
  );
 }
}

flutter医生:

[✓] Flutter (Channel dev, v0.2.6, on Mac OS X 10.13.3 17D102, locale en-US)
• Flutter version 0.2.6 at /Users/minime/flutter
• Framework revision 1d067220da (4 days ago), 2018-03-29 22:14:04 -0700
• Engine revision 9af82c5a1a
• Dart version 2.0.0-dev.43.0.flutter-e305117519

我认为您不使用MaterialApp作为BookTabs的父

如果您不想要MaterialApp,则至少需要将其包装在Material

我使用Scaffold.of(context)遇到了类似的问题,并通过将我所有的小部件包装在Builder包装器下。

检查BuildContext关于如何做的文档,当您需要有效的上下文时,这是要走的方法。

将小部件包装在材料类中修复了问题。

new Material(color: Theme
                        .of(context)
                        .dialogBackgroundColor, child: new BookTabs()
                    )

相关内容

最新更新