带有标签的Flutter动态主题

时间:2019-11-02 04:14:46

标签: android flutter material-design themes

我一直在尝试编写一个应用程序,该应用程序可以根据所按的选项卡动态更改其主题。我正在尝试使用*DynamicTheme Error* 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

0 个答案:

没有答案