ente/lib/ui/viewer/search_tab/search_tab.dart

157 lines
5.3 KiB
Dart
Raw Normal View History

import "package:fade_indexed_stack/fade_indexed_stack.dart";
2023-02-25 08:00:42 +00:00
import "package:flutter/material.dart";
import "package:flutter_animate/flutter_animate.dart";
import "package:photos/core/constants.dart";
import "package:photos/models/search/album_search_result.dart";
import "package:photos/models/search/index_of_indexed_stack.dart";
2023-02-25 08:00:42 +00:00
import "package:photos/models/search/search_types.dart";
import "package:photos/states/all_sections_examples_state.dart";
import "package:photos/ui/common/loading_widget.dart";
import "package:photos/ui/viewer/search/result/no_result_widget.dart";
2023-02-25 08:00:42 +00:00
import "package:photos/ui/viewer/search/search_section.dart";
import "package:photos/ui/viewer/search/search_suggestions.dart";
2023-10-31 11:04:32 +00:00
import "package:photos/ui/viewer/search/tab_empty_state.dart";
import 'package:photos/ui/viewer/search_tab/albums_section.dart';
2023-02-23 17:14:56 +00:00
class SearchTab extends StatefulWidget {
const SearchTab({Key? key}) : super(key: key);
2023-02-25 08:00:42 +00:00
2023-02-23 17:14:56 +00:00
@override
State<SearchTab> createState() => _SearchTabState();
}
class _SearchTabState extends State<SearchTab> {
late int index;
final indexOfStackNotifier = IndexOfStackNotifier();
@override
void initState() {
super.initState();
index = indexOfStackNotifier.index;
indexOfStackNotifier.addListener(indexNotifierListener);
}
void indexNotifierListener() {
setState(() {
index = indexOfStackNotifier.index;
});
}
@override
void dispose() {
indexOfStackNotifier.removeListener(indexNotifierListener);
indexOfStackNotifier.dispose();
super.dispose();
}
2023-02-25 08:00:42 +00:00
2023-02-23 17:14:56 +00:00
@override
Widget build(BuildContext context) {
2023-12-20 13:50:05 +00:00
return AllSectionsExamplesProvider(
child: FadeIndexedStack(
lazy: false,
2023-12-20 13:50:05 +00:00
duration: const Duration(milliseconds: 150),
index: index,
2023-12-23 15:56:29 +00:00
children: const [
AllSearchSections(),
SearchSuggestionsWidget(),
NoResultWidget(),
2023-12-20 13:50:05 +00:00
],
),
2023-02-25 08:00:42 +00:00
);
}
2023-02-23 17:14:56 +00:00
}
class AllSearchSections extends StatefulWidget {
const AllSearchSections({super.key});
@override
State<AllSearchSections> createState() => _AllSearchSectionsState();
}
class _AllSearchSectionsState extends State<AllSearchSections> {
@override
Widget build(BuildContext context) {
final searchTypes = SectionType.values.toList(growable: true);
// remove face and content sectionType
searchTypes.remove(SectionType.face);
searchTypes.remove(SectionType.content);
2023-12-20 13:50:05 +00:00
return Padding(
padding: const EdgeInsets.only(top: 8),
child: Stack(
children: [
FutureBuilder(
future: InheritedAllSectionsExamples.of(context)
.allSectionsExamplesFuture,
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data!.isNotEmpty) {
2023-12-20 13:50:05 +00:00
if (snapshot.data!.every((element) => element.isEmpty)) {
return const Padding(
padding: EdgeInsets.only(bottom: 72),
child: SearchTabEmptyState(),
);
}
return ListView.builder(
padding: const EdgeInsets.only(bottom: 180),
physics: const BouncingScrollPhysics(),
itemCount: searchTypes.length,
itemBuilder: (context, index) {
switch (searchTypes[index]) {
case SectionType.album:
return AlbumsSection(
snapshot.data!.elementAt(index)
as List<AlbumSearchResult>,
);
default:
return SearchSection(
sectionType: searchTypes[index],
examples: snapshot.data!.elementAt(index),
2024-02-15 10:46:02 +00:00
limit: kSearchSectionLimit,
);
}
2023-12-20 13:50:05 +00:00
},
)
.animate(
delay: const Duration(milliseconds: 150),
)
.slide(
begin: const Offset(0, -0.015),
end: const Offset(0, 0),
duration: const Duration(milliseconds: 300),
curve: Curves.easeOut,
)
.fadeIn(
duration: const Duration(milliseconds: 150),
curve: Curves.easeOut,
);
2023-12-20 13:50:05 +00:00
} else if (snapshot.hasError) {
//Errors are handled and this else if condition will be false always
//is the understanding.
return const Padding(
padding: EdgeInsets.only(bottom: 72),
2023-12-20 13:50:05 +00:00
child: EnteLoadingWidget(),
);
} else {
return const Padding(
padding: EdgeInsets.only(bottom: 72),
child: EnteLoadingWidget(),
);
}
2023-12-20 13:50:05 +00:00
},
),
ValueListenableBuilder(
valueListenable:
InheritedAllSectionsExamples.of(context).isDebouncingNotifier,
builder: (context, value, _) {
return value
? const EnteLoadingWidget(
alignment: Alignment.topRight,
)
: const SizedBox.shrink();
},
),
],
),
);
}
}