import 'package:firebase_analytics/firebase_analytics.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/painting.dart'; import 'package:flutter/services.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:tangheem/api/tangheem_user_api_client.dart'; import 'package:tangheem/app_state/app_state.dart'; import 'package:tangheem/classes/colors.dart'; import 'package:tangheem/classes/utils.dart'; import 'package:tangheem/models/content_info_model.dart'; import 'package:tangheem/models/surah_model.dart'; import 'package:tangheem/models/tangheem_type_model.dart'; import 'package:tangheem/ui/dialogs/general_dialog.dart'; import 'package:tangheem/ui/screens/tangheem_screen.dart'; import 'package:tangheem/widgets/common_dropdown_button.dart'; class HomeScreen extends StatefulWidget { static const String routeName = "/"; final FirebaseAnalytics analytics; HomeScreen(this.analytics, {Key key}) : super(key: key); @override _HomeScreenState createState() { return _HomeScreenState(); } } class _HomeScreenState extends State { TextEditingController _searchController = TextEditingController(); FocusNode _searchFocusNode = FocusNode(); ValueNotifier> _tangheemListNotifier = ValueNotifier([]); List _surahList = []; int _selectedSurah = -1; int _selectedTangheemType = -1; SurahModel _surahModel; TangheemType _tangheemType; ContentInfoModel _contentInfoModel; @override void initState() { super.initState(); SystemChannels.textInput.invokeMethod('TextInput.hide'); getSurahAndTangheemTypes(); } void getSurahAndTangheemTypes() async { Utils.showLoading(context); try { _contentInfoModel = await TangheemUserApiClient().getContentInfo(4); _surahModel = await TangheemUserApiClient().getSurahs(); AppState().setSurahModel(_surahModel); _surahList = _surahModel.data.map((element) => element.nameAR).toList(); await getTangheemTypes(); TangheemUserApiClient().addStatistics(1); AppState().setAnalytics(widget.analytics); await widget.analytics.setAnalyticsCollectionEnabled(true); await widget.analytics.setCurrentScreen(screenName: "home"); setState(() {}); Utils.hideLoading(context); } catch (ex) { Utils.hideLoading(context); Utils.handleException(ex, null); } checkScreenMode(); } void checkScreenMode() { WidgetsBinding.instance.addPostFrameCallback((_) { if (MediaQuery.of(context).orientation == Orientation.portrait) { showDialog( context: context, barrierColor: ColorConsts.secondaryWhite.withOpacity(0.8), builder: (BuildContext context) => GeneralDialog(), ); } }); } Future getTangheemTypes() async { try { _tangheemType = await TangheemUserApiClient().getTangheemType(); _tangheemListNotifier.value = _tangheemType?.data?.where((element) => element.isActive)?.toList()?.map((element) => element.tangheemTypeName)?.toList() ?? []; } catch (ex) {} } @override void dispose() { super.dispose(); } @override Widget build(BuildContext context) { return SizedBox( width: double.infinity, child: SingleChildScrollView( padding: EdgeInsets.fromLTRB(16, 16, 16, 0), physics: BouncingScrollPhysics(), child: Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ Text( "موسوعة الأداء القرآني", style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20, color: ColorConsts.primaryBlue, height: 1), ), SizedBox(height: 4), Text( "للأساليب اللغوية", style: TextStyle(fontSize: 20, color: ColorConsts.primaryBlue, height: 1), ), SizedBox(height: 8), Text( Utils.getNotNullValue(_contentInfoModel?.data ?? [], 0)?.content ?? "", style: TextStyle(fontSize: 14, color: ColorConsts.textGrey, height: 1), ), SizedBox(height: 32), Row( children: [ Expanded( child: ValueListenableBuilder( valueListenable: _tangheemListNotifier, builder: (context, value, child) { return CommonDropDownButton(_selectedTangheemType, hintText: "اختر الأسلوب اللغوي", list: value, onSelect: (index) { if (_selectedTangheemType != index) { setState(() { _selectedTangheemType = index; }); } }); }, ), ), SizedBox(width: 8), Expanded( child: CommonDropDownButton(_selectedSurah, hintText: "اختر السورة", list: _surahList, onSelect: (index) { if (_selectedSurah != index) { setState(() { _selectedSurah = index; }); } }), ) ], ), SizedBox(height: 16), InkWell( splashColor: Colors.transparent, highlightColor: Colors.transparent, onTap: () async { if (_selectedTangheemType < 0) { Utils.showToast("الرجائ تعبئة جميع القوائم"); return; } if (_selectedSurah < 0) { Utils.showToast("الرجائ تعبئة جميع القوائم"); return; } _searchFocusNode.unfocus(); _searchFocusNode.canRequestFocus = false; var data = {"tangheemTypeName": _tangheemListNotifier.value[_selectedTangheemType], "surahData": _surahModel.data[_selectedSurah]}; try { await widget.analytics.logEvent( name: 'tangheem_by_selection', parameters: { "tangheemTypeName": _tangheemListNotifier.value[_selectedTangheemType], "surahName": _surahModel.data[_selectedSurah].nameAR, }, ); } catch (ex) { print("tangheemTypeName:$ex"); } await Navigator.pushNamed(context, TangheemScreen.routeName, arguments: data); _searchFocusNode.canRequestFocus = true; getTangheemTypes(); }, child: Container( height: 36, decoration: BoxDecoration( color: ColorConsts.secondaryPink, borderRadius: BorderRadius.circular(6), ), padding: EdgeInsets.fromLTRB(8, 2, 8, 2), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.center, mainAxisSize: MainAxisSize.min, children: [ Text( "بحث", maxLines: 1, style: TextStyle(fontSize: 12, color: Colors.white), ), SizedBox(width: 12), SvgPicture.asset("assets/icons/go_forward.svg", width: 20, height: 20, color: Colors.white), ], ), ), ), SizedBox(height: 16), Container( height: 50, padding: EdgeInsets.only(top: 4, bottom: 6), child: TextField( controller: _searchController, focusNode: _searchFocusNode, style: TextStyle(color: ColorConsts.primaryBlack, fontSize: 14), decoration: InputDecoration( contentPadding: EdgeInsets.fromLTRB(4, 4, 4, 4), alignLabelWithHint: true, fillColor: Colors.white, filled: true, hintStyle: TextStyle(color: ColorConsts.textHintGrey, fontSize: 12), hintText: "البحث عن آية", prefixIconConstraints: BoxConstraints(maxHeight: 16), prefixIcon: Padding( padding: EdgeInsets.only(right: 6), child: SvgPicture.asset("assets/icons/search.svg"), ), suffixIcon: InkWell( onTap: () async { _searchFocusNode.unfocus(); if (_searchController.text.length < 1) { Utils.showToast("الرجاء ادخال محتوى للبحث"); return; } _searchFocusNode.canRequestFocus = false; var data = {"tangheemQuery": _searchController.text}; await widget.analytics.setAnalyticsCollectionEnabled(true); await widget.analytics.logEvent( name: 'tangheem_by_search', parameters: {"tangheemQuery": _searchController.text}, ); await Navigator.pushNamed(context, TangheemScreen.routeName, arguments: data); _searchFocusNode.canRequestFocus = true; getTangheemTypes(); }, splashColor: Colors.transparent, highlightColor: Colors.transparent, child: Container( alignment: Alignment.center, width: 80, child: Text( "بحث", style: TextStyle(fontSize: 14, color: Colors.white), ), decoration: BoxDecoration( color: ColorConsts.secondaryPink, borderRadius: BorderRadius.only( bottomLeft: Radius.circular(6), topLeft: Radius.circular(6), ), ), ), ), border: OutlineInputBorder(borderRadius: BorderRadius.circular(6), borderSide: BorderSide.none), ), ), ) ], ), ), ); } }