2021-02-11 20:47:42 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
|
|
|
|
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
|
2021-02-22 03:36:25 +00:00
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
2021-02-11 20:47:42 +00:00
|
|
|
|
2021-02-21 17:31:03 +00:00
|
|
|
import 'package:imagini/blocs/home_bloc.dart';
|
|
|
|
import 'package:imagini/core/app_provider.dart';
|
|
|
|
import 'package:imagini/graphql/imagini_graphql.dart';
|
|
|
|
|
2021-02-11 20:47:42 +00:00
|
|
|
class HomeScreen extends StatefulWidget {
|
|
|
|
static const String PATH = '/Home';
|
|
|
|
|
|
|
|
HomeScreen({Key key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
_HomeScreenState createState() => _HomeScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _HomeScreenState extends State<HomeScreen> {
|
|
|
|
|
2021-02-21 17:31:03 +00:00
|
|
|
HomeBloc bloc;
|
2021-02-11 20:47:42 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
_init();
|
|
|
|
|
|
|
|
return PlatformScaffold(
|
|
|
|
body: _buildGridView(),
|
|
|
|
bottomNavBar: _buildNavBar()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-21 17:31:03 +00:00
|
|
|
void _init(){
|
|
|
|
if(bloc != null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bloc = HomeBloc(AppProvider.getApplication(context));
|
|
|
|
// bloc.mediaItemsResult.listen((bool status) {
|
|
|
|
// });
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
// bloc.dispose();
|
|
|
|
}
|
|
|
|
|
2021-02-11 20:47:42 +00:00
|
|
|
Widget _buildNavBar() {
|
|
|
|
return PlatformNavBar(
|
|
|
|
currentIndex: 0,
|
|
|
|
itemChanged: (index) => setState(
|
|
|
|
() {
|
|
|
|
// _selectedTabIndex = index;
|
|
|
|
print(index);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
items: [
|
|
|
|
BottomNavigationBarItem(
|
|
|
|
label: "Gallery",
|
|
|
|
icon: Icon(PlatformIcons(context).collections),
|
|
|
|
),
|
|
|
|
BottomNavigationBarItem(
|
|
|
|
label: "Me",
|
|
|
|
icon: Icon(PlatformIcons(context).person),
|
|
|
|
),
|
|
|
|
BottomNavigationBarItem(
|
|
|
|
label: "Settings",
|
|
|
|
icon: Icon(PlatformIcons(context).settings),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildGridView() {
|
2021-02-21 17:31:03 +00:00
|
|
|
return StreamBuilder<MediaItems$Query>(
|
|
|
|
stream: bloc.mediaItemsResult,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.data == null)
|
|
|
|
return _appLoading();
|
|
|
|
|
|
|
|
List<MediaItems$Query$MediaItemResponse$MediaItem> allItems = snapshot.data.mediaItems.data;
|
|
|
|
|
2021-02-22 03:36:25 +00:00
|
|
|
MediaQueryData queryData = MediaQuery.of(context);
|
|
|
|
|
|
|
|
final double screenWidthSize = queryData.size.width * queryData.devicePixelRatio;
|
|
|
|
final int crossAxisCount = (screenWidthSize / 500).ceil();
|
|
|
|
final int derivedContentWidth = (screenWidthSize / crossAxisCount).ceil();
|
|
|
|
|
2021-02-21 17:31:03 +00:00
|
|
|
return new StaggeredGridView.countBuilder(
|
2021-02-22 03:36:25 +00:00
|
|
|
crossAxisCount: crossAxisCount,
|
2021-02-21 17:31:03 +00:00
|
|
|
itemCount: allItems.length,
|
|
|
|
itemBuilder: (BuildContext context, int index) {
|
|
|
|
String fileName = allItems[index].fileName;
|
2021-02-22 03:36:25 +00:00
|
|
|
return _buildCard(fileName, derivedContentWidth);
|
2021-02-21 17:31:03 +00:00
|
|
|
},
|
2021-02-22 03:36:25 +00:00
|
|
|
staggeredTileBuilder: (int index) => new StaggeredTile.fit(1),
|
2021-02-21 17:31:03 +00:00
|
|
|
mainAxisSpacing: 4.0,
|
|
|
|
crossAxisSpacing: 4.0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _appLoading(){
|
|
|
|
return Center(
|
|
|
|
child: ConstrainedBox(
|
|
|
|
constraints: BoxConstraints(maxWidth: 500),
|
|
|
|
child: Container(
|
|
|
|
margin: EdgeInsets.fromLTRB(50, 0, 50, 0),
|
|
|
|
height: 270,
|
|
|
|
child: Column(
|
|
|
|
children: <Widget>[
|
|
|
|
Container(
|
|
|
|
child: FittedBox(
|
|
|
|
fit: BoxFit.contain,
|
|
|
|
child: const FlutterLogo(),
|
|
|
|
),
|
|
|
|
width: 175,
|
|
|
|
margin: EdgeInsets.fromLTRB(0, 0, 0, 50),
|
|
|
|
),
|
|
|
|
PlatformCircularProgressIndicator()
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2021-02-11 20:47:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-22 03:36:25 +00:00
|
|
|
Widget _buildCard(charImageUrl, derivedContentWidth) {
|
|
|
|
return FutureBuilder<CachedNetworkImage>(
|
|
|
|
future: bloc.getImage(charImageUrl, derivedContentWidth),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (!snapshot.hasData) {
|
|
|
|
return PlatformCircularProgressIndicator();
|
|
|
|
}
|
|
|
|
|
|
|
|
return snapshot.data;
|
|
|
|
// Image newImage = snapshot.data;
|
|
|
|
// return newImage;
|
|
|
|
}
|
2021-02-11 20:47:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|