Skip to content

Commit

Permalink
fix: chirp tab pages rebuilding on tab page change
Browse files Browse the repository at this point in the history
  • Loading branch information
IamMuuo committed Sep 9, 2024
1 parent b8ee359 commit 053424b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 59 deletions.
2 changes: 0 additions & 2 deletions lib/controllers/reward_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import 'package:academia/controllers/user_controller.dart';
import 'package:academia/models/models.dart';
import 'package:academia/models/services/services.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:ionicons/ionicons.dart';
import 'package:dartz/dartz.dart';

class RewardController extends GetxController {
Expand Down
72 changes: 16 additions & 56 deletions lib/tools/chirp/pages/chirp_home_page.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
import 'package:academia/exports/barrel.dart';
import '../pages/post_create_page.dart';
import 'package:get/get.dart';
import '../widgets/widgets.dart';
import './feed_page.dart';
import '../controllers/chirp_controller.dart';
import './personal_posts.dart';

class ChirpHomePage extends StatefulWidget {
class ChirpHomePage extends StatelessWidget {
const ChirpHomePage({super.key});

@override
State<ChirpHomePage> createState() => _ChirpHomePageState();
}

class _ChirpHomePageState extends State<ChirpHomePage> {
final NotificationsController notificationsController =
Get.find<NotificationsController>();
final controller = Get.put(ChirpController());

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -64,7 +48,8 @@ class _ChirpHomePageState extends State<ChirpHomePage> {
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage(
"assets/images/sketchbook-passersby-people-working-around-1.png"),
"assets/images/sketchbook-passersby-people-working-around-1.png",
),
),
),
),
Expand All @@ -74,8 +59,8 @@ class _ChirpHomePageState extends State<ChirpHomePage> {
snap: true,
bottom: const TabBar(
tabs: [
Tab(text: 'My Feed'),
Tab(text: "My Posts"),
Tab(text: 'Trending'),
Tab(text: "Your Posts"),
],
),
),
Expand All @@ -87,41 +72,16 @@ class _ChirpHomePageState extends State<ChirpHomePage> {
PersistentStorySliverDelegate(child: const SizedBox()),
),
),
SliverFillRemaining(
hasScrollBody: true,
fillOverscroll: true,
child: TabBarView(children: [
const FeedPage(),
FutureBuilder(
future: controller.fetchUserPosts(),
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return ListView.separated(
itemBuilder: (context, index) {
return const EmptyPostCard();
},
separatorBuilder: (context, index) =>
const SizedBox(
height: 4,
),
itemCount: 12);
}
return snapshot.data!.fold((l) {
return Center(
child: Text("Snap! $l"),
);
}, (r) {
return ListView.separated(
itemBuilder: (context, index) {
final post = r[index];
return PostCard(post: post);
},
separatorBuilder: (context, index) =>
const SizedBox(height: 2),
itemCount: r.length);
});
}),
])),
const SliverFillRemaining(
hasScrollBody: true,
fillOverscroll: true,
child: TabBarView(
children: [
FeedPage(),
PersonalPostsPage(),
],
),
),
],
),
),
Expand Down
8 changes: 7 additions & 1 deletion lib/tools/chirp/pages/feed_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class FeedPage extends StatefulWidget {
State<FeedPage> createState() => _FeedPageState();
}

class _FeedPageState extends State<FeedPage> {
class _FeedPageState extends State<FeedPage>
with AutomaticKeepAliveClientMixin {
bool pageLoading = true;
bool morePostsLoading = false;
List<Post> feedPosts = [];
Expand All @@ -18,6 +19,10 @@ class _FeedPageState extends State<FeedPage> {

final UserController userController = Get.find<UserController>();
final PostService _service = PostService();

@override
bool get wantKeepAlive => true;

@override
void initState() {
super.initState();
Expand All @@ -44,6 +49,7 @@ class _FeedPageState extends State<FeedPage> {

@override
Widget build(BuildContext context) {
super.build(context);
return pageLoading
? ListView.separated(
itemBuilder: (context, index) {
Expand Down
65 changes: 65 additions & 0 deletions lib/tools/chirp/pages/personal_posts.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import 'package:academia/exports/barrel.dart';
import 'package:academia/tools/chirp/widgets/widgets.dart';
import 'package:dartz/dartz.dart' as dartz;
import '../controllers/chirp_controller.dart';
import 'package:get/get.dart';

class PersonalPostsPage extends StatefulWidget {
const PersonalPostsPage({super.key});

@override
State<PersonalPostsPage> createState() => _PersonalPostsPageState();
}

class _PersonalPostsPageState extends State<PersonalPostsPage>
with AutomaticKeepAliveClientMixin {
late Future<dartz.Either<String, List<Post>>> posts;
@override
bool get wantKeepAlive => true;

final controller = Get.find<ChirpController>();
@override
void initState() {
super.initState();
posts = controller.fetchUserPosts();
}

@override
Widget build(BuildContext context) {
super.build(context);
return RefreshIndicator(
onRefresh: () async {
posts = controller.fetchUserPosts();
setState(() {});
},
child: FutureBuilder(
future: posts,
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return ListView.separated(
itemBuilder: (context, index) {
return const EmptyPostCard();
},
separatorBuilder: (context, index) => const SizedBox(
height: 4,
),
itemCount: 12);
}
return snapshot.data!.fold((l) {
return Center(
child: Text("Snap! $l"),
);
}, (r) {
return ListView.separated(
itemBuilder: (context, index) {
final post = r[index];
return PostCard(post: post);
},
separatorBuilder: (context, index) => const SizedBox(height: 2),
itemCount: r.length);
});
},
),
);
}
}

0 comments on commit 053424b

Please sign in to comment.