✨ Post a post
This commit is contained in:
@ -106,7 +106,7 @@ class AccountNameCard extends StatelessWidget {
|
||||
contentPadding: const EdgeInsets.only(left: 22, right: 34, top: 4, bottom: 4),
|
||||
leading: AccountAvatar(content: snapshot.data!.body?['avatar'], radius: 24),
|
||||
title: Text(snapshot.data!.body?['nick']),
|
||||
subtitle: Text(snapshot.data!.body?['name']),
|
||||
subtitle: Text(snapshot.data!.body?['email']),
|
||||
),
|
||||
);
|
||||
},
|
||||
|
@ -1,8 +1,11 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
|
||||
import 'package:solian/models/pagination.dart';
|
||||
import 'package:solian/models/post.dart';
|
||||
import 'package:solian/providers/auth.dart';
|
||||
import 'package:solian/providers/content/post_explore.dart';
|
||||
import 'package:solian/router.dart';
|
||||
import 'package:solian/widgets/posts/post_item.dart';
|
||||
|
||||
class HomeScreen extends StatefulWidget {
|
||||
@ -13,27 +16,23 @@ class HomeScreen extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _HomeScreenState extends State<HomeScreen> {
|
||||
int _pageKey = 0;
|
||||
int? _dataTotal;
|
||||
|
||||
bool _isFirstLoading = true;
|
||||
|
||||
final List<Post> _data = List.empty(growable: true);
|
||||
|
||||
getPosts() async {
|
||||
if (_dataTotal != null && _pageKey * 10 > _dataTotal!) return;
|
||||
final PagingController<int, Post> _pagingController = PagingController(firstPageKey: 0);
|
||||
|
||||
getPosts(int pageKey) async {
|
||||
final PostExploreProvider provider = Get.find();
|
||||
final resp = await provider.listPost(_pageKey);
|
||||
final PaginationResult result = PaginationResult.fromJson(resp.body);
|
||||
final resp = await provider.listPost(pageKey);
|
||||
if (resp.statusCode != 200) {
|
||||
_pagingController.error = resp.bodyString;
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
final parsed = result.data?.map((e) => Post.fromJson(e));
|
||||
if (parsed != null) _data.addAll(parsed);
|
||||
_isFirstLoading = false;
|
||||
_dataTotal = result.count;
|
||||
_pageKey++;
|
||||
});
|
||||
final PaginationResult result = PaginationResult.fromJson(resp.body);
|
||||
final parsed = result.data?.map((e) => Post.fromJson(e)).toList();
|
||||
if (parsed != null && parsed.length >= 10) {
|
||||
_pagingController.appendPage(parsed, pageKey + parsed.length);
|
||||
} else if (parsed != null) {
|
||||
_pagingController.appendLastPage(parsed);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@ -41,36 +40,46 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
Get.lazyPut(() => PostExploreProvider());
|
||||
super.initState();
|
||||
|
||||
Future.delayed(Duration.zero, () => getPosts());
|
||||
_pagingController.addPageRequestListener(getPosts);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (_isFirstLoading) {
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
}
|
||||
final AuthProvider auth = Get.find();
|
||||
|
||||
return Material(
|
||||
color: Theme.of(context).colorScheme.background,
|
||||
child: RefreshIndicator(
|
||||
onRefresh: () {
|
||||
_data.clear();
|
||||
_pageKey = 0;
|
||||
_dataTotal = null;
|
||||
return getPosts();
|
||||
},
|
||||
child: ListView.separated(
|
||||
itemCount: _data.length,
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
final item = _data[index];
|
||||
return GestureDetector(
|
||||
child: PostItem(key: Key('p${item.alias}'), item: item),
|
||||
onTap: () {},
|
||||
);
|
||||
},
|
||||
separatorBuilder: (_, __) => const Divider(thickness: 0.3, height: 0.3),
|
||||
return Scaffold(
|
||||
floatingActionButton: FutureBuilder(
|
||||
future: auth.isAuthorized,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData && snapshot.data == true) {
|
||||
return FloatingActionButton(
|
||||
child: const Icon(Icons.add),
|
||||
onPressed: () async {
|
||||
final value = await AppRouter.instance.pushNamed('postPublishing');
|
||||
if (value != null) {
|
||||
_pagingController.refresh();
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
return Container();
|
||||
}),
|
||||
body: Material(
|
||||
color: Theme.of(context).colorScheme.background,
|
||||
child: RefreshIndicator(
|
||||
onRefresh: () => Future.sync(() => _pagingController.refresh()),
|
||||
child: PagedListView<int, Post>.separated(
|
||||
pagingController: _pagingController,
|
||||
builderDelegate: PagedChildBuilderDelegate<Post>(
|
||||
itemBuilder: (context, item, index) {
|
||||
return GestureDetector(
|
||||
child: PostItem(key: Key('p${item.alias}'), item: item),
|
||||
onTap: () {},
|
||||
);
|
||||
},
|
||||
),
|
||||
separatorBuilder: (_, __) => const Divider(thickness: 0.3, height: 0.3),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
124
lib/screens/posts/publish.dart
Normal file
124
lib/screens/posts/publish.dart
Normal file
@ -0,0 +1,124 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_animate/flutter_animate.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:solian/providers/auth.dart';
|
||||
import 'package:solian/router.dart';
|
||||
import 'package:solian/services.dart';
|
||||
import 'package:solian/widgets/account/account_avatar.dart';
|
||||
import 'package:solian/shells/nav_shell.dart' as shell;
|
||||
|
||||
class PostPublishingScreen extends StatefulWidget {
|
||||
const PostPublishingScreen({super.key});
|
||||
|
||||
@override
|
||||
State<PostPublishingScreen> createState() => _PostPublishingScreenState();
|
||||
}
|
||||
|
||||
class _PostPublishingScreenState extends State<PostPublishingScreen> {
|
||||
final _contentController = TextEditingController();
|
||||
|
||||
bool _isSubmitting = false;
|
||||
|
||||
void applyPost() async {
|
||||
final AuthProvider auth = Get.find();
|
||||
if (!await auth.isAuthorized) return;
|
||||
if (_contentController.value.text.isEmpty) return;
|
||||
|
||||
setState(() => _isSubmitting = true);
|
||||
|
||||
final client = GetConnect();
|
||||
client.httpClient.baseUrl = ServiceFinder.services['interactive'];
|
||||
client.httpClient.addAuthenticator(auth.reqAuthenticator);
|
||||
|
||||
final resp = await client.post('/api/posts', {
|
||||
'content': _contentController.value.text,
|
||||
});
|
||||
if (resp.statusCode != 200) {
|
||||
Get.showSnackbar(GetSnackBar(
|
||||
title: 'errorHappened'.tr,
|
||||
message: resp.bodyString,
|
||||
));
|
||||
} else {
|
||||
AppRouter.instance.pop(resp.body);
|
||||
}
|
||||
|
||||
setState(() => _isSubmitting = false);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final AuthProvider auth = Get.find();
|
||||
|
||||
return Material(
|
||||
color: Theme.of(context).colorScheme.background,
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('postPublishing'.tr),
|
||||
leading: const shell.BackButton(),
|
||||
actions: [
|
||||
TextButton(
|
||||
child: Text('postAction'.tr.toUpperCase()),
|
||||
onPressed: () => applyPost(),
|
||||
)
|
||||
],
|
||||
),
|
||||
body: SafeArea(
|
||||
top: false,
|
||||
child: Column(
|
||||
children: [
|
||||
_isSubmitting ? const LinearProgressIndicator().animate().scaleX() : Container(),
|
||||
FutureBuilder(
|
||||
future: auth.getProfile(),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return ListTile(
|
||||
leading: AccountAvatar(content: snapshot.data?.body!['avatar'], radius: 22),
|
||||
title: Text(snapshot.data?.body!['nick']),
|
||||
subtitle: Text('postIdentityNotify'.tr),
|
||||
);
|
||||
} else {
|
||||
return Container();
|
||||
}
|
||||
},
|
||||
),
|
||||
const Divider(thickness: 0.3),
|
||||
Expanded(
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
child: TextField(
|
||||
maxLines: null,
|
||||
autofocus: true,
|
||||
autocorrect: true,
|
||||
keyboardType: TextInputType.multiline,
|
||||
controller: _contentController,
|
||||
decoration: InputDecoration.collapsed(
|
||||
hintText: 'postContentPlaceholder'.tr,
|
||||
),
|
||||
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
|
||||
),
|
||||
),
|
||||
),
|
||||
Container(
|
||||
constraints: const BoxConstraints(minHeight: 56),
|
||||
decoration: BoxDecoration(
|
||||
border: Border(
|
||||
top: BorderSide(width: 0.3, color: Theme.of(context).dividerColor),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
TextButton(
|
||||
style: TextButton.styleFrom(shape: const CircleBorder()),
|
||||
child: const Icon(Icons.camera_alt),
|
||||
onPressed: () {},
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user