yubioath-flutter/lib/app/views/action_list.dart

122 lines
3.6 KiB
Dart
Raw Normal View History

2023-06-09 15:46:16 +03:00
/*
* Copyright (C) 2023 Yubico.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
2023-06-09 15:46:16 +03:00
import '../../core/state.dart';
2023-06-09 15:46:16 +03:00
import '../../widgets/list_title.dart';
2023-06-15 18:39:17 +03:00
import '../models.dart';
2023-06-12 12:28:22 +03:00
2023-06-09 15:46:16 +03:00
class ActionListItem extends StatelessWidget {
2023-06-15 18:39:17 +03:00
final Widget icon;
2023-06-09 15:46:16 +03:00
final String title;
final String? subtitle;
final Widget? trailing;
2023-06-15 18:39:17 +03:00
final void Function(BuildContext context)? onTap;
2023-06-12 12:28:22 +03:00
final ActionStyle actionStyle;
final Feature? feature;
2023-06-09 15:46:16 +03:00
const ActionListItem({
super.key,
2023-06-12 12:28:22 +03:00
required this.icon,
2023-06-09 15:46:16 +03:00
required this.title,
this.subtitle,
this.trailing,
this.onTap,
2023-06-12 12:28:22 +03:00
this.actionStyle = ActionStyle.normal,
this.feature,
2023-06-09 15:46:16 +03:00
});
@override
Widget build(BuildContext context) {
2023-12-20 17:09:31 +03:00
// final theme =
// ButtonTheme.of(context).colorScheme ?? Theme.of(context).colorScheme;
2023-06-09 15:46:16 +03:00
2023-12-20 17:09:31 +03:00
// final (foreground, background) = switch (actionStyle) {
// ActionStyle.normal => (theme.onSecondary, theme.secondary),
// ActionStyle.primary => (theme.onPrimary, theme.primary),
// ActionStyle.error => (theme.onError, theme.error),
// };
2023-06-12 12:28:22 +03:00
2023-06-09 15:46:16 +03:00
return ListTile(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),
title: Text(title),
subtitle: subtitle != null ? Text(subtitle!) : null,
2023-06-12 12:28:22 +03:00
leading: Opacity(
opacity: onTap != null ? 1.0 : 0.4,
child: CircleAvatar(
2023-12-20 17:09:31 +03:00
foregroundColor: Theme.of(context).colorScheme.onSurfaceVariant,
backgroundColor: Colors.transparent,
2023-06-12 12:28:22 +03:00
child: icon,
),
),
2023-06-09 15:46:16 +03:00
trailing: trailing,
2023-06-15 18:39:17 +03:00
onTap: onTap != null ? () => onTap?.call(context) : null,
2023-06-09 15:46:16 +03:00
enabled: onTap != null,
);
}
}
class ActionListSection extends ConsumerWidget {
2023-06-09 15:46:16 +03:00
final String title;
final List<ActionListItem> children;
const ActionListSection(this.title, {super.key, required this.children});
2023-06-15 18:39:17 +03:00
factory ActionListSection.fromMenuActions(BuildContext context, String title,
{Key? key, required List<ActionItem> actions}) {
return ActionListSection(
key: key,
title,
children: actions.map((action) {
final intent = action.intent;
return ActionListItem(
key: action.key,
feature: action.feature,
2023-06-15 18:39:17 +03:00
actionStyle: action.actionStyle ?? ActionStyle.normal,
icon: action.icon,
title: action.title,
subtitle: action.subtitle,
onTap: intent != null
? (context) => Actions.invoke(context, intent)
: null,
trailing: action.trailing,
);
}).toList(),
);
}
2023-06-09 15:46:16 +03:00
@override
Widget build(BuildContext context, WidgetRef ref) {
final hasFeature = ref.watch(featureProvider);
final enabledChildren = children
.where((item) => item.feature == null || hasFeature(item.feature!));
if (enabledChildren.isEmpty) {
return const SizedBox();
}
return SizedBox(
width: 360,
child: Column(children: [
ListTitle(
title,
),
...enabledChildren,
]),
);
}
2023-06-09 15:46:16 +03:00
}