Skip to content

Instantly share code, notes, and snippets.

@androidfanatic
Last active March 4, 2020 07:16
Show Gist options
  • Save androidfanatic/c7a952421ea84c79cf4c19b031f8232f to your computer and use it in GitHub Desktop.
Save androidfanatic/c7a952421ea84c79cf4c19b031f8232f to your computer and use it in GitHub Desktop.
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:splashscreen/splashscreen.dart';
import 'package:http/http.dart' as http;
void main() => runApp(MyApp());
Color getColorFromHex(String hexColor) {
hexColor = hexColor.toUpperCase().replaceAll('#', '');
if (hexColor.length == 6) {
hexColor = 'FF' + hexColor;
}
return Color(int.parse(hexColor, radix: 16));
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Places',
theme: ThemeData(
primarySwatch: primarySwatch,
),
home: FESplash(),
);
}
}
const COLORS = {
"main": "#008848"
};
class FESplash extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SplashScreen(
backgroundColor: getColorFromHex(COLORS["main"]),
seconds: 2,
navigateAfterSeconds: FEScreen(),
title: Text(
"FE Credit",
style: TextStyle(fontSize: 84, color: Colors.white),
),
image: null,
styleTextUnderTheLoader: TextStyle(),
photoSize: 100.0,
loaderColor: Colors.white);
}
}
class FEScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() => FEState();
}
class Todo {
int userId;
int id;
String title;
bool completed;
Todo(this.userId, this.id, this.title, this.completed);
}
class FEState extends State<FEScreen> {
List<Todo> todos = [];
bool notificationsLoaded = false;
@override
void initState() {
super.initState();
}
void _getTodos() async {
setState(() {
notificationsLoaded = false;
});
var res = await http.get("https://jsonplaceholder.typicode.com/todos");
if (res.statusCode == 200) {
List apiTodos = json.decode(res.body);
setState(() {
notificationsLoaded = true;
});
setState(() {
todos = apiTodos
.map((todo) => Todo(
todo["userId"], todo["id"], todo["title"], todo["completed"]))
.toList();
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: Drawer(
// Add a ListView to the drawer. This ensures the user can scroll
// through the options in the drawer if there isn't enough vertical
// space to fit everything.
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
child: Text('Drawer Header'),
decoration: BoxDecoration(
color: getColorFromHex(COLORS["main"]),
),
),
ListTile(
title: Text('Item 1'),
onTap: () {
// Update the state of the app.
// ...
},
),
ListTile(
title: Text('Item 2'),
onTap: () {
// Update the state of the app.
// ...
},
),
],
),
),
appBar: AppBar(
iconTheme: IconThemeData(color: Colors.black),
backgroundColor: Colors.white,
centerTitle: true,
title: Row(
children: <Widget>[
Expanded(
child: Center(
child: Image.asset("lib/assets/logo-vn.png", scale: 1.25),
),
)
],
),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.notifications,
color: Colors.black,
),
onPressed: () {
_getTodos();
},
)
],
),
body: Container(
color: getColorFromHex(COLORS["main"]),
child: !notificationsLoaded
? Center(
child: Text(
"Home Screen",
style: TextStyle(color: Colors.white, fontSize: 24),
))
: ListView.builder(
itemCount: todos.length,
itemBuilder: (context, i) => Padding(
padding: EdgeInsets.fromLTRB(12.0, 12.0, 12.0, 0),
child: Card(
color: getColorFromHex("#dddddd"),
child: Container(
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
todos[i].title +
". Planning to buy a car? Now may be the best time as FE Credit auto cos offer big deals ",
),
InkWell(
child: Text(
"https://economictimes.indiatimes.com",
style: TextStyle(
decoration:
TextDecoration.underline,
)),
onTap: () async {
if (await canLaunch(
"https://economictimes.indiatimes.com")) {
await launch(
"https://economictimes.indiatimes.com");
} else {
throw 'Could not launch https://economictimes.indiatimes.com';
}
},
)
],
),
)
],
),
))),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment