muh :(
This commit is contained in:
14
lib/api/fetchdata.dart
Normal file
14
lib/api/fetchdata.dart
Normal file
@ -0,0 +1,14 @@
|
||||
import 'package:f0ckapp/model/item.dart';
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
Future<List<Item>> fetchDataFromApi() async {
|
||||
final response = await http.get(Uri.parse('https://f0ck.dev/api/v2/items/get?mode=3'));
|
||||
|
||||
if(response.statusCode == 200) {
|
||||
List jsonresponse = json.decode(response.body)['items'];
|
||||
return jsonresponse.map((tmp) => Item.fromJson(tmp)).toList();
|
||||
} else {
|
||||
throw Exception('failed to load feed');
|
||||
}
|
||||
}
|
@ -1,29 +1,19 @@
|
||||
class Item {
|
||||
int id;
|
||||
String mime;
|
||||
int tagId;
|
||||
|
||||
Item({
|
||||
required this.id,
|
||||
required this.mime,
|
||||
required this.tagId
|
||||
});
|
||||
final int id;
|
||||
final String mime;
|
||||
final int tagId;
|
||||
|
||||
factory Item.fromJson(Map<String, dynamic> data) {
|
||||
final id = data['id'] as int;
|
||||
final mime = data['mime'] as String;
|
||||
final tagId = data['tag_id'] as int;
|
||||
return Item(
|
||||
id: id,
|
||||
mime: mime,
|
||||
tagId: tagId
|
||||
id: data['id'],
|
||||
mime: data['mime'],
|
||||
tagId: data['tag_id']
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
'mime': mime,
|
||||
'tagId': tagId
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:f0ckapp/api/fetchdata.dart';
|
||||
|
||||
import 'package:image_network/image_network.dart';
|
||||
|
||||
import 'package:f0ckapp/model/item.dart';
|
||||
|
||||
@ -14,22 +16,12 @@ class Home extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _HomeState extends State<Home> {
|
||||
List data = [];
|
||||
List<Item> f0cks = <Item>[];
|
||||
late Future<List<Item>> f0cks;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
fetchDataFromApi();
|
||||
}
|
||||
|
||||
Future<bool> fetchDataFromApi() async {
|
||||
final jsondata = await http.get(Uri.parse('https://f0ck.dev/api/v2/items/get?mode=3'));
|
||||
final list = json.decode(jsondata.body)['items'] as List<dynamic>;
|
||||
setState(() {
|
||||
f0cks = list.map((e) => Item.fromJson(e)).toList();
|
||||
});
|
||||
return true;
|
||||
f0cks = fetchDataFromApi();
|
||||
}
|
||||
|
||||
@override
|
||||
@ -41,41 +33,74 @@ class _HomeState extends State<Home> {
|
||||
title: const Text('f0cks'),
|
||||
centerTitle: true,
|
||||
),
|
||||
body: GridView.builder(
|
||||
gridDelegate:
|
||||
const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 3
|
||||
),
|
||||
itemCount: f0cks.length,
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
|
||||
Color mode;
|
||||
switch(f0cks[index].tagId) {
|
||||
case 1: mode = Colors.green; break;
|
||||
case 2: mode = Colors.red; break;
|
||||
default: mode = Colors.yellow; break;
|
||||
}
|
||||
|
||||
return Stack(
|
||||
children: <Widget>[
|
||||
Image.network("https://f0ck.dev/t/${f0cks[index].id}.webp"),
|
||||
Expanded(
|
||||
child: Align(
|
||||
alignment: FractionalOffset.bottomRight,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(bottom: 5.0, right: 5.0),
|
||||
child: Icon(
|
||||
Icons.square,
|
||||
color: mode,
|
||||
size: 20.0
|
||||
),
|
||||
body: FutureBuilder<List<Item>>(
|
||||
future: f0cks,
|
||||
builder: (context, snapshot) {
|
||||
if(snapshot.hasData) {
|
||||
return CustomScrollView(
|
||||
shrinkWrap: true,
|
||||
slivers: <Widget>[
|
||||
SliverGrid(
|
||||
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
|
||||
maxCrossAxisExtent: 130.0,
|
||||
mainAxisSpacing: 2,
|
||||
crossAxisSpacing: 2,
|
||||
),
|
||||
delegate: SliverChildBuilderDelegate(
|
||||
(context, index) {
|
||||
Color mode;
|
||||
switch(snapshot.data?[index].tagId) {
|
||||
case 1: mode = Colors.green; break;
|
||||
case 2: mode = Colors.red; break;
|
||||
default: mode = Colors.yellow; break;
|
||||
}
|
||||
return Stack(
|
||||
children: <Widget>[
|
||||
ImageNetwork(
|
||||
image: "https://f0ck.dev/t/${snapshot.data?[index].id}.webp",
|
||||
imageCache: CachedNetworkImageProvider("https://f0ck.dev/t/${snapshot.data?[index].id}.webp"),
|
||||
height: 128,
|
||||
width: 128,
|
||||
duration: 1500,
|
||||
curve: Curves.easeIn,
|
||||
onPointer: true,
|
||||
fitAndroidIos: BoxFit.cover,
|
||||
borderRadius: BorderRadius.circular(2),
|
||||
onError: const Icon(
|
||||
Icons.error,
|
||||
color: Colors.red,
|
||||
),
|
||||
onTap: () {
|
||||
debugPrint(snapshot.data?[index].mime);
|
||||
},
|
||||
),
|
||||
Expanded(
|
||||
child: Align(
|
||||
alignment: FractionalOffset.bottomRight,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(bottom: 0, right: 0),
|
||||
child: Icon(
|
||||
Icons.square,
|
||||
color: mode,
|
||||
size: 15.0
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
]
|
||||
);
|
||||
},
|
||||
childCount: snapshot.data?.length
|
||||
)
|
||||
),
|
||||
)
|
||||
]
|
||||
);
|
||||
},
|
||||
),
|
||||
]
|
||||
);
|
||||
} else if(snapshot.hasError) {
|
||||
return Text("${snapshot.error}");
|
||||
}
|
||||
return const CircularProgressIndicator();
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user