import 'package:flutter/material.dart';
import 'package:flutter_developer_test/model/language.dart';
import 'package:flutter_developer_test/pages/home/components/open_in_new.dart';
import 'package:flutter_developer_test/theme/app_theme.dart';
import 'package:flutter_developer_test/utils/fetch_data.dart';
import 'package:url_launcher/url_launcher.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage>
with SingleTickerProviderStateMixin {
TextEditingController _searchTextController = TextEditingController();
Color clickedTagBackgroundColor = Color(0xff5626df);
Color clickedTagTextColor = Color(0xffffffff);
List<LanguageData> resultdata = [];
bool showSearchError = true;
bool showSearchLoading = false;
double tagItemPadValue = 10;
late AnimationController _animationController;
@override
initState() {
_animationController =
AnimationController(vsync: this, duration: Duration(seconds: 4));
super.initState();
setState(() {
tagBackgroundColorList[0] = clickedTagBackgroundColor;
tagTextColorList[0] = clickedTagTextColor;
_searchTextController.text = tagList[0];
getData(_searchTextController.text);
showSearchLoading = true;
tagItemPadValue = 0;
});
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
getData(String languageName) async {
try {
Map<String, dynamic> result = await getLanguageData(languageName);
List<LanguageData> decodeResult = result['result']!;
String statuscode = result['statuscode']!;
String jsonstring = result['jsonstring']!;
if (decodeResult.isNotEmpty) {
setState(() {
showSearchError = false;
showSearchLoading = false;
});
} else if (statuscode != "200" || jsonstring == "[]") {
setState(() {
showSearchError = true;
showSearchLoading = false;
});
} else {
setState(() {
showSearchLoading = true;
});
}
setState(() {
resultdata = [];
});
resultdata.addAll(decodeResult);
} catch (e) {
// print(e);
setState(() {
showSearchError = true;
});
}
}
Widget _buildShowLoading(Function retry) {
retry();
return Image.asset("images/loading.gif");
}
Future<void> _launchUrl(String url) async {
final Uri _url = Uri.parse(url);
if (!await launchUrl(_url)) {
throw Exception('Could not launch $_url');
}
}
Widget _buildTagItem(
String _tagName, Color tagBackgroundColor, Color tagTextColor) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Container(
width: 15.0 * _tagName.length,
height: 40,
decoration: BoxDecoration(
color: tagBackgroundColor,
borderRadius: BorderRadius.all(Radius.circular(15)),
),
child: Center(
child: Text(
_tagName,
style: TextStyle(color: tagTextColor),
)),
),
);
}
Widget _buildSearchResult() {
return Expanded(
child: Container(
width: double.infinity,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20), color: Color(0xffffffff)),
child: showSearchLoading
? _buildShowLoading(() async {
await getData(_searchTextController.text);
})
: showSearchError
? _buildSearchingError()
: ListView.separated(
itemCount: resultdata.length,
separatorBuilder: (context, index) {
return Padding(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Divider());
},
itemBuilder: ((context, index) {
return _buildSearchResultItem(index);
}),
),
),
);
}
Widget _buildSearchResultItem(int index) {
return Container(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(
color: Color(0xFFCED2D2),
width: 1,
style: BorderStyle.solid,
),
),
child: Container(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Image.network(
resultdata[index].image,
width: 50,
height: 50,
),
),
),
),
SizedBox(
width: 15,
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
resultdata[index].title,
style: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 15,
),
),
Container(
width: 190,
height: 60,
child: Text(
resultdata[index].description,
softWrap: true,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: Color.fromARGB(255, 100, 93, 93),
fontSize: 11,
overflow: TextOverflow.ellipsis,
),
),
),
],
),
SizedBox(
width: 2,
),
GestureDetector(
onTap: () {
_launchUrl(resultdata[index].url);
print(resultdata[index].url);
},
child: OpenInNewIcon())
],
),
),
);
}
List<String> tagList = ["Language", "Build", "Design", "Cloud"];
List<Color> tagBackgroundColorList = [
Color(0xffffffff),
Color(0xffffffff),
Color(0xffffffff),
Color(0xffffffff)
];
List<Color> tagTextColorList = [
Color(0xFF4B4D4F),
Color(0xFF4B4D4F),
Color(0xFF4B4D4F),
Color(0xFF4B4D4F)
];
List<Widget> _buildTagList() {
List<Widget> list = [];
for (var i = 0; i < tagList.length; i++) {
list.add(GestureDetector(
onTap: () async {
setState(() {
showSearchLoading = true;
resultdata = [];
_searchTextController.text = tagList[i];
for (var j = 0; j < tagBackgroundColorList.length; j++) {
tagBackgroundColorList[j] = Color(0xffffffff);
tagTextColorList[j] = Color(0xFF4B4D4F);
}
tagBackgroundColorList[i] = clickedTagBackgroundColor;
tagTextColorList[i] = clickedTagTextColor;
});
var _ = await getData(tagList[i]);
},
child: _buildTagItem(
tagList[i], tagBackgroundColorList[i], tagTextColorList[i]),
));
}
return list;
}
Widget _buildSearchingError() {
return Column(children: [
SizedBox(
height: 20,
),
Image(image: AssetImage("images/error.jpeg")),
Text(
"Aaaaah!",
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w600),
),
Text(
"Something went wrong",
style: TextStyle(fontSize: 20, fontWeight: FontWeight.w600),
),
SizedBox(
height: 20,
),
Text(
"Brace yourself till we get the error fixed.",
style: TextStyle(fontWeight: FontWeight.w600),
),
SizedBox(
height: 10,
),
Text(
"You may also refresh the page",
style: TextStyle(fontWeight: FontWeight.w400),
),
SizedBox(
height: 10,
),
Text(
"or try again later",
style: TextStyle(fontWeight: FontWeight.w400),
)
]);
}
Widget _buildHomeContent() {
return Container(
padding: const EdgeInsets.only(top: 40),
child: Column(children: [
Padding(
padding: const EdgeInsets.only(top: 20, left: 20, right: 20),
child: Container(
height: 60,
width: double.infinity,
decoration: const BoxDecoration(
color: Color(0xffffffff),
borderRadius: BorderRadius.all(Radius.circular(13.0)),
),
child: Row(
children: [
const Padding(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Icon(
Icons.search,
color: Color(0xffa3a8b2),
size: 30,
),
),
Expanded(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: TextFormField(
showCursor: true,
controller: _searchTextController,
onChanged: (value) {
setState(() {
if (!tagList.contains(value)) {
for (var j = 0;
j < tagBackgroundColorList.length;
j++) {
setState(() {
tagBackgroundColorList[j] = Color(0xffffffff);
tagTextColorList[j] = Color(0xFF4B4D4F);
});
}
} else if (tagList.contains(value)) {
int index = tagList.indexOf(value);
tagBackgroundColorList[index] =
clickedTagBackgroundColor;
tagTextColorList[index] = clickedTagTextColor;
}
});
},
onFieldSubmitted: (value) {
setState(() {
resultdata = [];
showSearchLoading = true;
_searchTextController.text = value;
getData(_searchTextController.text);
});
},
style: const TextStyle(
fontFamily: AppTheme.fontName,
fontSize: 16,
color: Color.fromARGB(255, 0, 0, 0),
),
keyboardType: TextInputType.text,
decoration: InputDecoration(
hintText: 'Search..',
border: InputBorder.none,
hintStyle: TextStyle(
fontSize: 16,
letterSpacing: 0.2,
color: AppTheme.deactivatedText,
),
suffixIcon: GestureDetector(
onTap: () {
_searchTextController.clear();
_searchTextController.text = "";
},
child: _searchTextController.text != ""
? Icon(
Icons.clear,
size: 30,
color: Color(0xffa3a8b2),
)
: SizedBox()),
),
),
),
),
],
),
),
),
SizedBox(
height: 25,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: _buildTagList(),
),
),
),
SizedBox(
height: 25,
),
_buildSearchResult()
]),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: AppTheme.appBackgroundColor,
body: _buildHomeContent(),
);
}
}
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_developer_test/pages/home/home_page.dart';
import 'package:flutter_developer_test/routes/app_pages.dart';
import 'package:flutter_developer_test/routes/app_routes.dart';
import 'package:get/get.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Splash Screen',
theme: ThemeData(
primarySwatch: Colors.green,
),
home: MyHomePage(),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
Timer(
Duration(seconds: 2),
() => {
// Navigator.of(context).push(ScaleRoute(SearchLoading()));
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => HomePage()))
});
}
@override
Widget build(BuildContext context) {
return GetMaterialApp(
initialRoute: AppRoutes.SPLASH,
getPages: AppPages.list,
debugShowCheckedModeBanner: false,
);
}
}