TimeToCode
TimeToCode

Reputation: 1838

How to set same theme color on my entire app? | Flutter

I want a same theme color on my entire app

i want output like this

enter image description here

here is my code, i create this statelesswidget in my login file and passing Login (StatefulWidget) where i design my login page.

class MyApp extends StatelessWidget {
  // const MyApp({ Key? key }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: new ThemeData(
      primarySwatch: Colors.blue
    ),
    home: Login(),
    );
  }
}

and here is my main file code

void main() {
  runApp(MaterialApp(
    debugShowCheckedModeBanner: false,
    
    home: MyApp(),
  ));
}

Anyone please let me know where i am doing wrong.

here is my output

enter image description here

Upvotes: 0

Views: 1302

Answers (3)

Kalpesh Khandla
Kalpesh Khandla

Reputation: 746

For the entire app, you can set textTheme property in the Material app widget.

MaterialApp(
 theme: ThemeData(
 textTheme: TextTheme(
  bodyText1: TextStyle(),
  bodyText2: TextStyle(),
).apply(
  bodyColor: Colors.orange, 
  displayColor: Colors.blue, 
),
),
) 

Upvotes: 1

Muhammad Arbaz Zafar
Muhammad Arbaz Zafar

Reputation: 671

the best way to set the theme to make it sperate file of theme and call in main file theme: ThemeData(), like that you can also set theme of your icon and also you can set theme of your text.

 import 'package:flutter/material.dart';
ThemeData appthemedata(BuildContext context)
{
  return ThemeData(
    primaryColor:appcolor,
    // scaffoldBackgroundColor: Colors.white,
    appBarTheme: AppBarTheme(
        centerTitle: true,
        elevation: 0,
        textTheme: TextTheme(headline1: AppBarTextStyle )
    ),
    textTheme: TextTheme(

      headline1: TitleTextStyle,
      bodyText1:Body1TextStyle,
    ),
    iconTheme: IconThemeData(
      color:Colors.white,
      size: 17,
    ),
  );
}

Upvotes: 1

Jouby
Jouby

Reputation: 2471

Update your main function like :

void main() {
  runApp(new MyApp());
}

You don't have to use MaterialApp several times.

Here a quick example of working code :

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}
class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Generated App',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        appBar: new AppBar(
          title: new Text('App Name'),
          ),
      );
    }
}

Upvotes: 2

Related Questions