diff --git a/lib/appdata.dart b/lib/appdata.dart index 3825469..94f48c6 100644 --- a/lib/appdata.dart +++ b/lib/appdata.dart @@ -63,13 +63,16 @@ class _Appdata { Future readData() async { final file = File("${App.dataPath}/account.json"); if (file.existsSync()) { - account = Account.fromJson(jsonDecode(await file.readAsString())); + var json = jsonDecode(await file.readAsString()); + if(json != null) { + account = Account.fromJson(json); + } } final settingsFile = File("${App.dataPath}/settings.json"); if (settingsFile.existsSync()) { var json = jsonDecode(await settingsFile.readAsString()); for (var key in json.keys) { - if(json[key] != null) { + if (json[key] != null) { settings[key] = json[key]; } } diff --git a/lib/components/button.dart b/lib/components/button.dart new file mode 100644 index 0000000..7a0a85d --- /dev/null +++ b/lib/components/button.dart @@ -0,0 +1,96 @@ +import 'package:fluent_ui/fluent_ui.dart'; +import 'package:pixes/foundation/app.dart'; + +abstract class BaseButton extends StatelessWidget { + const BaseButton({this.enabled = true, this.isLoading = false, super.key}); + + final bool enabled; + + final bool isLoading; + + Widget buildNormal(BuildContext context); + + Widget buildLoading(BuildContext context); + + Widget buildDisabled(BuildContext context); + + @override + Widget build(BuildContext context) { + if (isLoading) { + return buildLoading(context); + } else if (enabled) { + return buildNormal(context); + } else { + return buildDisabled(context); + } + } +} + +class FluentButton extends BaseButton { + const FluentButton({ + required this.onPressed, + required this.child, + this.width, + super.enabled, + super.isLoading, + super.key, + }); + + final void Function() onPressed; + + final Widget child; + + final double? width; + + static const _kFluentButtonPadding = 24; + + @override + Widget buildNormal(BuildContext context) { + Widget child = this.child; + if (width != null) { + child = child.fixWidth(width! - _kFluentButtonPadding); + } + return FilledButton( + onPressed: onPressed, + child: child, + ); + } + + @override + Widget buildLoading(BuildContext context) { + Widget child = Center( + child: const ProgressRing( + strokeWidth: 1.6, + ).fixWidth(14).fixHeight(14), + ); + if (width != null) { + child = child.fixWidth(width!); + } + return Container( + height: 26, + decoration: BoxDecoration( + color: FluentTheme.of(context).inactiveBackgroundColor, + borderRadius: BorderRadius.circular(4)), + child: child, + ); + } + + @override + Widget buildDisabled(BuildContext context) { + Widget child = Center( + child: this.child, + ); + if (width != null) { + child = child.fixWidth(width!); + } + return Center( + child: Container( + height: 26, + decoration: BoxDecoration( + color: FluentTheme.of(context).inactiveBackgroundColor, + borderRadius: BorderRadius.circular(4)), + child: child, + ), + ); + } +} diff --git a/lib/pages/login_page.dart b/lib/pages/login_page.dart index d20a8bf..e131746 100644 --- a/lib/pages/login_page.dart +++ b/lib/pages/login_page.dart @@ -1,4 +1,5 @@ import 'package:fluent_ui/fluent_ui.dart'; +import 'package:pixes/components/button.dart'; import 'package:pixes/foundation/app.dart'; import 'package:pixes/network/network.dart'; import 'package:pixes/pages/webview_page.dart'; @@ -56,22 +57,11 @@ class _LoginPageState extends State { child: Column( mainAxisSize: MainAxisSize.min, children: [ - if (checked) - FilledButton( - onPressed: onContinue, - child: Text("Continue".tl), - ) - else - Container( - height: 28, - width: 78, - decoration: BoxDecoration( - color: FluentTheme.of(context) - .inactiveBackgroundColor, - borderRadius: BorderRadius.circular(4)), - child: Center( - child: Text("Continue".tl), - ), + FluentButton( + onPressed: onContinue, + enabled: checked, + width: 96, + child: Text("Continue".tl), ), const SizedBox( height: 16, diff --git a/lib/pages/settings_page.dart b/lib/pages/settings_page.dart index b15ca49..ebaa816 100644 --- a/lib/pages/settings_page.dart +++ b/lib/pages/settings_page.dart @@ -88,6 +88,7 @@ class _SettingsPageState extends State { child: Text('Continue'.tl), onPressed: () { appdata.account = null; + appdata.writeData(); App.rootNavigatorKey.currentState!.pushAndRemoveUntil( AppPageRoute( builder: (context) => const MainPage()),