92 lines
2.7 KiB
Dart
92 lines
2.7 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'property_model.dart';
|
||
|
||
class PropertyProvider with ChangeNotifier {
|
||
List<Property> _properties = <Property>[];
|
||
|
||
List<Property> get properties => _properties;
|
||
|
||
// Добавление недвижимости
|
||
void addProperty({
|
||
required String title,
|
||
String? description,
|
||
required String location,
|
||
required DateTime? daterent,
|
||
required double price,
|
||
PropertyType type = PropertyType.house,
|
||
}) {
|
||
final house = Property(
|
||
title: title,
|
||
description: description,
|
||
location: location,
|
||
daterent: daterent,
|
||
price: price,
|
||
type: type,
|
||
);
|
||
_properties.add(house);
|
||
notifyListeners();
|
||
}
|
||
|
||
// Обновление цены недвижимости
|
||
void updatePropertyPrice(Property property, double newPrice) {
|
||
property.price = newPrice;
|
||
notifyListeners();
|
||
}
|
||
|
||
// Удаление записи (о недвижимости)
|
||
void removeProperty(Property property) {
|
||
_properties.remove(property);
|
||
notifyListeners();
|
||
}
|
||
|
||
// Вывод
|
||
void printAvailableProperties(List<Property> properties) {
|
||
for (var property in properties) {
|
||
if (property.isAvailable) {
|
||
print('${property.title} is available for \$${property.price.toStringAsFixed(2)}');
|
||
}
|
||
}
|
||
}
|
||
|
||
// Сортировка недвижимости по цене
|
||
void sortPropertiesByPrice() {
|
||
_properties.sort((a, b) => a.price.compareTo(b.price));
|
||
notifyListeners();
|
||
}
|
||
|
||
// Изменение доступности недвижимости (переименовано в togglePropertyAvailability)
|
||
void togglePropertyAvailability(Property property) {
|
||
property.isAvailable = !property.isAvailable;
|
||
notifyListeners();
|
||
}
|
||
|
||
// Асинхронная загрузка записей (loadProperties)
|
||
Future<void> loadProperties() async {
|
||
// Имитируем задержку сети, запрос к бэку
|
||
await Future.delayed(Duration(seconds: 2));
|
||
|
||
if (_properties.isEmpty) {
|
||
_properties = [
|
||
Property(
|
||
title: "Apartment1",
|
||
type: PropertyType.apartment,
|
||
location: "Moscow",
|
||
daterent: DateTime.now().add(Duration(days: 1)),
|
||
price: 1200.0),
|
||
Property(
|
||
title: "Apartment2",
|
||
type: PropertyType.apartment,
|
||
location: "Moscow",
|
||
daterent: DateTime.now().add(Duration(days: 5)),
|
||
price: 1500.0),
|
||
Property(
|
||
title: "Flat2",
|
||
type: PropertyType.flat,
|
||
location: "Samara",
|
||
daterent: DateTime.now().add(Duration(days: 1)),
|
||
price: 1100.0),
|
||
];
|
||
}
|
||
notifyListeners();
|
||
}
|
||
} |