I have this code as below

import 'package:flutter/material.dart';

class MyAppTheme extends StatelessWidget {
  Widget build(BuildContext context) {
    return ThemeData(primaryColor: Colors.yellow);

I get this error

A value of type 'ThemeData' can't be returned from method 'build' because it has a return type of 'Widget'

I have tried for a while like changing the return type of build and putting it to another type for example dynamic but it doesn't work as it only expects Widget

So what can I do if I want to split my ThemeData to another file? Any ideas? You're very welcome to post your own solutions

Solution 1: Arsalan Umer

You cant return ThemeData because it is not a widget so it cant be rendered on-screen that why flutter giving you this error. Use ThemeData like this

  title: title,
  theme: ThemeData(
    // Define the default brightness and colors.
    brightness: Brightness.dark,
    primaryColor: Colors.lightBlue[800],
    accentColor: Colors.cyan[600],

    // Define the default font family.
    fontFamily: 'Georgia',

    // Define the default TextTheme. Use this to specify the default
    // text styling for headlines, titles, bodies of text, and more.
    textTheme: TextTheme(
      headline1: TextStyle(fontSize: 72.0, fontWeight: FontWeight.bold),
      headline6: TextStyle(fontSize: 36.0, fontStyle: FontStyle.italic),
      bodyText2: TextStyle(fontSize: 14.0, fontFamily: 'Hind'),

Solution 2: Nitrodon

As you correctly determined, ThemeData isn't a Widget at all. If you're trying to create a ThemeData to plug into the theme parameter of your MaterialApp or similar, you can just make a top-level variable:

final myAppTheme = ThemeData(primaryColor: Colors.yellow);

  // somewhere else
  return MaterialApp(
    theme: myAppTheme,
    // ...