最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to set the theme for a Flutter app taking the primary color from a Provider store - Stack Overflow

programmeradmin1浏览0评论

I have the following code in Flutter:

void main() {
  runApp(
    ChangeNotifierProvider(
      create: (context) => MyStore(),
      child: MaterialApp(
        theme: primaryTheme(Provider.of<MyStore>(context).appColors.primaryColor),
        home: NavigationBarApp()
      ),
    )
  );
}

The store MyStore contains the primary color to use as the seed for the theme so it can be changed at any time by the user, but I get an error because the context is not in scope the way I'm attempting to use it.

how can I take the content of the context in this scope?

I have the following code in Flutter:

void main() {
  runApp(
    ChangeNotifierProvider(
      create: (context) => MyStore(),
      child: MaterialApp(
        theme: primaryTheme(Provider.of<MyStore>(context).appColors.primaryColor),
        home: NavigationBarApp()
      ),
    )
  );
}

The store MyStore contains the primary color to use as the seed for the theme so it can be changed at any time by the user, but I get an error because the context is not in scope the way I'm attempting to use it.

how can I take the content of the context in this scope?

Share Improve this question asked Feb 6 at 23:58 HuLu ViCaHuLu ViCa 5,45212 gold badges50 silver badges113 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Wrap your MaterialApp with a builder like this:

void main() {
  runApp(
    ChangeNotifierProvider(
      create: (context) => MyStore(),
      child: Builder(
      builder: (context)=> MaterialApp(
        theme: primaryTheme(Provider.of<MyStore>(context).appColors.primaryColor),
        home: NavigationBarApp()
      ),),
    )
  );
}
发布评论

评论列表(0)

  1. 暂无评论