55 lines
1.4 KiB
Dart
55 lines
1.4 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
abstract class LoadCityEvent {}
|
|
|
|
class LoadAllCity extends LoadCityEvent {}
|
|
|
|
class SearchCity extends LoadCityEvent {
|
|
final String query;
|
|
|
|
SearchCity(this.query);
|
|
}
|
|
|
|
// ----- State -----
|
|
class CityState {
|
|
final List<Map<String, String>> offers;
|
|
|
|
const CityState(this.offers);
|
|
}
|
|
|
|
// ----- Bloc -----
|
|
class SearchCityBloc extends Bloc<LoadCityEvent, CityState> {
|
|
SearchCityBloc() : super(const CityState([])) {
|
|
on<LoadAllCity>(_onLoadCity);
|
|
on<SearchCity>(_onSearchCity);
|
|
}
|
|
|
|
final List<Map<String, String>> _allOffers = [
|
|
{"image": "assets/images/aa1.png", "title": "Sydney"},
|
|
{"image": "assets/images/aa2.png", "title": "New York"},
|
|
{"image": "assets/images/aa3.png", "title": "Abu Dhabi"},
|
|
{"image": "assets/images/aa4.png", "title": "Dubai"},
|
|
{
|
|
"image": "assets/images/card_banner.png",
|
|
"title": "Tokyo",
|
|
},
|
|
{"image": "assets/images/city_germany.jpg", "title": "Ontario"},
|
|
{"image": "assets/images/aa2.png", "title": "Mumbai"},
|
|
{"image": "assets/images/aa3.png", "title": "Louisiana"},
|
|
];
|
|
|
|
void _onLoadCity(event, emit) {
|
|
emit(CityState(_allOffers));
|
|
}
|
|
|
|
void _onSearchCity(event, emit) {
|
|
final filtered = _allOffers
|
|
.where(
|
|
(offer) =>
|
|
offer["title"]!.toLowerCase().contains(event.query.toLowerCase()),
|
|
)
|
|
.toList();
|
|
emit(CityState(filtered));
|
|
}
|
|
}
|