Home > Net >  darkTheme is not changing through primaryColor it stays default
darkTheme is not changing through primaryColor it stays default

Time:12-15

I've a question about changing theme of a flutter app, when I need to change the theme to dark mode, the appbar will be default while I set a color using primaryColor. the primaryColor is working perfectly in light theme and changing the app bar but in dark theme it doesn't change.

I used this

main.dart

import 'package:flutter/material.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: Themes.light,
      darkTheme: Themes.dark,
      themeMode: ThemeMode.dark,
      home: HomePage(),
    );
  }
}

Theme.dart

import 'package:flutter/material.dart';

const Color bluishClr = Color(0xFF4e5ae8);
const Color yellowClr = Color(0xFFFFB746);
const Color pinkClr = Color(0xFFFF4667);
const Color white = Colors.white;
const primaryClr = bluishClr;
const Color darkGreyClr = Color(0xFF121212);
Color darkHeaderClr = Color(0xFF424242);

class Themes {
  static final light = ThemeData(
    primaryColor: primaryClr,
    brightness: Brightness.light,
  );

  static final dark = ThemeData(
    primaryColor: darkGreyClr,
    brightness: Brightness.dark,
  );
}

This the result when changing it to dark theme

CodePudding user response:

You can use the default dark theme by

theme: ThemeData.dark()

You can also customize the dark theme through_

theme: ThemeData.dark().copyWith(
          primaryColor: Color(0xFF0A0E21),
          scaffoldBackgroundColor: Color(0xFF0A0E21),
          accentColor: Colors.purple,
          textTheme: TextTheme(
              bodyText2: TextStyle(color: Colors.white)
          )
      )

CodePudding user response:

Use this instead to set dark-theme globally:

theme: ThemeData.dark()
  • Related