Advertisement
Hollow09Knight

Untitled

Jan 26th, 2025
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 14.86 KB | Source Code | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:provider/provider.dart';
  3. import 'pages/home_page.dart'; // Import the HomePage
  4. import 'theme_provider.dart'; // Import the ThemeProvider
  5.  
  6. void main() {
  7.   runApp(
  8.     ChangeNotifierProvider<ThemeProvider>(
  9.       create: (_) => ThemeProvider()..initialize(), // Initialize ThemeProvider
  10.       child: const MyApp(),
  11.     ),
  12.   );
  13. }
  14.  
  15. class MyApp extends StatelessWidget {
  16.   const MyApp({super.key});
  17.  
  18.   @override
  19.   Widget build(BuildContext context) {
  20.     return Consumer<ThemeProvider>(builder: (context, provider, child) {
  21.       return MaterialApp(
  22.         debugShowCheckedModeBanner: false,
  23.         theme: ThemeData.light(useMaterial3: true)
  24.             .copyWith(colorScheme: provider.seedColor),
  25.         darkTheme: ThemeData.dark(useMaterial3: true)
  26.             .copyWith(colorScheme: provider.seedColor),
  27.         themeMode: provider.themeMode, // Use the current theme mode
  28.         home: HomePage(),
  29.       );
  30.     });
  31.   }
  32. }
  33.  
  34. //main.dart^^^
  35.  
  36. ---------------------------------------------------------------------------------------------------------------------
  37.  
  38. import 'package:flutter/material.dart';
  39. import 'package:provider/provider.dart';
  40. import '../theme_provider.dart'; // Import the ThemeProvider
  41.  
  42. class SettingsPage extends StatefulWidget {
  43.   const SettingsPage({super.key});
  44.   @override
  45.   State<SettingsPage> createState() => _SettingsPageState();
  46. }
  47.  
  48. class _SettingsPageState extends State<SettingsPage> {
  49.   @override
  50.   Widget build(BuildContext context) {
  51.     var screenWidth = MediaQuery.of(context).size.width;
  52.  
  53.     return Scaffold(
  54.       appBar: AppBar(
  55.         backgroundColor: Colors.blue[300], // Fixed AppBar color
  56.         title: Text("Settings"),
  57.         leading: IconButton(
  58.           icon: Icon(Icons.arrow_back_rounded),
  59.           onPressed: () => Navigator.pop(context),
  60.         ),
  61.       ),
  62.       body: Padding(
  63.         padding: const EdgeInsets.all(25),
  64.         child: Column(
  65.           children: [
  66.             // Theme Dropdown
  67.             SizedBox(
  68.               width: screenWidth / 1.5,
  69.               height: 75,
  70.               child: Card(
  71.                 child: Row(
  72.                   children: [
  73.                     // Left-aligned text
  74.                     Expanded(
  75.                       child: Align(
  76.                         alignment: Alignment.centerLeft,
  77.                         child: Padding(
  78.                           padding: const EdgeInsets.only(left: 16.0),
  79.                           child: Text(
  80.                             "Theme",
  81.                             style: TextStyle(fontSize: 16),
  82.                           ),
  83.                         ),
  84.                       ),
  85.                     ),
  86.                     // Right-aligned dropdown
  87.                     Expanded(
  88.                       child: Align(
  89.                         alignment: Alignment.centerRight,
  90.                         child: Padding(
  91.                           padding: const EdgeInsets.only(right: 16.0),
  92.                           child: Consumer<ThemeProvider>(
  93.                             builder: (context, provider, child) {
  94.                               return DropdownButtonFormField<String>(
  95.                                 value: provider.currentTheme,
  96.                                 items: [
  97.                                   DropdownMenuItem(
  98.                                     value: 'system',
  99.                                     child: Text("System"),
  100.                                   ),
  101.                                   DropdownMenuItem(
  102.                                     value: 'light',
  103.                                     child: Text("Light"),
  104.                                   ),
  105.                                   DropdownMenuItem(
  106.                                     value: 'dark',
  107.                                     child: Text("Dark"),
  108.                                   ),
  109.                                 ],
  110.                                 onChanged: (String? value) {
  111.                                   provider.changeTheme(value ?? 'system');
  112.                                 },
  113.                                 decoration: InputDecoration(
  114.                                   border: OutlineInputBorder(
  115.                                     borderRadius: BorderRadius.circular(10),
  116.                                   ),
  117.                                 ),
  118.                               );
  119.                             },
  120.                           ),
  121.                         ),
  122.                       ),
  123.                     ),
  124.                   ],
  125.                 ),
  126.               ),
  127.             ),
  128.             SizedBox(height: 20), // Spacing between widgets
  129.             // Seed Color Dropdown
  130.             SizedBox(
  131.               width: screenWidth / 1.5,
  132.               height: 75,
  133.               child: Card(
  134.                 child: Row(
  135.                   children: [
  136.                     // Left-aligned text
  137.                     Expanded(
  138.                       child: Align(
  139.                         alignment: Alignment.centerLeft,
  140.                         child: Padding(
  141.                           padding: const EdgeInsets.only(left: 16.0),
  142.                           child: Text(
  143.                             "Color Seed",
  144.                             style: TextStyle(fontSize: 16),
  145.                           ),
  146.                         ),
  147.                       ),
  148.                     ),
  149.                     // Right-aligned dropdown
  150.                     Expanded(
  151.                       child: Align(
  152.                         alignment: Alignment.centerRight,
  153.                         child: Padding(
  154.                           padding: const EdgeInsets.only(right: 16.0),
  155.                           child: Consumer<ThemeProvider>(
  156.                             builder: (context, provider, child) {
  157.                               return DropdownButtonFormField<String>(
  158.                                 value: provider.currentSeed,
  159.                                 items: [
  160.                                   DropdownMenuItem(
  161.                                     value: 'blue',
  162.                                     child: Row(
  163.                                       children: [
  164.                                         Padding(
  165.                                           padding: const EdgeInsets.fromLTRB(
  166.                                               0, 0, 5, 0),
  167.                                           child: ClipOval(
  168.                                             child: Container(
  169.                                               height: 10,
  170.                                               width: 10,
  171.                                               color: Colors.blue,
  172.                                             ),
  173.                                           ),
  174.                                         ),
  175.                                         Text("Blue"),
  176.                                       ],
  177.                                     ),
  178.                                   ),
  179.                                   DropdownMenuItem(
  180.                                     value: 'green',
  181.                                     child: Row(
  182.                                       children: [
  183.                                         Padding(
  184.                                           padding: const EdgeInsets.fromLTRB(
  185.                                               0, 0, 5, 0),
  186.                                           child: ClipOval(
  187.                                             child: Container(
  188.                                               height: 10,
  189.                                               width: 10,
  190.                                               color: Colors.green,
  191.                                             ),
  192.                                           ),
  193.                                         ),
  194.                                         Text("Green"),
  195.                                       ],
  196.                                     ),
  197.                                   ),
  198.                                   DropdownMenuItem(
  199.                                     value: 'red',
  200.                                     child: Row(
  201.                                       children: [
  202.                                         Padding(
  203.                                           padding: const EdgeInsets.fromLTRB(
  204.                                               0, 0, 5, 0),
  205.                                           child: ClipOval(
  206.                                             child: Container(
  207.                                               height: 10,
  208.                                               width: 10,
  209.                                               color: Colors.red,
  210.                                             ),
  211.                                           ),
  212.                                         ),
  213.                                         Text("Red"),
  214.                                       ],
  215.                                     ),
  216.                                   ),
  217.                                   DropdownMenuItem(
  218.                                     value: 'orange',
  219.                                     child: Row(
  220.                                       children: [
  221.                                         Padding(
  222.                                           padding: const EdgeInsets.fromLTRB(
  223.                                               0, 0, 5, 0),
  224.                                           child: ClipOval(
  225.                                             child: Container(
  226.                                               height: 10,
  227.                                               width: 10,
  228.                                               color: Colors.orange,
  229.                                             ),
  230.                                           ),
  231.                                         ),
  232.                                         Text("Orange"),
  233.                                       ],
  234.                                     ),
  235.                                   ),
  236.                                   DropdownMenuItem(
  237.                                     value: 'purple',
  238.                                     child: Row(
  239.                                       children: [
  240.                                         Padding(
  241.                                           padding: const EdgeInsets.fromLTRB(
  242.                                               0, 0, 5, 0),
  243.                                           child: ClipOval(
  244.                                             child: Container(
  245.                                               height: 10,
  246.                                               width: 10,
  247.                                               color: Colors.purple,
  248.                                             ),
  249.                                           ),
  250.                                         ),
  251.                                         Text("Purple"),
  252.                                       ],
  253.                                     ),
  254.                                   ),
  255.                                   DropdownMenuItem(
  256.                                     value: 'teal',
  257.                                     child: Row(
  258.                                       children: [
  259.                                         Padding(
  260.                                           padding: const EdgeInsets.fromLTRB(
  261.                                               0, 0, 5, 0),
  262.                                           child: ClipOval(
  263.                                             child: Container(
  264.                                               height: 10,
  265.                                               width: 10,
  266.                                               color: Colors.teal,
  267.                                             ),
  268.                                           ),
  269.                                         ),
  270.                                         Text("Teal"),
  271.                                       ],
  272.                                     ),
  273.                                   ),
  274.                                 ],
  275.                                 onChanged: (String? value) {
  276.                                   if (value != null) {
  277.                                     provider.changeSeed(
  278.                                         value); // Update the seed color
  279.                                   }
  280.                                 },
  281.                                 decoration: InputDecoration(
  282.                                   border: OutlineInputBorder(
  283.                                     borderRadius: BorderRadius.circular(10),
  284.                                   ),
  285.                                 ),
  286.                               );
  287.                             },
  288.                           ),
  289.                         ),
  290.                       ),
  291.                     ),
  292.                   ],
  293.                 ),
  294.               ),
  295.             ),
  296.           ],
  297.         ),
  298.       ),
  299.     );
  300.   }
  301. }
  302.  
  303.  
  304. //settings.dart^^^
  305.  
  306. ----------------------------------------------------------------------------------------------------------------
  307.  
  308. import 'package:flutter/material.dart';
  309. import 'package:shared_preferences/shared_preferences.dart';
  310.  
  311. class ThemeProvider extends ChangeNotifier {
  312.   String currentTheme = 'system';
  313.   String currentSeed = 'blue';
  314.  
  315.   ThemeMode get themeMode {
  316.     if (currentTheme == 'light') {
  317.       return ThemeMode.light;
  318.     } else if (currentTheme == 'dark') {
  319.       return ThemeMode.dark;
  320.     } else {
  321.       return ThemeMode.system;
  322.     }
  323.   }
  324.  
  325.   ColorScheme get seedColor {
  326.     switch (currentSeed) {
  327.       case 'blue':
  328.         return ColorScheme.fromSeed(seedColor: Colors.blue);
  329.       case 'green':
  330.         return ColorScheme.fromSeed(seedColor: Colors.green);
  331.       case 'red':
  332.         return ColorScheme.fromSeed(seedColor: Colors.red);
  333.       case 'orange':
  334.         return ColorScheme.fromSeed(seedColor: Colors.orange);
  335.       case 'purple':
  336.         return ColorScheme.fromSeed(seedColor: Colors.purple);
  337.       case 'teal':
  338.         return ColorScheme.fromSeed(seedColor: Colors.teal);
  339.       default:
  340.         return ColorScheme.fromSeed(seedColor: Colors.blue);
  341.     }
  342.   }
  343.  
  344.   Future<void> changeTheme(String theme) async {
  345.     final SharedPreferences prefs = await SharedPreferences.getInstance();
  346.     await prefs.setString('theme', theme);
  347.     currentTheme = theme;
  348.     notifyListeners();
  349.   }
  350.  
  351.   Future<void> changeSeed(String seed) async {
  352.     final SharedPreferences prefs = await SharedPreferences.getInstance();
  353.     await prefs.setString('seed', seed);
  354.     currentSeed = seed;
  355.     notifyListeners();
  356.   }
  357.  
  358.   Future<void> initialize() async {
  359.     final SharedPreferences prefs = await SharedPreferences.getInstance();
  360.     currentTheme = prefs.getString('theme') ?? 'system';
  361.     currentSeed = prefs.getString('seed') ?? 'blue';
  362.     notifyListeners();
  363.   }
  364. }
  365.  
  366.  
  367. //theme_provider.dart^^^
  368. --------------------------------------------------------------------------------------------------
  369.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement