不使用ThemeData创建深色主题

时间:2020-05-22 11:08:32

标签: flutter dart flutter-layout flutter-theme

我创建了一个名为 import wikipediaapi from gtts import gTTS import speech_recognition as sr import os import datetime import calendar import warnings import random # ignore any warnings warnings.filterwarnings('ignore') # audio -> command -> response (text) -> response (speech) # records audio and return it as string def recordAudio(): r = sr.Recognizer # creating a recognizer object # open the mic with sr.Microphone() as src: print('say something!') audio = r.listen(self=None, source=src, timeout=None, phrase_time_limit=None, snowboy_configuration=None) # use google speech recog data = '' try: r.recognize_google(self=None, audio_data=audio, key=None, language='en', show_all=False) print('You said: ' + data) except sr.UnknownValueError as s: print('Your audio could not be recognized' + s) except sr.RequestError as e: print('Request results from Google Speech Recog error' + e) return data def assistantResponse(string): print(string) myObj = gTTS(text=string, lang='en, fr', slow=False) # save the converted audio to file. myObj.save('assistant_response.mp3') # Play the file os.system('start assistant_response.mp3') # A function for wake word(s) or phrase def wakeWord(string): WAKE_WORDS = ['hey Apoorva', 'okay Apoorva'] # A list of wake words text = string.lower() for phrase in WAKE_WORDS: if phrase in text: return True # IF wake word isn't found in the text, comp returns false. return False # A function to reutrn the current date def getDate(): now = datetime.datetime.now() my_date = datetime.datetime.today() weekday = calendar.day_name[my_date.weekday()] monthNum = now.month dayNum = now.day # a list of months month_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] return 'Today is ' + month_names[monthNum - 1], ', ' + dayNum, 'st, ' + weekday def greetingUser(string): GREETINGS_INPUT = ['hello', 'hola', 'hi', 'greetings', 'wassup'] GREETINGS_OUTPUT = ['hello, matey!', 'oh hi!', 'hey there!'] if GREETINGS_INPUT in string: rando = random.randint(0, 2) return GREETINGS_OUTPUT[rando] def wikiSearch(string): wordList = string.split() for i in range(0, len(wordList)): if i + 3 <= len(wordList) - 1 and wordList[i].lower() == 'who' or 'what' and wordList[i + 1].lower() == 'is': return wordList[i + 2] + ' ' + wordList[i + 3] while True: # Record Audio text = recordAudio() response = '' # Check fro wake word/phrase if (wakeWord(text)) == True: response = response + greetingUser(text) if ('date' in text): get_date = getDate() response = response + ' ' + get_date if ('who is' or 'what is' in text): person = wikiSearch(text) wiki = wikipedia.summary(person, sentences=2) response = response + ' ' + wiki assistantResponse(response) 的飞镖文件,在其中放置了我所有的theme.dart以及我的colorsfont sizes

我的 text styles 看起来像这样:

theme.dart

例如,我在“设置”菜单中有一个import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; //COLORS const colorPrimary = Color(0xFF913f91); const colorPrimaryLight = Color(0xFFed74ed); const colorSecondary = Color(0xFF1fb4ac); const colorSecondaryLight = Color(0xFF3ee6dc); const colorTertiary = Color(0xFFff8519); const colorTertiaryLight = Color(0xFFf7a963); const colorPositive = Color(0xFF00d96f); const colorNegative = Color(0xFFFF4464); const colorNegativeDark = Color(0xFFff8095); const colorDisabled = Color(0xFFC1C1C1); const colorOffWhite = Color(0xFFf2f2f2); const colorOffBlack = Color(0xFF222222); const colorText = Color(0xFF222222); const colorAqua = Color(0xFF19c4fc); const colorAquaDark = Color(0xFF09a7db); const colorDarkBlue = Color(0xFF00649e); const colorBlue = Color(0xFF1fadff); const colorYellow = Color(0xFFfff021); const colorYellowLight = Color(0xFFfff354); const gradientTop = Color(0xFFFFD560); const gradientBottom = Color(0xFFFF8519); const colorShadowDark = Color(0xFF090909); //FONT SIZE const fontSizeRegular = 18.0; const fontSizeSmall = 12.0; const fontSizeMedium = 15.0; const fontSizeSmallest = 10.0; const fontSizeLarge = 22.0; const fontExtraLarge = 30.0; //TEXT STYLES textHeaderStyle(Color _color, FontStyle _fontStyle, FontWeight _fontWeight) { return TextStyle( fontSize: ScreenUtil().setSp(fontSizeLarge), color: _color, fontStyle: _fontStyle, fontWeight: _fontWeight); } 的说法,可以在暗模式和亮模式之间切换。

什么是最好的方法?

如果我在以下颜色上添加条件:

switch

其中“模式” 是全局变量,并且每次在“设置” 菜单中切换const colorPrimary = mode == "light" ? Color(0xFF913f91) : Color(0xFFed74ed); 时都会添加一个setState() ,会起作用吗?

我会很乐意尝试您的所有建议。

感谢能提供帮助的人!

1 个答案:

答案 0 :(得分:0)

我能想到的更简单的解决方案;您可以在调用时使用bool选择主题某种模式,如果设置为true,则可以设置明亮的主题颜色,如果使用假的深色主题颜色和关键部分,则需要将其与shared preferences存储在一起才能应用整个应用并使其永久不变,我的意思是即使用户靠近应用程序也可以再次设置用户设置,然后重新启动它。

因此您可以在主题飞镖中使用bool:

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

    //COLORS
    colorPrimary = mode ? Color(0xFFed74ed) : Color(0xFF913f91);
    colorSecondary = mode ? Color(0xFF3ee6dc) : Color(0xFF1fb4ac);
    colorTertiary = mode ? Color(0xFFf7a963): Color(0xFFff8519);
    ...

然后,您无需制作全局变量,只需说您每次在“设置”菜单中切换开关即可添加setState()。