Skip to content

Commit

Permalink
Add functionality to theme switcher
Browse files Browse the repository at this point in the history
  • Loading branch information
deathblade666 committed Jul 28, 2024
1 parent 8df3785 commit 3225621
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
43 changes: 34 additions & 9 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,50 @@ import 'package:flutter/material.dart';
import 'package:mdeditor/pages/split_edit.dart';

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

final _defaultDarkColorScheme = ColorScheme.fromSwatch(primarySwatch: Colors.indigo, brightness: Brightness.dark);
final _defaultLightColorScheme = ColorScheme.fromSwatch(primarySwatch: Colors.indigo);

class MyApp extends StatelessWidget{
const MyApp({super.key});
class MyApp extends StatefulWidget{
MyApp({super.key});


@override
State<MyApp> createState() => MyAppState();
}

// ignore: non_constant_identifier_names
class MyAppState extends State<MyApp> {
MyAppState();
String selectedTheme = '';
var theme;


void setTheme(selectedTheme){
setState(() {
if (selectedTheme == "system"){
theme = ThemeMode.system;
}
if (selectedTheme == "light"){
theme = ThemeMode.light;
}
if (selectedTheme == "dark"){
theme = ThemeMode.dark;
}
print(selectedTheme);
print(theme);
});
}


@override
Widget build(BuildContext context) {

return DynamicColorBuilder(builder: (lightColorScheme, darkColorScheme) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Editor(),
home: Editor(onThemeSelect: setTheme,),
theme: ThemeData(
colorScheme: lightColorScheme ?? _defaultLightColorScheme,
useMaterial3: true,
Expand All @@ -29,10 +56,8 @@ class MyApp extends StatelessWidget{
colorScheme: darkColorScheme ?? _defaultDarkColorScheme,
useMaterial3: true,
),
//themeMode: ThemeMode.dark,
//themeMode: ThemeMode.light,
themeMode: ThemeMode.system,
themeMode: theme,
);
});
}
}
}
18 changes: 12 additions & 6 deletions lib/pages/menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ import 'package:shared_preferences/shared_preferences.dart';


class Menu extends StatefulWidget {
Menu(this.inputText,this.OpenFile,this.wordCount,{required this.onEnableWordCount, required this.onModeToggle, required this.onFileLoad,required this.onfileName,super.key});
Menu(this.inputText,this.OpenFile,this.wordCount,{required this.onEnableWordCount, required this.onModeToggle, required this.onFileLoad,required this.onfileName,required this.onThemeSelected,super.key});
final void Function(String fileContent) onFileLoad;
final void Function(String fileName) onfileName;
final void Function(bool fullEdit) onModeToggle;
final void Function(bool WordCount) onEnableWordCount;
void Function(String? selectedTheme) onThemeSelected;
TextEditingController OpenFile = TextEditingController();
final String inputText;
int wordCount;
Expand Down Expand Up @@ -85,6 +86,10 @@ class Menu extends StatefulWidget {
prefs.setBool("DisplayWordCount", WordCount);
}

void setTheme(String? value) async {
var selectedTheme = value;
widget.onThemeSelected(selectedTheme);
}

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -140,7 +145,7 @@ class Menu extends StatefulWidget {
child: const Text("Options"),
onTap: () { showDialog(
context: context,
builder: (context) => optionsDialog(switchModeValue,switchWCValue,widget.onEnableWordCount,widget.onModeToggle)
builder: (context) => optionsDialog(switchModeValue,switchWCValue,widget.onEnableWordCount,widget.onModeToggle, onThemeSelected: setTheme,)

);}
),
Expand All @@ -160,8 +165,8 @@ class optionsDialog extends StatefulWidget {
bool switchModeValue;
bool switchWCValue;

optionsDialog(this.switchModeValue, this.switchWCValue, this.onEnableWordCount, this.onModeToggle, {super.key});

optionsDialog(this.switchModeValue, this.switchWCValue, this.onEnableWordCount, this.onModeToggle, {required this.onThemeSelected,super.key});
void Function (String? selectedTheme) onThemeSelected;
@override
optionsDialogState createState() => optionsDialogState();
}
Expand All @@ -183,7 +188,7 @@ class optionsDialogState extends State<optionsDialog> {
widget.onEnableWordCount(WordCount);
prefs.setBool("DisplayWordCount", WordCount);
}

@override
Widget build(BuildContext context) {
return Dialog(
Expand All @@ -205,7 +210,8 @@ class optionsDialogState extends State<optionsDialog> {
initialSelection: list.first,
onSelected: (String? value) async {
setState(() {
//TODO: to be implemented
var selectedTheme = value;
widget.onThemeSelected(selectedTheme);
});
},
dropdownMenuEntries: list.map<DropdownMenuEntry<String>>((String value) {
Expand Down
11 changes: 9 additions & 2 deletions lib/pages/split_edit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import 'package:mdeditor/pages/preview.dart';
import 'package:mdeditor/pages/textfield.dart';

class Editor extends StatefulWidget {
const Editor({super.key});
const Editor({super.key, required this.onThemeSelect});
final void Function(String selectedtheme) onThemeSelect;

@override
State<Editor> createState() => editorState();
Expand All @@ -18,6 +19,7 @@ class editorState extends State<Editor> {
bool _full = fullEdit;
bool showWordCount = WordCount;
int wordCount = 0;
var theme = ThemeMode.system;
TextEditingController OpenFile = TextEditingController();

void mdText(String inputText) {
Expand Down Expand Up @@ -55,6 +57,11 @@ class editorState extends State<Editor> {
});
}

void setTheme(selectedTheme){
widget.onThemeSelect(selectedTheme);
}


@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -91,7 +98,7 @@ class editorState extends State<Editor> {
visible: WordCount,
child: Text("$wordCount"),
),
Menu(onFileLoad: loadedFile, contents, OpenFile, onfileName: setFileName, onModeToggle: switchViewMode, wordCount, onEnableWordCount: enableWordCount,),
Menu(onFileLoad: loadedFile, contents, OpenFile, onfileName: setFileName, onModeToggle: switchViewMode, wordCount, onEnableWordCount: enableWordCount,onThemeSelected: setTheme,),
const Padding(padding: EdgeInsets.only(right: 15)),
],
),
Expand Down
2 changes: 1 addition & 1 deletion test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'package:mdeditor/main.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
await tester.pumpWidget( MyApp());

// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
Expand Down

0 comments on commit 3225621

Please sign in to comment.