2023-06-17 20:57:57 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2023-08-30 19:48:25 +03:00
|
|
|
import 'package:flutter/services.dart';
|
2023-06-17 20:57:57 +03:00
|
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:iconsax/iconsax.dart';
|
|
|
|
import 'package:rain/app/api/api.dart';
|
|
|
|
import 'package:rain/app/api/city.dart';
|
|
|
|
import 'package:rain/app/controller/controller.dart';
|
2023-07-15 21:51:32 +03:00
|
|
|
import 'package:rain/app/modules/list_weather_card.dart';
|
2023-06-17 20:57:57 +03:00
|
|
|
import 'package:rain/app/modules/settings.dart';
|
|
|
|
import 'package:rain/app/modules/weather.dart';
|
|
|
|
import 'package:rain/app/widgets/create_card_weather.dart';
|
|
|
|
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-06-28 23:10:43 +03:00
|
|
|
late TabController tabController;
|
2023-08-04 21:19:30 +03:00
|
|
|
final locationController = Get.put(LocationController());
|
|
|
|
bool visible = false;
|
|
|
|
final _controller = TextEditingController();
|
|
|
|
final _focusNode = FocusNode();
|
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 {
|
|
|
|
await locationController.deleteCache();
|
|
|
|
await locationController.updateCacheCard(false);
|
|
|
|
await locationController.setLocation();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-30 19:48:25 +03:00
|
|
|
SystemChrome.setSystemUIOverlayStyle(
|
|
|
|
SystemUiOverlayStyle(
|
|
|
|
systemNavigationBarColor:
|
|
|
|
context.theme.navigationBarTheme.backgroundColor,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2023-06-28 23:10:43 +03:00
|
|
|
return DefaultTabController(
|
|
|
|
length: pages.length,
|
|
|
|
child: Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
centerTitle: true,
|
|
|
|
automaticallyImplyLeading: false,
|
2023-07-12 20:52:25 +03:00
|
|
|
leading: const Icon(
|
2023-07-07 22:00:39 +03:00
|
|
|
Iconsax.location,
|
|
|
|
size: 18,
|
|
|
|
),
|
2023-06-28 23:10:43 +03:00
|
|
|
title: visible
|
|
|
|
? RawAutocomplete<Result>(
|
|
|
|
focusNode: _focusNode,
|
|
|
|
textEditingController: _controller,
|
|
|
|
fieldViewBuilder: (BuildContext context,
|
|
|
|
TextEditingController fieldTextEditingController,
|
|
|
|
FocusNode fieldFocusNode,
|
|
|
|
VoidCallback onFieldSubmitted) {
|
|
|
|
return TextField(
|
|
|
|
controller: _controller,
|
|
|
|
focusNode: _focusNode,
|
2023-07-14 20:19:43 +03:00
|
|
|
style:
|
|
|
|
context.textTheme.labelLarge?.copyWith(fontSize: 16),
|
2023-06-28 23:10:43 +03:00
|
|
|
decoration: InputDecoration(
|
|
|
|
hintText: 'search'.tr,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
optionsBuilder: (TextEditingValue textEditingValue) {
|
|
|
|
if (textEditingValue.text.isEmpty) {
|
|
|
|
return const Iterable<Result>.empty();
|
|
|
|
}
|
|
|
|
return WeatherAPI()
|
|
|
|
.getSuggestions(textEditingValue.text, locale);
|
|
|
|
},
|
|
|
|
onSelected: (Result selection) async {
|
|
|
|
await locationController.deleteAll(true);
|
|
|
|
await locationController.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}',
|
2023-07-14 20:19:43 +03:00
|
|
|
style: context.textTheme.labelLarge,
|
2023-06-28 23:10:43 +03:00
|
|
|
),
|
2023-06-17 20:57:57 +03:00
|
|
|
),
|
2023-06-28 23:10:43 +03:00
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2023-06-17 20:57:57 +03:00
|
|
|
),
|
|
|
|
),
|
2023-06-28 23:10:43 +03:00
|
|
|
);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
: Obx(() => Text(
|
|
|
|
locationController.isLoading.isFalse
|
|
|
|
? locationController.location.district!.isEmpty
|
|
|
|
? '${locationController.location.city}'
|
|
|
|
: locationController.location.city!.isEmpty
|
|
|
|
? '${locationController.location.district}'
|
|
|
|
: locationController.location.city ==
|
|
|
|
locationController.location.district
|
|
|
|
? '${locationController.location.city}'
|
|
|
|
: '${locationController.location.city}'
|
|
|
|
', ${locationController.location.district}'
|
|
|
|
: settings.location
|
|
|
|
? 'search'.tr
|
|
|
|
: locationController.isSearch.isFalse
|
|
|
|
? 'loading'.tr
|
|
|
|
: 'searchCity'.tr,
|
2023-07-09 23:41:51 +03:00
|
|
|
style: context.textTheme.titleMedium?.copyWith(
|
2023-06-28 23:10:43 +03:00
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
fontSize: 18,
|
2023-06-17 20:57:57 +03:00
|
|
|
),
|
2023-06-28 23:10:43 +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-06-17 20:57:57 +03:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2023-06-28 23:10:43 +03:00
|
|
|
body: SafeArea(
|
2023-07-17 20:53:23 +03:00
|
|
|
child: Row(
|
|
|
|
children: [
|
|
|
|
// Row(
|
|
|
|
// children: [
|
|
|
|
// NavigationRail(
|
|
|
|
// selectedIndex: tabIndex,
|
|
|
|
// labelType: NavigationRailLabelType.all,
|
|
|
|
// onDestinationSelected: (int index) => changeTabIndex(index),
|
|
|
|
// destinations: [
|
|
|
|
// NavigationRailDestination(
|
|
|
|
// icon: const Icon(Iconsax.cloud_sunny),
|
|
|
|
// selectedIcon: const Icon(Iconsax.cloud_sunny5),
|
|
|
|
// label: Text('name'.tr),
|
|
|
|
// ),
|
|
|
|
// NavigationRailDestination(
|
|
|
|
// icon: const Icon(Iconsax.map_1),
|
|
|
|
// selectedIcon: const Icon(Iconsax.map5),
|
|
|
|
// label: Text('city'.tr),
|
|
|
|
// ),
|
|
|
|
// NavigationRailDestination(
|
|
|
|
// icon: const Icon(Iconsax.category),
|
|
|
|
// selectedIcon: const Icon(Iconsax.category5),
|
|
|
|
// label: Text('settings'.tr),
|
|
|
|
// ),
|
|
|
|
// ],
|
|
|
|
// ),
|
|
|
|
// const VerticalDivider(thickness: 1, width: 1),
|
|
|
|
// ],
|
|
|
|
// ),
|
|
|
|
Expanded(
|
|
|
|
child: TabBarView(
|
|
|
|
controller: tabController,
|
|
|
|
children: pages,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
2023-06-28 23:10:43 +03:00
|
|
|
),
|
2023-06-17 20:57:57 +03:00
|
|
|
),
|
2023-07-12 20:52:25 +03:00
|
|
|
bottomNavigationBar: NavigationBar(
|
|
|
|
onDestinationSelected: (int index) => changeTabIndex(index),
|
|
|
|
selectedIndex: tabIndex,
|
2023-07-17 20:53:23 +03:00
|
|
|
destinations: [
|
2023-07-12 20:52:25 +03:00
|
|
|
NavigationDestination(
|
|
|
|
icon: const Icon(Iconsax.cloud_sunny),
|
|
|
|
selectedIcon: const Icon(Iconsax.cloud_sunny5),
|
|
|
|
label: 'name'.tr,
|
|
|
|
),
|
|
|
|
NavigationDestination(
|
2023-07-15 21:51:32 +03:00
|
|
|
icon: const Icon(Iconsax.map_1),
|
|
|
|
selectedIcon: const Icon(Iconsax.map5),
|
2023-07-12 20:52:25 +03:00
|
|
|
label: 'city'.tr,
|
|
|
|
),
|
|
|
|
NavigationDestination(
|
|
|
|
icon: const Icon(Iconsax.category),
|
|
|
|
selectedIcon: const Icon(Iconsax.category5),
|
|
|
|
label: 'settings'.tr,
|
|
|
|
),
|
|
|
|
],
|
2023-06-17 20:57:57 +03:00
|
|
|
),
|
2023-06-28 23:10:43 +03:00
|
|
|
floatingActionButton: tabIndex == 1
|
|
|
|
? FloatingActionButton(
|
|
|
|
onPressed: () => showModalBottomSheet(
|
|
|
|
context: context,
|
|
|
|
isScrollControlled: true,
|
2023-07-12 20:52:25 +03:00
|
|
|
enableDrag: false,
|
|
|
|
builder: (BuildContext context) => const CreateWeatherCard(),
|
2023-06-28 23:10:43 +03:00
|
|
|
),
|
|
|
|
child: const Icon(Iconsax.add),
|
|
|
|
)
|
|
|
|
: null,
|
2023-06-17 20:57:57 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|