mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-11-27 19:54:55 +00:00
Refactor routine creation logic and add new dropdown events
This commit is contained in:
@ -1,12 +1,7 @@
|
||||
|
||||
|
||||
|
||||
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_web/pages/space_tree/bloc/space_tree_bloc.dart';
|
||||
import 'package:syncrow_web/pages/space_tree/bloc/space_tree_event.dart';
|
||||
import 'package:syncrow_web/pages/space_tree/bloc/space_tree_state.dart';
|
||||
import 'space_tree_dropdown_bloc.dart';
|
||||
|
||||
class DropdownMenuContent extends StatefulWidget {
|
||||
final String? selectedSpaceId;
|
||||
@ -14,6 +9,7 @@ class DropdownMenuContent extends StatefulWidget {
|
||||
final VoidCallback onClose;
|
||||
|
||||
const DropdownMenuContent({
|
||||
super.key,
|
||||
required this.selectedSpaceId,
|
||||
required this.onChanged,
|
||||
required this.onClose,
|
||||
@ -26,6 +22,7 @@ class DropdownMenuContent extends StatefulWidget {
|
||||
class _DropdownMenuContentState extends State<DropdownMenuContent> {
|
||||
final ScrollController _scrollController = ScrollController();
|
||||
final TextEditingController _searchController = TextEditingController();
|
||||
Timer? _debounceTimer;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@ -35,43 +32,49 @@ class _DropdownMenuContentState extends State<DropdownMenuContent> {
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_debounceTimer?.cancel();
|
||||
_scrollController.dispose();
|
||||
_searchController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _onScroll() {
|
||||
final bloc = context.read<SpaceTreeBloc>();
|
||||
final bloc = context.read<SpaceTreeDropdownBloc>();
|
||||
final state = bloc.state;
|
||||
if (_scrollController.position.pixels >=
|
||||
_scrollController.position.maxScrollExtent - 30) {
|
||||
if (state is SpaceTreeState && !state.paginationIsLoading) {
|
||||
bloc.add(PaginationEvent(state.paginationModel, state.communityList));
|
||||
if (state.paginationModel?.hasNext == true &&
|
||||
!state.paginationIsLoading) {
|
||||
bloc.add(PaginationEvent());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _handleSearch(String query) {
|
||||
_debounceTimer?.cancel();
|
||||
_debounceTimer = Timer(const Duration(milliseconds: 500), () {
|
||||
context.read<SpaceTreeDropdownBloc>().add(SearchQueryEvent(query));
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxHeight: 300),
|
||||
child: BlocBuilder<SpaceTreeBloc, SpaceTreeState>(
|
||||
child: BlocBuilder<SpaceTreeDropdownBloc, SpaceTreeDropdownState>(
|
||||
builder: (context, state) {
|
||||
final communities = state.searchQuery.isNotEmpty
|
||||
? state.filteredCommunity
|
||||
: state.communityList;
|
||||
? state.filteredCommunities
|
||||
: state.communities;
|
||||
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// Search bar
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: TextFormField(
|
||||
controller: _searchController,
|
||||
onChanged: (query) {
|
||||
context.read<SpaceTreeBloc>().add(SearchQueryEvent(query));
|
||||
},
|
||||
onChanged: _handleSearch,
|
||||
style: const TextStyle(fontSize: 14, color: Colors.black),
|
||||
decoration: InputDecoration(
|
||||
hintText: 'Search for space...',
|
||||
@ -85,7 +88,6 @@ class _DropdownMenuContentState extends State<DropdownMenuContent> {
|
||||
),
|
||||
),
|
||||
),
|
||||
// Community list
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
controller: _scrollController,
|
||||
@ -121,19 +123,12 @@ class _DropdownMenuContentState extends State<DropdownMenuContent> {
|
||||
),
|
||||
),
|
||||
onTap: () {
|
||||
setState(() {
|
||||
_searchController.clear();
|
||||
_searchController.text.isEmpty
|
||||
? context
|
||||
.read<SpaceTreeBloc>()
|
||||
.add(SearchQueryEvent(''))
|
||||
: context.read<SpaceTreeBloc>().add(
|
||||
SearchQueryEvent(_searchController.text));
|
||||
});
|
||||
// Future.delayed(const Duration(seconds: 1), () {
|
||||
context
|
||||
.read<SpaceTreeDropdownBloc>()
|
||||
.add(SearchQueryEvent(''));
|
||||
|
||||
widget.onChanged(community.uuid);
|
||||
widget.onClose();
|
||||
// });
|
||||
},
|
||||
);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user