f0ckapp/lib/view/home.dart
2022-05-30 07:34:42 +02:00

106 lines
3.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:f0ckapp/api/fetchdata.dart';
import 'package:f0ckapp/model/item.dart';
import 'package:f0ckapp/view/item.dart';
class Home extends StatefulWidget {
const Home({Key? key}) : super(key: key);
@override
// ignore: library_private_types_in_public_api
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
late Future<List<Item>> f0cks;
@override
void initState() {
super.initState();
f0cks = fetchDataFromApi();
}
Future loadf0cks() {
setState(() {
f0cks = fetchDataFromApi();
});
return f0cks;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
backgroundColor: const Color.fromARGB(255, 43, 43, 43),
title: const Text('f0cks'),
centerTitle: true,
),
body: FutureBuilder<List<Item>>(
future: f0cks,
builder: (context, snapshot) {
if(!snapshot.hasData) {
return Text("${snapshot.error}");
}
return RefreshIndicator(
onRefresh: () {
return loadf0cks();
},
child: 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 GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ItemPage(id: snapshot.data![index].id),
),
);
},
child: Stack(
children: <Widget>[
Image.network("https://f0ck.dev/t/${snapshot.data?[index].id}.webp"),
SizedBox(
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
)
),
]
)
);
}
)
);
}
}