adguard-home-manager/lib/widgets/tab_content_list.dart

203 lines
6.3 KiB
Dart
Raw Normal View History

2023-04-06 18:10:23 +02:00
import 'package:flutter/material.dart';
2023-04-06 18:27:57 +02:00
import 'package:provider/provider.dart';
2023-04-06 18:10:23 +02:00
2023-04-06 18:27:57 +02:00
import 'package:adguard_home_manager/providers/app_config_provider.dart';
2023-04-06 18:10:23 +02:00
import 'package:adguard_home_manager/constants/enums.dart';
class CustomTabContentList extends StatelessWidget {
final Widget Function() loadingGenerator;
final int itemsCount;
final Widget Function(int index) contentWidget;
final Widget noData;
final Widget Function() errorGenerator;
final LoadStatus loadStatus;
final Future<void> Function() onRefresh;
2023-04-06 18:50:06 +02:00
final double? refreshIndicatorOffset;
2023-04-06 18:27:57 +02:00
final Widget? fab;
final bool? fabVisible;
2023-04-30 23:36:02 +02:00
final bool? noSliver;
2023-05-01 21:34:00 +02:00
final EdgeInsets? listPadding;
2024-02-04 21:23:45 +01:00
final double? heightFabHidden;
2023-04-06 18:10:23 +02:00
const CustomTabContentList({
2023-12-09 04:04:14 +01:00
super.key,
2023-04-06 18:10:23 +02:00
required this.loadingGenerator,
required this.itemsCount,
required this.contentWidget,
required this.noData,
required this.errorGenerator,
required this.loadStatus,
required this.onRefresh,
2023-04-06 18:50:06 +02:00
this.refreshIndicatorOffset,
2023-04-06 18:27:57 +02:00
this.fab,
2023-04-30 23:36:02 +02:00
this.fabVisible,
2023-05-01 21:34:00 +02:00
this.noSliver,
2024-02-04 21:23:45 +01:00
this.listPadding,
this.heightFabHidden,
2023-12-09 04:04:14 +01:00
});
2023-04-06 18:10:23 +02:00
@override
Widget build(BuildContext context) {
2023-04-06 18:27:57 +02:00
final appConfigProvider = Provider.of<AppConfigProvider>(context);
2023-04-06 18:10:23 +02:00
switch (loadStatus) {
case LoadStatus.loading:
2023-04-30 23:36:02 +02:00
if (noSliver == true) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: loadingGenerator()
);
}
else {
return SafeArea(
top: false,
child: Builder(
builder: (BuildContext context) => CustomScrollView(
slivers: [
SliverOverlapInjector(
handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
2023-04-06 18:10:23 +02:00
),
2023-04-30 23:36:02 +02:00
SliverFillRemaining(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: loadingGenerator()
),
)
],
),
)
);
}
2023-04-06 18:10:23 +02:00
case LoadStatus.loaded:
2023-04-30 23:36:02 +02:00
if (noSliver == true) {
if (itemsCount > 0) {
2023-12-09 04:04:14 +01:00
return SafeArea(
child: Stack(
children: [
ListView.builder(
padding: listPadding,
itemCount: itemsCount,
itemBuilder: (context, index) => contentWidget(index),
),
if (fab != null) AnimatedPositioned(
duration: const Duration(milliseconds: 100),
curve: Curves.easeInOut,
bottom: fabVisible != null && fabVisible == true ?
appConfigProvider.showingSnackbar
? 70 : 20
: -70,
right: 20,
child: fab!
),
],
),
2023-04-30 23:36:02 +02:00
);
}
else {
2023-12-09 04:04:14 +01:00
return SafeArea(
child: Stack(
children: [
noData,
if (fab != null) AnimatedPositioned(
duration: const Duration(milliseconds: 100),
curve: Curves.easeInOut,
bottom: fabVisible != null && fabVisible == true ?
appConfigProvider.showingSnackbar
? 70 : 20
: -70,
right: 20,
child: fab!
),
],
),
2023-04-30 23:36:02 +02:00
);
}
}
else {
return Stack(
children: [
SafeArea(
top: false,
bottom: false,
child: Builder(
builder: (BuildContext context) {
return RefreshIndicator(
onRefresh: onRefresh,
2023-05-01 21:34:00 +02:00
edgeOffset: refreshIndicatorOffset ?? 70,
2023-04-30 23:36:02 +02:00
child: CustomScrollView(
slivers: <Widget>[
SliverOverlapInjector(
handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
2023-04-06 18:27:57 +02:00
),
2023-04-30 23:36:02 +02:00
if (itemsCount > 0) SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) => contentWidget(index),
childCount: itemsCount
),
),
if (itemsCount == 0) SliverFillRemaining(
child: noData,
)
],
),
);
},
),
2023-04-06 18:27:57 +02:00
),
2023-04-30 23:36:02 +02:00
if (fab != null) AnimatedPositioned(
duration: const Duration(milliseconds: 100),
curve: Curves.easeInOut,
bottom: fabVisible != null && fabVisible == true ?
appConfigProvider.showingSnackbar
2023-12-09 04:04:14 +01:00
? 90 : 20
2024-02-04 21:23:45 +01:00
: (heightFabHidden ?? -90),
2023-04-30 23:36:02 +02:00
right: 20,
2023-12-09 04:04:14 +01:00
child: SafeArea(child: fab!)
2023-04-30 23:36:02 +02:00
),
],
);
}
2023-04-06 18:10:23 +02:00
case LoadStatus.error:
2023-04-30 23:36:02 +02:00
if (noSliver == true) {
return Padding(
padding: const EdgeInsets.only(
top: 95,
left: 16,
right: 16
2023-04-06 18:10:23 +02:00
),
2023-04-30 23:36:02 +02:00
child: errorGenerator()
);
}
else {
return SafeArea(
top: false,
child: Builder(
builder: (BuildContext context) => CustomScrollView(
slivers: [
SliverOverlapInjector(
handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
),
SliverFillRemaining(
child: Padding(
padding: const EdgeInsets.only(
top: 95,
left: 16,
right: 16
),
child: errorGenerator()
),
)
],
),
)
);
}
2023-04-06 18:10:23 +02:00
default:
return const SizedBox();
}
}
}