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

163 lines
4.6 KiB
Dart
Raw Normal View History

2023-03-19 16:51:06 +01:00
import 'package:cached_network_image/cached_network_image.dart';
2021-04-10 06:28:12 +02:00
import 'package:flutter/material.dart';
2023-06-10 12:44:02 +02:00
import 'package:flutter_bloc/flutter_bloc.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/account.dart';
2023-03-19 16:51:06 +01:00
import 'package:nc_photos/api/api_util.dart' as api_util;
2023-06-10 12:44:02 +02:00
import 'package:nc_photos/controller/account_controller.dart';
import 'package:nc_photos/controller/account_pref_controller.dart';
2023-06-10 12:44:02 +02:00
import 'package:nc_photos/stream_util.dart';
2021-04-17 10:59:16 +02:00
import 'package:nc_photos/theme.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/widget/account_picker_dialog.dart';
import 'package:np_ui/np_ui.dart';
2021-04-10 06:28:12 +02:00
/// AppBar for home screens
class HomeSliverAppBar extends StatelessWidget {
2021-09-15 08:58:06 +02:00
const HomeSliverAppBar({
2021-07-23 22:05:57 +02:00
Key? key,
required this.account,
2021-04-10 06:28:12 +02:00
this.actions,
this.menuActions,
this.onSelectedMenuActions,
this.isShowProgressIcon = false,
2021-04-10 06:28:12 +02:00
}) : super(key: key);
@override
2023-06-10 12:44:02 +02:00
Widget build(BuildContext context) {
return TranslucentSliverAppBar(
2021-04-10 06:28:12 +02:00
title: InkWell(
onTap: () {
showDialog(
context: context,
2023-06-04 19:15:29 +02:00
builder: (_) => const AccountPickerDialog(),
2021-04-10 06:28:12 +02:00
);
},
2023-06-10 12:44:02 +02:00
child: _TitleView(account: account),
2021-04-10 06:28:12 +02:00
),
scrolledUnderBackgroundColor:
Theme.of(context).homeNavigationBarBackgroundColor,
blurFilter: Theme.of(context).appBarBlurFilter,
2021-04-10 06:28:12 +02:00
floating: true,
automaticallyImplyLeading: false,
2023-06-09 18:41:24 +02:00
actions: [
...actions ?? [],
if (menuActions?.isNotEmpty == true)
PopupMenuButton<int>(
tooltip: MaterialLocalizations.of(context).moreButtonTooltip,
itemBuilder: (_) => menuActions!,
onSelected: (option) {
if (option >= 0) {
onSelectedMenuActions?.call(option);
}
},
),
_ProfileIconView(
account: account,
isProcessing: isShowProgressIcon,
onTap: () {
showDialog(
context: context,
builder: (_) => const AccountPickerDialog(),
);
},
),
const SizedBox(width: 8),
],
2021-04-10 06:28:12 +02:00
);
}
final Account account;
/// Screen specific action buttons
2021-07-23 22:05:57 +02:00
final List<Widget>? actions;
2021-04-10 06:28:12 +02:00
/// Screen specific actions under the overflow menu. The value of each item
/// much >= 0
2021-07-23 22:05:57 +02:00
final List<PopupMenuEntry<int>>? menuActions;
final void Function(int)? onSelectedMenuActions;
final bool isShowProgressIcon;
2021-04-10 06:28:12 +02:00
}
2022-11-12 10:55:33 +01:00
2023-06-10 12:44:02 +02:00
class _TitleView extends StatelessWidget {
const _TitleView({
required this.account,
});
@override
Widget build(BuildContext context) {
return ValueStreamBuilder<String?>(
stream:
context.read<AccountController>().accountPrefController.accountLabel,
builder: (context, snapshot) => AppBarTitleContainer(
title: Row(
2023-07-24 15:48:35 +02:00
mainAxisSize: MainAxisSize.min,
2023-06-10 12:44:02 +02:00
children: [
account.scheme == "http"
? Icon(
Icons.no_encryption_outlined,
color: Theme.of(context).colorScheme.error,
size: 16,
)
: Icon(
Icons.https,
color: Theme.of(context).colorScheme.primary,
size: 16,
),
2023-07-24 15:48:35 +02:00
Text(
snapshot.data ?? account.address,
maxLines: 1,
overflow: TextOverflow.clip,
2023-06-10 12:44:02 +02:00
),
],
),
subtitle: snapshot.data == null ? Text(account.username2) : null,
),
);
}
final Account account;
}
2023-06-09 18:41:24 +02:00
class _ProfileIconView extends StatelessWidget {
const _ProfileIconView({
2023-03-19 16:51:06 +01:00
required this.account,
2023-06-09 18:41:24 +02:00
required this.isProcessing,
required this.onTap,
2023-03-19 16:51:06 +01:00
});
@override
Widget build(BuildContext context) {
2023-06-09 18:41:24 +02:00
return SizedBox.square(
dimension: _size,
child: Stack(
children: [
isProcessing
? const AppBarCircularProgressIndicator()
: ClipRRect(
borderRadius: BorderRadius.circular(_size / 2),
child: CachedNetworkImage(
imageUrl: api_util.getAccountAvatarUrl(account, 64),
fadeInDuration: const Duration(),
filterQuality: FilterQuality.high,
),
),
Positioned.fill(
child: Material(
type: MaterialType.transparency,
child: InkWell(
onTap: onTap,
borderRadius: BorderRadius.circular(_size / 2),
),
),
),
],
2023-03-19 16:51:06 +01:00
),
);
}
final Account account;
2023-06-09 18:41:24 +02:00
final bool isProcessing;
final VoidCallback onTap;
static const _size = 40.0;
2023-03-19 16:51:06 +01:00
}