nc-photos/app/lib/widget/home_app_bar.dart

176 lines
5.4 KiB
Dart
Raw Normal View History

2023-03-19 23:51:06 +08:00
import 'package:cached_network_image/cached_network_image.dart';
2021-04-17 17:04:46 +08:00
import 'package:event_bus/event_bus.dart';
2021-04-10 12:28:12 +08:00
import 'package:flutter/material.dart';
2021-04-17 17:04:46 +08:00
import 'package:kiwi/kiwi.dart';
2021-04-10 12:28:12 +08:00
import 'package:nc_photos/account.dart';
2023-03-19 23:51:06 +08:00
import 'package:nc_photos/api/api_util.dart' as api_util;
2021-07-25 13:00:38 +08:00
import 'package:nc_photos/app_localizations.dart';
2021-04-17 17:04:46 +08:00
import 'package:nc_photos/event/event.dart';
2021-09-08 16:18:55 +08:00
import 'package:nc_photos/help_utils.dart' as help_utils;
2021-04-17 17:04:46 +08:00
import 'package:nc_photos/pref.dart';
2021-04-17 16:59:16 +08:00
import 'package:nc_photos/theme.dart';
2022-07-08 17:38:24 +08:00
import 'package:nc_photos/url_launcher_util.dart';
2021-04-10 12:28:12 +08:00
import 'package:nc_photos/widget/account_picker_dialog.dart';
2022-12-03 15:47:40 +08:00
import 'package:nc_photos/widget/app_bar_circular_progress_indicator.dart';
2022-11-12 17:55:33 +08:00
import 'package:nc_photos/widget/app_bar_title_container.dart';
2021-04-10 12:28:12 +08:00
import 'package:nc_photos/widget/settings.dart';
import 'package:nc_photos/widget/translucent_sliver_app_bar.dart';
2021-04-10 12:28:12 +08:00
/// AppBar for home screens
class HomeSliverAppBar extends StatelessWidget {
2021-09-15 14:58:06 +08:00
const HomeSliverAppBar({
2021-07-24 04:05:57 +08:00
Key? key,
required this.account,
2021-04-10 12:28:12 +08:00
this.actions,
this.menuActions,
this.onSelectedMenuActions,
this.isShowProgressIcon = false,
2021-04-10 12:28:12 +08:00
}) : super(key: key);
@override
build(BuildContext context) {
2022-07-10 23:53:24 +08:00
final accountLabel = AccountPref.of(account).getAccountLabel();
return TranslucentSliverAppBar(
2021-04-10 12:28:12 +08:00
title: InkWell(
onTap: () {
showDialog(
context: context,
builder: (_) => AccountPickerDialog(
account: account,
),
);
},
2022-11-12 17:55:33 +08:00
child: AppBarTitleContainer(
2023-03-19 23:51:06 +08:00
title: Row(
children: [
account.scheme == "http"
? Icon(
Icons.no_encryption_outlined,
color: Theme.of(context).colorScheme.error,
2023-03-19 23:51:06 +08:00
size: 16,
)
: Icon(
Icons.https,
color: Theme.of(context).colorScheme.primary,
2023-03-19 23:51:06 +08:00
size: 16,
),
Expanded(
child: Text(
accountLabel ?? account.address,
maxLines: 1,
overflow: TextOverflow.clip,
),
),
],
),
subtitle: accountLabel == null ? Text(account.username2) : null,
icon: isShowProgressIcon
? const AppBarCircularProgressIndicator()
: _LeadingView(account: account),
2021-04-10 12:28:12 +08:00
),
),
scrolledUnderBackgroundColor:
Theme.of(context).homeNavigationBarBackgroundColor,
2021-04-10 12:28:12 +08:00
floating: true,
automaticallyImplyLeading: false,
actions: (actions ?? []) +
[
2021-10-28 04:40:54 +08:00
if (!Pref().isFollowSystemThemeOr(false))
2022-11-12 17:55:33 +08:00
_DarkModeSwitch(
onChanged: _onDarkModeChanged,
),
2021-07-24 04:05:57 +08:00
PopupMenuButton<int>(
2021-04-10 12:28:12 +08:00
tooltip: MaterialLocalizations.of(context).moreButtonTooltip,
itemBuilder: (context) =>
(menuActions ?? []) +
[
PopupMenuItem(
value: _menuValueAbout,
child: Text(L10n.global().settingsMenuLabel),
2021-04-10 12:28:12 +08:00
),
2021-08-18 12:24:50 +08:00
PopupMenuItem(
value: _menuValueHelp,
child: Text(L10n.global().helpTooltip),
2021-08-18 12:24:50 +08:00
),
2021-04-10 12:28:12 +08:00
],
onSelected: (option) {
if (option >= 0) {
onSelectedMenuActions?.call(option);
} else {
if (option == _menuValueAbout) {
Navigator.of(context).pushNamed(Settings.routeName,
arguments: SettingsArguments(account));
2021-08-18 12:24:50 +08:00
} else if (option == _menuValueHelp) {
2021-09-08 16:18:55 +08:00
launch(help_utils.mainUrl);
2021-04-10 12:28:12 +08:00
}
}
},
),
],
);
}
2021-04-17 17:04:46 +08:00
void _onDarkModeChanged(bool value) {
2021-10-28 04:40:54 +08:00
Pref().setDarkTheme(value).then((_) {
2021-04-17 17:04:46 +08:00
KiwiContainer().resolve<EventBus>().fire(ThemeChangedEvent());
});
}
2021-04-10 12:28:12 +08:00
final Account account;
/// Screen specific action buttons
2021-07-24 04:05:57 +08:00
final List<Widget>? actions;
2021-04-10 12:28:12 +08:00
/// Screen specific actions under the overflow menu. The value of each item
/// much >= 0
2021-07-24 04:05:57 +08:00
final List<PopupMenuEntry<int>>? menuActions;
final void Function(int)? onSelectedMenuActions;
final bool isShowProgressIcon;
2021-04-10 12:28:12 +08:00
static const _menuValueAbout = -1;
2021-08-18 12:24:50 +08:00
static const _menuValueHelp = -2;
2021-04-10 12:28:12 +08:00
}
2022-11-12 17:55:33 +08:00
class _DarkModeSwitch extends StatelessWidget {
const _DarkModeSwitch({
this.onChanged,
});
@override
Widget build(BuildContext context) {
return Theme(
data: buildDarkModeSwitchTheme(context),
child: Switch(
value: Theme.of(context).brightness == Brightness.dark,
onChanged: onChanged,
activeThumbImage:
const AssetImage("assets/ic_dark_mode_switch_24dp.png"),
inactiveThumbImage:
const AssetImage("assets/ic_dark_mode_switch_24dp.png"),
),
);
}
final ValueChanged<bool>? onChanged;
}
2023-03-19 23:51:06 +08:00
class _LeadingView extends StatelessWidget {
const _LeadingView({
required this.account,
});
@override
Widget build(BuildContext context) {
return ClipRRect(
borderRadius: BorderRadius.circular(24),
child: CachedNetworkImage(
imageUrl: api_util.getAccountAvatarUrl(account, 64),
fadeInDuration: const Duration(),
filterQuality: FilterQuality.high,
),
);
}
final Account account;
}