I wanted to change the primary color of my Flutter app from theme data, but it is not applying to the AppBar.
I began using primarySwatch, but after reading that primarySwatch is being shut down, I used ColorScheme.fromSeed(), yet that did not work either. Here is the code for the main app:
import 'package:flutter/material.dart';
import 'package:to_do/home_page.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.yellow),
),
);
}
}
The HomePage() app’s code is as follows:
import 'package:flutter/material.dart';
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.yellow[200],
appBar: AppBar(),
);
}
}
I have not given the appbar a manual color, as I expected it to change automatically based on theme, but that is not working and it stays white.
I would appreciate it if anyone had any ideas.
Thanks.
1
To use an automatic background color from the ThemeData, provide surface:Colors.green within the ColorScheme, as shown below
ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor:Colors.blue,
surface:Colors.green,
),
);
This will automatically pick a green background color for your AppBar.
0