我一直在尝试编写一个应用程序,该应用程序可以根据所按的选项卡动态更改其主题。我正在尝试使用 DynamicTheme窗口小部件,但一直出现此错误(如图所示),我在TabBar内使用了
DynamicTheme.of(context).setThemeData(ThemeData(
primaryColor: crypto.color
));
方法,该在哪里放置函数,或者我有完全使用其他方法?
完整代码:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new DynamicTheme(
defaultBrightness: Brightness.light,
data: (brightness) => new ThemeData(
primaryColor: Colors.black
),
themedWidgetBuilder: (context,theme){
return MyHomePage();
},
);
}
}
class MyHomePage extends StatefulWidget {
final String title = "Mobile Block Explorer";
@override
_MyHomePageState createState() => _MyHomePageState();
}
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primaryColor: Colors.black),
debugShowCheckedModeBanner: false,
home: DefaultTabController(
length: crypto.length,
child: Scaffold(
appBar: AppBar(
title: Text(widget.title),
bottom: TabBar(
isScrollable: false,
indicatorColor: Colors.black,
tabs: crypto.map((Crypto crypto) {
DynamicTheme.of(context).setThemeData(ThemeData(
primaryColor: crypto.color
));
return Tab(
child: Text("${crypto.title}",
style: TextStyle(color: Colors.white),)
,
);
}).toList()
),
),
body: TabBarView(
children: crypto.map((Crypto crypto) {
return blockView(crypto.url);
}).toList()
)
)
,
),
);
}...More code