2023-06-17 20:57:57 +03:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:iconsax/iconsax.dart';
|
2023-09-19 15:26:59 +03:00
|
|
|
import 'package:isar/isar.dart';
|
2023-06-17 20:57:57 +03:00
|
|
|
import 'package:rain/app/api/api.dart';
|
|
|
|
import 'package:rain/app/api/city.dart';
|
|
|
|
import 'package:rain/app/controller/controller.dart';
|
2023-09-19 15:26:59 +03:00
|
|
|
import 'package:rain/app/data/weather.dart';
|
2023-09-03 09:08:43 +03:00
|
|
|
import 'package:rain/app/modules/cards/view/list_weather_card.dart';
|
|
|
|
import 'package:rain/app/modules/settings/view/settings.dart';
|
|
|
|
import 'package:rain/app/modules/main/view/weather.dart';
|
|
|
|
import 'package:rain/app/modules/cards/widgets/create_card_weather.dart';
|
2023-09-05 21:31:29 +03:00
|
|
|
import 'package:rain/app/services/utils.dart';
|
2023-06-17 20:57:57 +03:00
|
|
|
import 'package:rain/main.dart';
|
|
|
|
|
|
|
|
class HomePage extends StatefulWidget {
|
|
|
|
const HomePage({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<HomePage> createState() => _HomePageState();
|
|
|
|
}
|
|
|
|
|
2023-06-28 23:10:43 +03:00
|
|
|
class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
|
2023-06-17 20:57:57 +03:00
|
|
|
int tabIndex = 0;
|
2023-09-03 09:08:43 +03:00
|
|
|
bool visible = false;
|
|
|
|
final _focusNode = FocusNode();
|
2023-06-28 23:10:43 +03:00
|
|
|
late TabController tabController;
|
2023-09-01 20:18:40 +03:00
|
|
|
final weatherController = Get.put(WeatherController());
|
2023-08-04 21:19:30 +03:00
|
|
|
final _controller = TextEditingController();
|
2023-06-28 23:10:43 +03:00
|
|
|
|
|
|
|
final pages = [
|
|
|
|
const WeatherPage(),
|
2023-07-15 21:51:32 +03:00
|
|
|
const ListWeatherCard(),
|
2023-06-28 23:10:43 +03:00
|
|
|
const SettingsPage(),
|
|
|
|
];
|
2023-06-17 20:57:57 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
getData();
|
2023-06-28 23:10:43 +03:00
|
|
|
tabController = TabController(
|
|
|
|
initialIndex: tabIndex,
|
|
|
|
length: pages.length,
|
|
|
|
vsync: this,
|
|
|
|
);
|
|
|
|
tabController.addListener(() {
|
|
|
|
setState(() {
|
|
|
|
tabIndex = tabController.index;
|
|
|
|
});
|
|
|
|
});
|
2023-06-17 20:57:57 +03:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void getData() async {
|
2023-09-01 20:18:40 +03:00
|
|
|
await weatherController.deleteCache();
|
|
|
|
await weatherController.updateCacheCard(false);
|
|
|
|
await weatherController.setLocation();
|
2023-06-17 20:57:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void changeTabIndex(int index) {
|
|
|
|
setState(() {
|
|
|
|
tabIndex = index;
|
|
|
|
});
|
2023-07-12 20:52:25 +03:00
|
|
|
tabController.animateTo(tabIndex);
|
2023-06-17 20:57:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-06-28 23:10:43 +03:00
|
|
|
return DefaultTabController(
|
|
|
|
length: pages.length,
|
2023-09-05 21:31:29 +03:00
|
|
|
child: ScaffoldMessenger(
|
|
|
|
key: globalKey,
|
|
|
|
child: Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
centerTitle: true,
|
|
|
|
automaticallyImplyLeading: false,
|
|
|
|
leading: const Icon(
|
|
|
|
Iconsax.location,
|
|
|
|
size: 18,
|
|
|
|
),
|
|
|
|
title: visible
|
|
|
|
? RawAutocomplete<Result>(
|
|
|
|
focusNode: _focusNode,
|
|
|
|
textEditingController: _controller,
|
|
|
|
fieldViewBuilder: (BuildContext context,
|
|
|
|
TextEditingController fieldTextEditingController,
|
|
|
|
FocusNode fieldFocusNode,
|
|
|
|
VoidCallback onFieldSubmitted) {
|
|
|
|
return TextField(
|
|
|
|
controller: _controller,
|
|
|
|
focusNode: _focusNode,
|
|
|
|
style: context.textTheme.labelLarge
|
|
|
|
?.copyWith(fontSize: 16),
|
|
|
|
decoration: InputDecoration(
|
|
|
|
hintText: 'search'.tr,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
optionsBuilder: (TextEditingValue textEditingValue) {
|
|
|
|
if (textEditingValue.text.isEmpty) {
|
|
|
|
return const Iterable<Result>.empty();
|
|
|
|
}
|
|
|
|
return WeatherAPI()
|
2023-09-09 21:15:47 +03:00
|
|
|
.getCity(textEditingValue.text, locale);
|
2023-09-05 21:31:29 +03:00
|
|
|
},
|
|
|
|
onSelected: (Result selection) async {
|
|
|
|
await weatherController.deleteAll(true);
|
|
|
|
await weatherController.getLocation(
|
|
|
|
double.parse('${selection.latitude}'),
|
|
|
|
double.parse('${selection.longitude}'),
|
|
|
|
selection.admin1,
|
|
|
|
selection.name,
|
|
|
|
);
|
|
|
|
visible = false;
|
|
|
|
_controller.clear();
|
|
|
|
_focusNode.unfocus();
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
displayStringForOption: (Result option) =>
|
|
|
|
'${option.name}, ${option.admin1}',
|
|
|
|
optionsViewBuilder: (BuildContext context,
|
|
|
|
AutocompleteOnSelected<Result> onSelected,
|
|
|
|
Iterable<Result> options) {
|
|
|
|
return Align(
|
|
|
|
alignment: Alignment.topLeft,
|
|
|
|
child: Material(
|
|
|
|
borderRadius: BorderRadius.circular(20),
|
|
|
|
elevation: 4.0,
|
|
|
|
child: SizedBox(
|
|
|
|
width: 250,
|
|
|
|
child: ListView.builder(
|
|
|
|
padding: EdgeInsets.zero,
|
|
|
|
shrinkWrap: true,
|
|
|
|
itemCount: options.length,
|
|
|
|
itemBuilder: (BuildContext context, int index) {
|
|
|
|
final Result option = options.elementAt(index);
|
|
|
|
return InkWell(
|
|
|
|
onTap: () => onSelected(option),
|
|
|
|
child: ListTile(
|
|
|
|
title: Text(
|
|
|
|
'${option.name}, ${option.admin1}',
|
|
|
|
style: context.textTheme.labelLarge,
|
|
|
|
),
|
2023-06-28 23:10:43 +03:00
|
|
|
),
|
2023-09-05 21:31:29 +03:00
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2023-06-28 23:10:43 +03:00
|
|
|
),
|
2023-06-17 20:57:57 +03:00
|
|
|
),
|
2023-09-05 21:31:29 +03:00
|
|
|
);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
: Obx(() => Text(
|
|
|
|
weatherController.isLoading.isFalse
|
|
|
|
? weatherController.location.district!.isEmpty
|
|
|
|
? '${weatherController.location.city}'
|
|
|
|
: weatherController.location.city!.isEmpty
|
|
|
|
? '${weatherController.location.district}'
|
|
|
|
: weatherController.location.city ==
|
|
|
|
weatherController.location.district
|
|
|
|
? '${weatherController.location.city}'
|
|
|
|
: '${weatherController.location.city}'
|
|
|
|
', ${weatherController.location.district}'
|
|
|
|
: settings.location
|
|
|
|
? 'search'.tr
|
2023-09-19 15:26:59 +03:00
|
|
|
: (isar.locationCaches.where().findAllSync())
|
|
|
|
.isNotEmpty
|
2023-09-05 21:31:29 +03:00
|
|
|
? 'loading'.tr
|
|
|
|
: 'searchCity'.tr,
|
|
|
|
style: context.textTheme.titleMedium?.copyWith(
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
fontSize: 18,
|
2023-06-17 20:57:57 +03:00
|
|
|
),
|
2023-09-05 21:31:29 +03:00
|
|
|
)),
|
|
|
|
actions: [
|
|
|
|
IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
if (visible) {
|
|
|
|
_controller.clear();
|
|
|
|
_focusNode.unfocus();
|
|
|
|
visible = false;
|
|
|
|
} else {
|
|
|
|
visible = true;
|
|
|
|
}
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
icon: Icon(
|
|
|
|
visible ? Icons.close : Iconsax.search_normal_1,
|
|
|
|
size: 18,
|
2023-07-17 20:53:23 +03:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
2023-06-28 23:10:43 +03:00
|
|
|
),
|
2023-09-05 21:31:29 +03:00
|
|
|
body: SafeArea(
|
2023-09-08 20:35:56 +03:00
|
|
|
child: TabBarView(
|
|
|
|
controller: tabController,
|
|
|
|
children: pages,
|
2023-07-12 20:52:25 +03:00
|
|
|
),
|
2023-09-05 21:31:29 +03:00
|
|
|
),
|
|
|
|
bottomNavigationBar: NavigationBar(
|
|
|
|
onDestinationSelected: (int index) => changeTabIndex(index),
|
|
|
|
selectedIndex: tabIndex,
|
|
|
|
destinations: [
|
|
|
|
NavigationDestination(
|
|
|
|
icon: const Icon(Iconsax.cloud_sunny),
|
|
|
|
selectedIcon: const Icon(Iconsax.cloud_sunny5),
|
|
|
|
label: 'name'.tr,
|
|
|
|
),
|
|
|
|
NavigationDestination(
|
|
|
|
icon: const Icon(Iconsax.map_1),
|
|
|
|
selectedIcon: const Icon(Iconsax.map5),
|
|
|
|
label: 'city'.tr,
|
|
|
|
),
|
|
|
|
NavigationDestination(
|
|
|
|
icon: const Icon(Iconsax.category),
|
|
|
|
selectedIcon: const Icon(Iconsax.category5),
|
|
|
|
label: 'settings'.tr,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
floatingActionButton: tabIndex == 1
|
|
|
|
? FloatingActionButton(
|
|
|
|
onPressed: () => showModalBottomSheet(
|
|
|
|
context: context,
|
|
|
|
isScrollControlled: true,
|
|
|
|
enableDrag: false,
|
|
|
|
builder: (BuildContext context) =>
|
|
|
|
const CreateWeatherCard(),
|
|
|
|
),
|
|
|
|
child: const Icon(Iconsax.add),
|
|
|
|
)
|
|
|
|
: null,
|
2023-06-17 20:57:57 +03:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|