manually restart app

This commit is contained in:
lollipopkit
2023-03-21 14:46:30 +08:00
parent 43e32775a3
commit 4cc72328a7
5 changed files with 68 additions and 77 deletions

View File

@@ -19,69 +19,63 @@ class MyApp extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
setTransparentNavigationBar(context); setTransparentNavigationBar(context);
final fontName = getFileName(_setting.fontPath.fetch()); final fontName = getFileName(_setting.fontPath.fetch());
primaryColor = Color(_setting.primaryColor.fetch()!);
final textStyle = TextStyle(color: primaryColor);
final materialColor = primaryColor.materialStateColor;
final materialColorAlpha = primaryColor.withOpacity(0.7).materialStateColor;
final fabTheme =
FloatingActionButtonThemeData(backgroundColor: primaryColor);
final switchTheme = SwitchThemeData(
thumbColor: materialColor,
trackColor: materialColorAlpha,
);
final appBarTheme = AppBarTheme(backgroundColor: primaryColor);
final iconTheme = IconThemeData(color: primaryColor);
final inputDecorationTheme = InputDecorationTheme(
labelStyle: textStyle,
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor),
),
);
final radioTheme = RadioThemeData(
fillColor: materialColor,
);
return ValueListenableBuilder<int>( return ValueListenableBuilder<int>(
valueListenable: _setting.primaryColor.listenable(), valueListenable: _setting.themeMode.listenable(),
builder: (_, colorValue, __) { builder: (_, tMode, __) {
primaryColor = Color(colorValue); final ok = tMode >= 0 && tMode <= ThemeMode.values.length - 1;
final themeMode = ok ? ThemeMode.values[tMode] : ThemeMode.system;
final textStyle = TextStyle(color: primaryColor); final theme = ThemeData(
final materialColor = primaryColor.materialStateColor; useMaterial3: false,
final materialColorAlpha = fontFamily: fontName,
primaryColor.withOpacity(0.7).materialStateColor; primaryColor: primaryColor,
final fabTheme = primarySwatch: primaryColor.materialColor,
FloatingActionButtonThemeData(backgroundColor: primaryColor); appBarTheme: appBarTheme,
final switchTheme = SwitchThemeData( floatingActionButtonTheme: fabTheme,
thumbColor: materialColor, iconTheme: iconTheme,
trackColor: materialColorAlpha, primaryIconTheme: iconTheme,
switchTheme: switchTheme,
inputDecorationTheme: inputDecorationTheme,
radioTheme: radioTheme,
); );
final appBarTheme = AppBarTheme(backgroundColor: primaryColor);
final iconTheme = IconThemeData(color: primaryColor);
final inputDecorationTheme = InputDecorationTheme(
labelStyle: textStyle,
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor),
),
);
final radioTheme = RadioThemeData(
fillColor: materialColor,
);
return ValueListenableBuilder<int>(
valueListenable: _setting.themeMode.listenable(),
builder: (_, tMode, __) {
final ok = tMode >= 0 && tMode <= ThemeMode.values.length - 1;
final themeMode = ok ? ThemeMode.values[tMode] : ThemeMode.system;
final theme = ThemeData( return MaterialApp(
useMaterial3: false, debugShowCheckedModeBanner: false,
fontFamily: fontName, localizationsDelegates: S.localizationsDelegates,
primaryColor: primaryColor, supportedLocales: S.supportedLocales,
title: BuildData.name,
themeMode: themeMode,
theme: theme,
darkTheme: theme.copyWith(
colorScheme: ColorScheme.fromSwatch(
primarySwatch: primaryColor.materialColor, primarySwatch: primaryColor.materialColor,
appBarTheme: appBarTheme, brightness: Brightness.dark,
floatingActionButtonTheme: fabTheme, accentColor: primaryColor,
iconTheme: iconTheme, ),
primaryIconTheme: iconTheme, ),
switchTheme: switchTheme, home: const MyHomePage(),
inputDecorationTheme: inputDecorationTheme,
radioTheme: radioTheme,
);
return MaterialApp(
debugShowCheckedModeBanner: false,
localizationsDelegates: S.localizationsDelegates,
supportedLocales: S.supportedLocales,
title: BuildData.name,
themeMode: themeMode,
theme: theme,
darkTheme: theme.copyWith(
colorScheme: ColorScheme.fromSwatch(
primarySwatch: primaryColor.materialColor,
brightness: Brightness.dark,
accentColor: primaryColor,
),
),
home: const MyHomePage(),
);
},
); );
}, },
); );

View File

@@ -34,5 +34,5 @@ class SettingStore extends PersistentStore {
StoreProperty<int> get themeMode => property('themeMode', defaultValue: 0); StoreProperty<int> get themeMode => property('themeMode', defaultValue: 0);
/// Font file path /// Font file path
StoreProperty<String> get fontPath => property('fontPath2'); StoreProperty<String> get fontPath => property('fontPath');
} }

View File

@@ -68,7 +68,6 @@ class _MyHomePageState extends State<MyHomePage>
void dispose() { void dispose() {
super.dispose(); super.dispose();
WidgetsBinding.instance.removeObserver(this); WidgetsBinding.instance.removeObserver(this);
_serverProvider.closeServer();
} }
@override @override

View File

@@ -445,8 +445,7 @@ class _ServerPageState extends State<ServerPage>
await GetIt.I.allReady(); await GetIt.I.allReady();
if (_serverProvider.servers.isEmpty) { if (_serverProvider.servers.isEmpty) {
await _serverProvider.loadLocalData(); await _serverProvider.loadLocalData();
await _serverProvider.refreshData();
_serverProvider.startAutoRefresh();
} }
_serverProvider.startAutoRefresh();
} }
} }

View File

@@ -241,7 +241,7 @@ class _SettingPageState extends State<SettingPage> {
icon: const Icon(Icons.save), icon: const Icon(Icons.save),
onPressed: (() { onPressed: (() {
_setting.primaryColor.put(_selectedColorValue); _setting.primaryColor.put(_selectedColorValue);
setState(() {}); _showRestartSnackbar();
}), }),
); );
} }
@@ -459,18 +459,13 @@ class _SettingPageState extends State<SettingPage> {
mainAxisAlignment: MainAxisAlignment.spaceAround, mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [ children: [
TextButton( TextButton(
onPressed: () async => pickFontFile(), onPressed: () async => _pickFontFile(),
child: Text(_s.pickFile), child: Text(_s.pickFile),
), ),
TextButton( TextButton(
onPressed: () => setState(() { onPressed: () => setState(() {
_setting.fontPath.delete(); _setting.fontPath.delete();
showSnackBarWithAction( _showRestartSnackbar();
context,
'${_s.success}\n${_s.needRestart}',
_s.restart,
() => rebuildAll(context),
);
}), }),
child: Text(_s.clear), child: Text(_s.clear),
) )
@@ -480,7 +475,7 @@ class _SettingPageState extends State<SettingPage> {
); );
} }
Future<void> pickFontFile() async { Future<void> _pickFontFile() async {
final path = await pickOneFile(); final path = await pickOneFile();
if (path != null) { if (path != null) {
final fontDir_ = await fontDir; final fontDir_ = await fontDir;
@@ -489,14 +484,18 @@ class _SettingPageState extends State<SettingPage> {
await fontFile.copy(newPath); await fontFile.copy(newPath);
_setting.fontPath.put(newPath); _setting.fontPath.put(newPath);
setState(() {}); setState(() {});
showSnackBarWithAction( _showRestartSnackbar();
context,
'${_s.success}\n${_s.needRestart}',
_s.restart,
() => rebuildAll(context),
);
return; return;
} }
showSnackBar(context, Text(_s.failed)); showSnackBar(context, Text(_s.failed));
} }
void _showRestartSnackbar() {
showSnackBarWithAction(
context,
'${_s.success}\n${_s.needRestart}',
_s.restart,
() => rebuildAll(context),
);
}
} }