202 lines
7.3 KiB
Dart
202 lines
7.3 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_html/flutter_html.dart';
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:traderscircuit/Utils/Common/CommonAppbar.dart';
|
|
import 'package:traderscircuit/Utils/Common/CustomTextFormField.dart';
|
|
import 'package:traderscircuit/Utils/text.dart';
|
|
import 'package:traderscircuit/model/HomeModel/search_model.dart';
|
|
import 'package:traderscircuit/resources/routes/route_name.dart';
|
|
import 'package:traderscircuit/view/MainScreen/stockDetails/stock_details_screen.dart';
|
|
import 'package:traderscircuit/view/onBoarding/splashScreen1.dart';
|
|
import 'package:traderscircuit/view_model/HomeApi/Search_api.dart';
|
|
import 'dart:async';
|
|
|
|
class Search extends StatefulWidget {
|
|
const Search({super.key});
|
|
|
|
@override
|
|
State<Search> createState() => _SearchState();
|
|
}
|
|
|
|
class _SearchState extends State<Search> {
|
|
RxBool isLoading = true.obs;
|
|
SearchModel searchModel = SearchModel();
|
|
TextEditingController searchController = TextEditingController();
|
|
Timer? _debounce;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadSearchData();
|
|
}
|
|
|
|
Future<void> _loadSearchData() async {
|
|
if (searchController.text.isEmpty || searchController.text.length < 2) {
|
|
setState(() {
|
|
searchModel = SearchModel(data: []);
|
|
isLoading.value = false;
|
|
});
|
|
return;
|
|
}
|
|
|
|
final value =
|
|
await SearchAPI().searchData(searchText: searchController.text);
|
|
setState(() {
|
|
searchModel = SearchModel.fromJson(value.data);
|
|
isLoading.value = false;
|
|
});
|
|
}
|
|
|
|
void _onSearchChanged() {
|
|
if (_debounce?.isActive ?? false) _debounce?.cancel();
|
|
_debounce = Timer(const Duration(milliseconds: 300), () {
|
|
setState(() {
|
|
isLoading.value = true;
|
|
});
|
|
_loadSearchData();
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_debounce?.cancel();
|
|
searchController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
toolbarHeight: 70.h,
|
|
scrolledUnderElevation: 0.0,
|
|
backgroundColor: Colors.black,
|
|
elevation: 0,
|
|
automaticallyImplyLeading: false,
|
|
titleSpacing: 0,
|
|
centerTitle: true,
|
|
leading: GestureDetector(
|
|
onTap: () {
|
|
Get.back();
|
|
},
|
|
child: Center(
|
|
child: Icon(
|
|
Icons.arrow_back_ios,
|
|
color: Colors.white,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
// appBar: const CommonAppbar(
|
|
// titleTxt: "",
|
|
// ),
|
|
backgroundColor: Colors.black,
|
|
extendBody: true,
|
|
body: Obx(
|
|
() => Stack(
|
|
children: [
|
|
const CommonBlurLeft(),
|
|
const CommonBlurRight(),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
|
|
child: Column(
|
|
children: [
|
|
CustomTextFormField(
|
|
autofocus: true,
|
|
onInput: (p0) {
|
|
_onSearchChanged();
|
|
},
|
|
leadingIcon: const Icon(
|
|
Icons.search,
|
|
color: Colors.white,
|
|
),
|
|
hintText: "Search",
|
|
texttype: TextInputType.text,
|
|
textEditingController: searchController,
|
|
),
|
|
SizedBox(
|
|
height: 10.h,
|
|
),
|
|
Expanded(
|
|
child: isLoading.value
|
|
? const Center(
|
|
child: CircularProgressIndicator(
|
|
color: Color(0xFF0093FF),
|
|
),
|
|
)
|
|
: searchModel.data == null || searchModel.data!.isEmpty
|
|
? const Center(
|
|
child: Text(
|
|
"Try Searching For TATA MOTORS, HCL...",
|
|
style: TextStyle(color: Colors.white),
|
|
),
|
|
)
|
|
: ListView.separated(
|
|
shrinkWrap: true,
|
|
itemCount: searchModel.data?.length ?? 0,
|
|
itemBuilder: (context, index) {
|
|
return InkWell(
|
|
onTap: () {
|
|
Get.to(const StockDetailsScreen(),
|
|
arguments: {
|
|
"instrument_name": searchModel.data!
|
|
.elementAt(index)
|
|
.instrumentKey,
|
|
});
|
|
},
|
|
child: Row(
|
|
children: [
|
|
Image.asset(
|
|
"assets/images/png/search.png",
|
|
height: 35.h,
|
|
width: 35.w,
|
|
),
|
|
SizedBox(
|
|
width: 25.w,
|
|
),
|
|
Column(
|
|
crossAxisAlignment:
|
|
CrossAxisAlignment.start,
|
|
children: [
|
|
SizedBox(
|
|
width: 230.w,
|
|
child: text16W500(searchModel
|
|
.data!
|
|
.elementAt(index)
|
|
.name ??
|
|
""),
|
|
),
|
|
SizedBox(
|
|
width: 297.w,
|
|
child: text12W400(
|
|
searchModel.data!
|
|
.elementAt(index)
|
|
.tradingSymbol ??
|
|
"",
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
separatorBuilder:
|
|
(BuildContext context, int index) =>
|
|
const Divider(
|
|
color: Color(0xff242424),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|