Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
LiquidatorCoder committed Feb 2, 2022
1 parent 991de20 commit 7be662d
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 206 deletions.
282 changes: 76 additions & 206 deletions lib/pages/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:ip_notices/models/notice.dart';
import 'package:ip_notices/notifiers/algolia_notifier.dart';
Expand All @@ -9,7 +8,12 @@ import 'package:ip_notices/services/locator.dart';
import 'package:ip_notices/services/logger.dart';
import 'package:ip_notices/services/theme_service.dart';
import 'package:ip_notices/widgets/about_button.dart';
import 'package:ip_notices/widgets/error_sliver.dart';
import 'package:ip_notices/widgets/go_to_top_fab.dart';
import 'package:ip_notices/widgets/loading_sliver.dart';
import 'package:ip_notices/widgets/notice_tile.dart';
import 'package:ip_notices/widgets/search_bar.dart';
import 'package:ip_notices/widgets/search_button.dart';
import 'package:provider/provider.dart';

class HomePage extends StatefulWidget {
Expand All @@ -23,18 +27,11 @@ class _HomePageState extends State<HomePage> {
ScrollController controller = ScrollController();
TextEditingController searchController = TextEditingController();
late FocusNode searchFocusNode;

String? query;
bool showFab = false;
bool showSearch = false;
bool animating = false;
@override
void initState() {
super.initState();
searchFocusNode = FocusNode();
Future.delayed(const Duration())
.then((value) => context.read<FirestoreNotifier>().initNoticeStream());
controller.addListener(_scrollListener);
}

void _scrollListener() {
if (controller.position.pixels <= 10 && animating) {
Expand Down Expand Up @@ -80,6 +77,15 @@ class _HomePageState extends State<HomePage> {
}
}

@override
void initState() {
super.initState();
searchFocusNode = FocusNode();
Future.delayed(const Duration())
.then((value) => context.read<FirestoreNotifier>().initNoticeStream());
controller.addListener(_scrollListener);
}

@override
void dispose() {
searchFocusNode.dispose();
Expand All @@ -91,6 +97,7 @@ class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
final _themeService = locator<ThemeService>();

SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
Expand All @@ -104,6 +111,38 @@ class _HomePageState extends State<HomePage> {
: Brightness.dark,
),
);

onChanged(value) {
if (value.trim().isNotEmpty) {
setState(() {
query = value;
});
context.read<AlgoliaNotifier>().getNoticeSearch(value);
}
}

onPressed() {
setState(() {
animating = true;
});
controller.animateTo(
0,
duration: const Duration(milliseconds: 300),
curve: Curves.easeOutCubic,
);
searchFocusNode.requestFocus();
}

onFABPressed() {
setState(() {
showFab = false;
animating = true;
});
controller.animateTo(0,
duration: const Duration(milliseconds: 300),
curve: Curves.easeOutCubic);
}

return CupertinoPageScaffold(
backgroundColor: _themeService.background(context),
child: SafeArea(
Expand Down Expand Up @@ -132,34 +171,18 @@ class _HomePageState extends State<HomePage> {
.withOpacity(0.1),
width: 1)),
automaticallyImplyLeading: false,
trailing: showSearch
? Card(
elevation: 0,
color: Colors.transparent,
child: IconButton(
padding: const EdgeInsets.only(bottom: 2),
highlightColor: Colors.transparent,
hoverColor: Colors.transparent,
splashColor: Colors.transparent,
onPressed: () {
setState(() {
animating = true;
});
controller.animateTo(
0,
duration: const Duration(milliseconds: 300),
curve: Curves.easeOutCubic,
);
searchFocusNode.requestFocus();
},
icon: const Icon(
CupertinoIcons.search,
),
color: _themeService.onBackground(context),
iconSize: 20,
),
)
: null,
trailing: AnimatedSwitcher(
duration: const Duration(milliseconds: 200),
transitionBuilder: (child, animation) => FadeTransition(
opacity: animation,
child: child,
),
child: showSearch
? SearchButton(
onPressed: onPressed,
)
: null,
),
padding: EdgeInsetsDirectional.zero,
stretch: true,
largeTitle: Text(
Expand All @@ -170,99 +193,18 @@ class _HomePageState extends State<HomePage> {
),
backgroundColor: _themeService.background(context),
),
// SliverToBoxAdapter(
// child: Material(
// color: _themeService.background(context),
// child: InkWell(
// onTap: () {
// context.read<FirestoreNotifier>().togglePriorityCheck();
// },
// child: Padding(
// padding: const EdgeInsets.fromLTRB(16, 24, 16, 16),
// child: Row(
// children: [
// Text(
// context.watch<FirestoreNotifier>().priorityCheck
// ? "Priority"
// : "Latest",
// style: TextStyle(
// fontWeight: FontWeight.w600,
// fontSize: 14,
// color: _themeService.onBackground(context),
// ),
// ),
// const Spacer(),
// Icon(
// context.watch<FirestoreNotifier>().priorityCheck
// ? CupertinoIcons.star
// : CupertinoIcons.time,
// color: _themeService
// .onBackground(context)
// .withOpacity(0.8),
// size: 18,
// ),
// ],
// ),
// ),
// ),
// ),
// ),
SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20.0, vertical: 20.0),
child: CupertinoSearchTextField(
controller: searchController,
focusNode: searchFocusNode,
onChanged: (value) {
if (value.trim().isNotEmpty) {
setState(() {
query = value;
});
context
.read<AlgoliaNotifier>()
.getNoticeSearch(value);
}
},
onSubmitted: (value) {
if (value.trim().isNotEmpty) {
setState(() {
query = value;
});
context
.read<AlgoliaNotifier>()
.getNoticeSearch(value);
}
},
autocorrect: true,
style: TextStyle(
color: _themeService.onBackground(context),
),
),
),
SearchBar(
searchController: searchController,
searchFocusNode: searchFocusNode,
onChanged: onChanged,
),
(searchController.text.trim().isNotEmpty)
? () {
final snapshot =
context.watch<AlgoliaNotifier>().snapshot;
if (snapshot?.empty ?? false) {
// logger.e(snapshot.error);
return SliverToBoxAdapter(
child: Material(
color: Colors.transparent,
child: SizedBox(
height:
MediaQuery.of(context).size.height * 0.8,
width: MediaQuery.of(context).size.width,
child: const Center(
child: Icon(
CupertinoIcons.multiply_circle,
color: Colors.red,
),
),
),
),
);
return const ErrorSliver();
}
if (snapshot?.hits.isNotEmpty ?? false) {
return SliverList(
Expand Down Expand Up @@ -291,27 +233,7 @@ class _HomePageState extends State<HomePage> {
),
);
}
return SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return Material(
color: Colors.transparent,
child: SizedBox(
height:
MediaQuery.of(context).size.height * 0.8,
width: MediaQuery.of(context).size.width,
child: const Center(
child: CupertinoActivityIndicator(
animating: true,
radius: 20,
),
),
),
);
},
childCount: 1,
),
);
return const LoadingSliver();
}()
: StreamBuilder<List<Notice>>(
stream:
Expand All @@ -320,22 +242,7 @@ class _HomePageState extends State<HomePage> {
AsyncSnapshot<List<Notice>> snapshot) {
if (snapshot.hasError) {
logger.e(snapshot.error);
return SliverToBoxAdapter(
child: Material(
color: Colors.transparent,
child: SizedBox(
height:
MediaQuery.of(context).size.height * 0.8,
width: MediaQuery.of(context).size.width,
child: const Center(
child: Icon(
CupertinoIcons.multiply_circle,
color: Colors.red,
),
),
),
),
);
return const ErrorSliver();
}
if (snapshot.hasData) {
return SliverList(
Expand All @@ -347,10 +254,11 @@ class _HomePageState extends State<HomePage> {
height: 100,
width: 100,
child: Center(
child: CupertinoActivityIndicator(
animating: true,
radius: 14,
)),
child: CupertinoActivityIndicator(
animating: true,
radius: 14,
),
),
);
}
final bool download = snapshot
Expand All @@ -369,52 +277,14 @@ class _HomePageState extends State<HomePage> {
),
);
}
return SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return Material(
color: Colors.transparent,
child: SizedBox(
height: MediaQuery.of(context).size.height *
0.8,
width: MediaQuery.of(context).size.width,
child: const Center(
child: CupertinoActivityIndicator(
animating: true,
radius: 20,
),
),
),
);
},
childCount: 1,
),
);
return const LoadingSliver();
},
)
],
),
AnimatedPositioned(
curve: Curves.easeOutCubic,
duration: const Duration(milliseconds: 250),
bottom: showFab ? 20 : -50,
right: 20,
child: CupertinoButton.filled(
padding: EdgeInsets.zero,
borderRadius: BorderRadius.circular(500),
child: const Icon(
CupertinoIcons.up_arrow,
),
onPressed: () {
setState(() {
showFab = false;
animating = true;
});
controller.animateTo(0,
duration: const Duration(milliseconds: 300),
curve: Curves.easeOutCubic);
},
),
GoToTopFAB(
showFab: showFab,
onFABPressed: onFABPressed,
),
],
),
Expand Down
Loading

0 comments on commit 7be662d

Please sign in to comment.