Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:provider/provider.dart';
- import 'pages/home_page.dart'; // Import the HomePage
- import 'theme_provider.dart'; // Import the ThemeProvider
- void main() {
- runApp(
- ChangeNotifierProvider<ThemeProvider>(
- create: (_) => ThemeProvider()..initialize(), // Initialize ThemeProvider
- child: const MyApp(),
- ),
- );
- }
- class MyApp extends StatelessWidget {
- const MyApp({super.key});
- @override
- Widget build(BuildContext context) {
- return Consumer<ThemeProvider>(builder: (context, provider, child) {
- return MaterialApp(
- debugShowCheckedModeBanner: false,
- theme: ThemeData.light(useMaterial3: true)
- .copyWith(colorScheme: provider.seedColor),
- darkTheme: ThemeData.dark(useMaterial3: true)
- .copyWith(colorScheme: provider.seedColor),
- themeMode: provider.themeMode, // Use the current theme mode
- home: HomePage(),
- );
- });
- }
- }
- //main.dart^^^
- ---------------------------------------------------------------------------------------------------------------------
- import 'package:flutter/material.dart';
- import 'package:provider/provider.dart';
- import '../theme_provider.dart'; // Import the ThemeProvider
- class SettingsPage extends StatefulWidget {
- const SettingsPage({super.key});
- @override
- State<SettingsPage> createState() => _SettingsPageState();
- }
- class _SettingsPageState extends State<SettingsPage> {
- @override
- Widget build(BuildContext context) {
- var screenWidth = MediaQuery.of(context).size.width;
- return Scaffold(
- appBar: AppBar(
- backgroundColor: Colors.blue[300], // Fixed AppBar color
- title: Text("Settings"),
- leading: IconButton(
- icon: Icon(Icons.arrow_back_rounded),
- onPressed: () => Navigator.pop(context),
- ),
- ),
- body: Padding(
- padding: const EdgeInsets.all(25),
- child: Column(
- children: [
- // Theme Dropdown
- SizedBox(
- width: screenWidth / 1.5,
- height: 75,
- child: Card(
- child: Row(
- children: [
- // Left-aligned text
- Expanded(
- child: Align(
- alignment: Alignment.centerLeft,
- child: Padding(
- padding: const EdgeInsets.only(left: 16.0),
- child: Text(
- "Theme",
- style: TextStyle(fontSize: 16),
- ),
- ),
- ),
- ),
- // Right-aligned dropdown
- Expanded(
- child: Align(
- alignment: Alignment.centerRight,
- child: Padding(
- padding: const EdgeInsets.only(right: 16.0),
- child: Consumer<ThemeProvider>(
- builder: (context, provider, child) {
- return DropdownButtonFormField<String>(
- value: provider.currentTheme,
- items: [
- DropdownMenuItem(
- value: 'system',
- child: Text("System"),
- ),
- DropdownMenuItem(
- value: 'light',
- child: Text("Light"),
- ),
- DropdownMenuItem(
- value: 'dark',
- child: Text("Dark"),
- ),
- ],
- onChanged: (String? value) {
- provider.changeTheme(value ?? 'system');
- },
- decoration: InputDecoration(
- border: OutlineInputBorder(
- borderRadius: BorderRadius.circular(10),
- ),
- ),
- );
- },
- ),
- ),
- ),
- ),
- ],
- ),
- ),
- ),
- SizedBox(height: 20), // Spacing between widgets
- // Seed Color Dropdown
- SizedBox(
- width: screenWidth / 1.5,
- height: 75,
- child: Card(
- child: Row(
- children: [
- // Left-aligned text
- Expanded(
- child: Align(
- alignment: Alignment.centerLeft,
- child: Padding(
- padding: const EdgeInsets.only(left: 16.0),
- child: Text(
- "Color Seed",
- style: TextStyle(fontSize: 16),
- ),
- ),
- ),
- ),
- // Right-aligned dropdown
- Expanded(
- child: Align(
- alignment: Alignment.centerRight,
- child: Padding(
- padding: const EdgeInsets.only(right: 16.0),
- child: Consumer<ThemeProvider>(
- builder: (context, provider, child) {
- return DropdownButtonFormField<String>(
- value: provider.currentSeed,
- items: [
- DropdownMenuItem(
- value: 'blue',
- child: Row(
- children: [
- Padding(
- padding: const EdgeInsets.fromLTRB(
- 0, 0, 5, 0),
- child: ClipOval(
- child: Container(
- height: 10,
- width: 10,
- color: Colors.blue,
- ),
- ),
- ),
- Text("Blue"),
- ],
- ),
- ),
- DropdownMenuItem(
- value: 'green',
- child: Row(
- children: [
- Padding(
- padding: const EdgeInsets.fromLTRB(
- 0, 0, 5, 0),
- child: ClipOval(
- child: Container(
- height: 10,
- width: 10,
- color: Colors.green,
- ),
- ),
- ),
- Text("Green"),
- ],
- ),
- ),
- DropdownMenuItem(
- value: 'red',
- child: Row(
- children: [
- Padding(
- padding: const EdgeInsets.fromLTRB(
- 0, 0, 5, 0),
- child: ClipOval(
- child: Container(
- height: 10,
- width: 10,
- color: Colors.red,
- ),
- ),
- ),
- Text("Red"),
- ],
- ),
- ),
- DropdownMenuItem(
- value: 'orange',
- child: Row(
- children: [
- Padding(
- padding: const EdgeInsets.fromLTRB(
- 0, 0, 5, 0),
- child: ClipOval(
- child: Container(
- height: 10,
- width: 10,
- color: Colors.orange,
- ),
- ),
- ),
- Text("Orange"),
- ],
- ),
- ),
- DropdownMenuItem(
- value: 'purple',
- child: Row(
- children: [
- Padding(
- padding: const EdgeInsets.fromLTRB(
- 0, 0, 5, 0),
- child: ClipOval(
- child: Container(
- height: 10,
- width: 10,
- color: Colors.purple,
- ),
- ),
- ),
- Text("Purple"),
- ],
- ),
- ),
- DropdownMenuItem(
- value: 'teal',
- child: Row(
- children: [
- Padding(
- padding: const EdgeInsets.fromLTRB(
- 0, 0, 5, 0),
- child: ClipOval(
- child: Container(
- height: 10,
- width: 10,
- color: Colors.teal,
- ),
- ),
- ),
- Text("Teal"),
- ],
- ),
- ),
- ],
- onChanged: (String? value) {
- if (value != null) {
- provider.changeSeed(
- value); // Update the seed color
- }
- },
- decoration: InputDecoration(
- border: OutlineInputBorder(
- borderRadius: BorderRadius.circular(10),
- ),
- ),
- );
- },
- ),
- ),
- ),
- ),
- ],
- ),
- ),
- ),
- ],
- ),
- ),
- );
- }
- }
- //settings.dart^^^
- ----------------------------------------------------------------------------------------------------------------
- import 'package:flutter/material.dart';
- import 'package:shared_preferences/shared_preferences.dart';
- class ThemeProvider extends ChangeNotifier {
- String currentTheme = 'system';
- String currentSeed = 'blue';
- ThemeMode get themeMode {
- if (currentTheme == 'light') {
- return ThemeMode.light;
- } else if (currentTheme == 'dark') {
- return ThemeMode.dark;
- } else {
- return ThemeMode.system;
- }
- }
- ColorScheme get seedColor {
- switch (currentSeed) {
- case 'blue':
- return ColorScheme.fromSeed(seedColor: Colors.blue);
- case 'green':
- return ColorScheme.fromSeed(seedColor: Colors.green);
- case 'red':
- return ColorScheme.fromSeed(seedColor: Colors.red);
- case 'orange':
- return ColorScheme.fromSeed(seedColor: Colors.orange);
- case 'purple':
- return ColorScheme.fromSeed(seedColor: Colors.purple);
- case 'teal':
- return ColorScheme.fromSeed(seedColor: Colors.teal);
- default:
- return ColorScheme.fromSeed(seedColor: Colors.blue);
- }
- }
- Future<void> changeTheme(String theme) async {
- final SharedPreferences prefs = await SharedPreferences.getInstance();
- await prefs.setString('theme', theme);
- currentTheme = theme;
- notifyListeners();
- }
- Future<void> changeSeed(String seed) async {
- final SharedPreferences prefs = await SharedPreferences.getInstance();
- await prefs.setString('seed', seed);
- currentSeed = seed;
- notifyListeners();
- }
- Future<void> initialize() async {
- final SharedPreferences prefs = await SharedPreferences.getInstance();
- currentTheme = prefs.getString('theme') ?? 'system';
- currentSeed = prefs.getString('seed') ?? 'blue';
- notifyListeners();
- }
- }
- //theme_provider.dart^^^
- --------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement