uri_launch canLaunch interfering with flatpak bugfix

Change theme manually support
This commit is contained in:
Kingkor Roy Tirtho 2022-01-20 18:45:12 +06:00
parent 8acd6c106a
commit 00acdf598d
5 changed files with 146 additions and 61 deletions

View File

@ -1,4 +1,3 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
@ -9,6 +8,8 @@ import 'package:spotube/helpers/server_ipc.dart';
import 'package:spotube/models/LocalStorageKeys.dart';
import 'package:spotube/provider/Auth.dart';
const redirectUri = "http://localhost:4304/auth/spotify/callback";
class Login extends StatefulWidget {
@override
_LoginState createState() => _LoginState();
@ -31,7 +32,6 @@ class _LoginState extends State<Login> {
}
final credentials = SpotifyApiCredentials(clientId, clientSecret);
final grant = SpotifyApi.authorizationCodeGrant(credentials);
const redirectUri = "http://localhost:4304/auth/spotify/callback";
final authUri = grant.getAuthorizationUrl(Uri.parse(redirectUri),
scopes: spotifyScopes);

View File

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:spotube/components/PageWindowTitleBar.dart';
import 'package:spotube/main.dart';
import 'package:spotube/models/LocalStorageKeys.dart';
import 'package:spotube/provider/Auth.dart';
import 'package:spotube/provider/UserPreferences.dart';
@ -50,8 +51,10 @@ class _SettingsState extends State<Settings> {
),
const SizedBox(height: 10),
Padding(
padding: const EdgeInsets.all(10),
child: Row(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Row(
children: [
Expanded(
flex: 2,
@ -92,6 +95,38 @@ class _SettingsState extends State<Settings> {
)
],
),
const SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text("Theme"),
DropdownButton<ThemeMode>(
value: MyApp.of(context)?.getThemeMode(),
items: const [
DropdownMenuItem(
child: Text(
"Dark",
),
value: ThemeMode.dark,
),
DropdownMenuItem(
child: Text(
"Light",
),
value: ThemeMode.light,
),
DropdownMenuItem(
child: Text("System"),
value: ThemeMode.system,
),
],
onChanged: (value) {
if (value != null) {
MyApp.of(context)?.setThemeMode(value);
}
},
)
],
),
const SizedBox(height: 10),
Builder(builder: (context) {
@ -109,6 +144,9 @@ class _SettingsState extends State<Settings> {
})
],
),
),
],
),
);
}
}

View File

@ -4,10 +4,8 @@ import 'package:url_launcher/url_launcher.dart';
Future<String?> connectIpc(String authUri, String redirectUri) async {
try {
if (await canLaunch(authUri)) {
print("[Launching]: $authUri");
await launch(authUri);
}
HttpServer server =
await HttpServer.bind(InternetAddress.loopbackIPv4, 4304);
@ -32,7 +30,9 @@ Future<String?> connectIpc(String authUri, String redirectUri) async {
}
}
}
} catch (error) {
throw error;
} catch (error, stack) {
print("[connectIpc]: $error");
print(stack);
rethrow;
}
}

View File

@ -24,7 +24,52 @@ void main() async {
});
}
class MyApp extends StatelessWidget {
class MyApp extends StatefulWidget {
static _MyAppState? of(BuildContext context) =>
context.findAncestorStateOfType<_MyAppState>();
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
ThemeMode _themeMode = ThemeMode.system;
@override
void initState() {
WidgetsBinding.instance?.addPostFrameCallback((timeStamp) async {
SharedPreferences localStorage = await SharedPreferences.getInstance();
String? themeMode = localStorage.getString(LocalStorageKeys.themeMode);
setState(() {
switch (themeMode) {
case "light":
_themeMode = ThemeMode.light;
break;
case "dark":
_themeMode = ThemeMode.dark;
break;
default:
_themeMode = ThemeMode.system;
}
});
});
super.initState();
}
void setThemeMode(ThemeMode themeMode) {
SharedPreferences.getInstance().then((localStorage) {
localStorage.setString(
LocalStorageKeys.themeMode, themeMode.toString().split(".").last);
setState(() {
_themeMode = themeMode;
});
});
}
ThemeMode getThemeMode() {
return _themeMode;
}
@override
Widget build(BuildContext context) {
return MultiProvider(
@ -145,7 +190,7 @@ class MyApp extends StatelessWidget {
unselectedIconTheme: const IconThemeData(opacity: 1),
),
),
themeMode: ThemeMode.system,
themeMode: _themeMode,
home: const Home(),
),
);

View File

@ -5,4 +5,6 @@ abstract class LocalStorageKeys {
static String refreshToken = 'refresh_token';
static String expiration = "expiration";
static String geniusAccessToken = "genius_access_token";
static String themeMode = "theme_mode";
}