This repository has been archived on 2023-11-13. You can view files and clone it, but cannot push or open issues or pull requests.
imagini/web_native/lib/screens/home_screen.dart

216 lines
5.8 KiB
Dart
Raw Normal View History

2021-02-11 20:47:42 +00:00
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
2021-02-11 20:47:42 +00:00
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
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';
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
ScrollController _gridViewController = new ScrollController();
ScrollController _parentController = new ScrollController();
int _currentIndex = 0;
int _totalLength = 1;
double _lastOffset = 0.0;
2021-02-11 20:47:42 +00:00
@override
Widget build(BuildContext context) {
_init();
return PlatformScaffold(
body: _buildBody(),
2021-02-11 20:47:42 +00:00
bottomNavBar: _buildNavBar()
);
}
2021-02-21 17:31:03 +00:00
void _init(){
_gridViewController.addListener(_scrollListener);
2021-02-21 17:31:03 +00:00
if(bloc != null)
return;
bloc = HomeBloc(AppProvider.getApplication(context));
}
// Needed Due to Nested Scroll Views
void _scrollListener() {
// Reflect Downwards & Upwards Scrolling (Respectively) on Parent
if (_gridViewController.offset > _lastOffset) {
if (_parentController.offset < _parentController.position.maxScrollExtent) {
_parentController.jumpTo(_parentController.offset + (_lastOffset - _gridViewController.offset).abs());
}
} else if (_gridViewController.offset < _lastOffset) {
if (_parentController.offset > 0) {
_parentController.jumpTo(_parentController.offset - (_lastOffset - _gridViewController.offset).abs());
}
}
// Don't update offset on top or bottom bounce
if (_gridViewController.offset < _gridViewController.position.maxScrollExtent && _gridViewController.offset > 0)
_lastOffset = _gridViewController.offset;
2021-02-21 17:31:03 +00:00
}
@override
void dispose() {
super.dispose();
// bloc.dispose();
}
Widget _buildBody() {
var widgetMap = [
<Widget>[
_buildAppBar("Gallery"),
SliverFillRemaining(
child: _buildGridView()
)
],
<Widget>[
_buildAppBar("Albums"),
SliverToBoxAdapter()
],
<Widget>[
_buildAppBar("Settings"),
SliverToBoxAdapter()
],
];
return CustomScrollView(
shrinkWrap: true,
controller: _parentController,
slivers: widgetMap[_currentIndex],
);
}
Widget _buildAppBar(String title) {
return SliverAppBar(
title: new Text(title),
pinned: false,
snap: false,
floating: true,
leading: PlatformIconButton(
icon: Icon(PlatformIcons(context).person),
),
actions: <Widget>[
PlatformIconButton(
icon: Icon(PlatformIcons(context).search),
2021-02-11 20:47:42 +00:00
),
PlatformIconButton(
icon: Icon(PlatformIcons(context).add),
),
],
);
2021-02-11 20:47:42 +00:00
}
Widget _buildNavBar() {
return PlatformNavBar(
currentIndex: _currentIndex,
itemChanged: (index) => setState(() {
_currentIndex = index;
}),
items: [
BottomNavigationBarItem(
label: "Photos",
icon: Icon(isMaterial(context) ? Icons.insert_photo : CupertinoIcons.photo),
),
BottomNavigationBarItem(
label: "Albums",
icon: Icon(isMaterial(context) ? Icons.collections : CupertinoIcons.photo_on_rectangle),
),
BottomNavigationBarItem(
label: "Settings",
icon: Icon(PlatformIcons(context).settings),
),
],
2021-02-21 17:31:03 +00:00
);
}
Widget _appLoading(){
return Center(
child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: 500),
child: Container(
margin: EdgeInsets.fromLTRB(50, 0, 50, 0),
2021-02-22 06:37:41 +00:00
height: 370,
2021-02-21 17:31:03 +00:00
child: Column(
children: <Widget>[
PlatformCircularProgressIndicator()
],
),
),
),
2021-02-11 20:47:42 +00:00
);
}
Widget _buildGridView() {
MediaQueryData queryData = MediaQuery.of(context);
final int itemMultiplier = 3;
final int crossAxisCount = (queryData.size.width * itemMultiplier / 500).ceil();
final int derivedContentWidth = 500;
return new StaggeredGridView.builder(
shrinkWrap: true,
itemCount: _totalLength,
controller: _gridViewController,
gridDelegate: SliverStaggeredGridDelegateWithFixedCrossAxisCount(
staggeredTileCount: _totalLength,
crossAxisCount: crossAxisCount,
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
staggeredTileBuilder: (int index) => new StaggeredTile.fit(1),
),
itemBuilder: (BuildContext context, int index) {
return _buildCard(index, derivedContentWidth);
},
);
}
Widget _buildCard(index, derivedContentWidth) {
return FutureBuilder<CachedNetworkImage>(
future: bloc.getMedia(index, derivedContentWidth),
builder: (context, snapshot) {
if (!snapshot.hasData)
return SizedBox(width: 500, height: 500);
WidgetsBinding.instance.addPostFrameCallback((_) {
if (_totalLength == bloc.totalMediaItems)
return;
setState(() {
_totalLength = bloc.totalMediaItems;
});
});
return snapshot.data;
// WidgetsBinding.instance.addPostFrameCallback((_) {
// if (_totalLength == bloc.cachedMediaItemList.length - 1)
// return;
// if (bloc.cachedMediaItemList.length == 0)
// return;
// setState(() {
// _totalLength = bloc.cachedMediaItemList.length - 1;
// });
// });
}
2021-02-11 20:47:42 +00:00
);
}
}