# ThemData(colorScheme: xxx) vs ThemeData.from(colorScheme: xxx)?

**URL:** <https://forums.kodeco.com/t/themdata-colorscheme-xxx-vs-themedata-from-colorscheme-xxx/153238>\
**Category:** Flutter Apprentice\
**Created:** [October 15, 2021, 6:27am UTC](https://forums.kodeco.com/t/themdata-colorscheme-xxx-vs-themedata-from-colorscheme-xxx/153238 "2021-10-15T06:27:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![salmankhilji](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/salmankhilji/32/81494_2.png) [@salmankhilji](https://forums.kodeco.com/u/salmankhilji)\
**Post date:** [October 15, 2021, 6:27am UTC](https://forums.kodeco.com/t/themdata-colorscheme-xxx-vs-themedata-from-colorscheme-xxx/153238/1 "2021-10-15T06:27:31Z")

</div>

In Chapter 2, Hello, Flutter, Section, Styling your app, we theme our app like this:

```dart
@override
Widget build(BuildContext context) {
  final ThemeData theme = ThemeData();
  return MaterialApp(
    title: 'Recipe Calculator',
    theme: theme.copyWith(
        colorScheme: theme.colorScheme.copyWith(
            primary: Colors.grey,
            secondary: Colors.black,
        ),
    ),
    home: const MyHomePage(title: 'Recipe Calculator'),
  );
}

```

Not liking the creation of an intermediate variable, `theme`, I changed it as follows:

```dart
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Recipe Calculator',
      theme: ThemeData(
        colorScheme: Theme.of(context).colorScheme.copyWith(
              primary: Colors.grey,
              secondary: Colors.black,
            ),
      ),
      home: const MyHomePage(title: 'Recipe Calculator'),
    );
  }

```

So far so good—no visual differences. However, rather than using the `ThemeData()` constructor, my first attempt was instead using `ThemeData.from()`, like this:

```dart
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Recipe Calculator',
      theme: ThemeData.from(
        colorScheme: Theme.of(context).colorScheme.copyWith(
              primary: Colors.grey,
              secondary: Colors.black,
            ),
      ),
      home: const MyHomePage(title: 'Recipe Calculator'),
    );
  }

```

The results, however, are not the same. When using `ThemeData.from()`, the background color of the screen is blue, whereas when using `ThemeData()`, the background is white.

The documentation for `ThemeData.from()` states:

> Create a [ThemeData](https://api.flutter.dev/flutter/material/ThemeData-class.html) based on the colors in the given `colorScheme`…

Whereas, the documentation of `ThemeData() constructor` does not specifically mention anything about the `colorScheme` other than that Material components use it. Not getting into the documentation, one would logically guess that the constructor ought to create a `ThemeData` instance with its colors in the given `colorScheme`, just like `ThemeData.from()` does.

So I fail to understand the difference.

Can someone shed some light on when to prefer one of these constructors over the other?

---

<div class="post-metadata">

**Author:** ![jomoka](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jomoka/32/109828_2.png) [@jomoka](https://forums.kodeco.com/u/jomoka)\
**Post date:** [October 17, 2021, 6:07am UTC](https://forums.kodeco.com/t/themdata-colorscheme-xxx-vs-themedata-from-colorscheme-xxx/153238/2 "2021-10-17T06:07:00Z")

</div>

@salmankhilji thanks for the interesting question.

By diving into the ThemeData documentation and implementation here is the reason why the background color is blue vs white in some cases.

## Using ThemeData Constructor

When you used the ThemeData constructor:

```auto
ThemeData(
        colorScheme: Theme.of(context).colorScheme.copyWith(
              primary: Colors.grey,
              secondary: Colors.black,
            ),
      ),

```

By default `scaffoldBackgroundColor` takes the canvasColor as shown below:

 ![Screen Shot 2021-10-17 at 1.51.47 PM](https://files.chunter.kodeco.com/original/3X/c/f/cf6f9d155034d29424626f66da63c386bdcce2fc.png)

This is why the background color is set to grey[50] (Almost white looking)

## Using ThemeData.from

In the case when you changed to use the `ThemeData.from`

```auto
ThemeData.from(
        colorScheme: Theme.of(context).colorScheme.copyWith(
              primary: Colors.grey,
              secondary: Colors.black,
            ),
      ),

```

What you did here was:

1. Take the existing colorScheme from the context, and create a new copy but **only** set primary and secondary color.

The reason why the background is light blue in this case is because the default background color uses primarySwatch which is set to blue by default. You will have to update the colorScheme’s background color in this case.

Here is a screenshot of the ThemeData constructor:

 ![Screen Shot 2021-10-17 at 1.50.09 PM](https://files.chunter.kodeco.com/original/3X/4/3/43b9409423947618aa6e38c6e494b3f5ca06996f.png)

You will notice that the default colorScheme’s backgroundColor is also set to blue.

When you look at the ThemeData.from constructor, you will see it uses `colorScheme.background` to set the scaffoldBackgroundCOlor

 ![Screen Shot 2021-10-17 at 2.09.35 PM](https://files.chunter.kodeco.com/original/3X/8/a/8a8920a39d2c882563776f7acea3f69310cd249c.png)

You can check out the entire ThemeData constructor implementation here:

> **[ThemeData.new constructor - ThemeData - material library - Dart API](https://api.flutter.dev/flutter/material/ThemeData/ThemeData.html)**
>
> API docs for the ThemeData.new constructor from Class ThemeData from the material library, for the Dart programming language.

Hope this is helpful!

---

<div class="post-metadata">

**Author:** ![cormier](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/cormier/32/81494_2.png) [@cormier](https://forums.kodeco.com/u/cormier)\
**Post date:** [October 26, 2021, 5:17am UTC](https://forums.kodeco.com/t/themdata-colorscheme-xxx-vs-themedata-from-colorscheme-xxx/153238/3 "2021-10-26T05:17:01Z")

</div>

Thanks for theme Colour data solution.

[myccpay](https://www.myccpay.tech/)
