TA的每日心情 | 开心 2020-1-2 23:51 |
---|
签到天数: 2 天 [LV.1]炼气
|
要求它写个app,能和爱坛同步,有浏览、登录、发贴功能。它很乖,几秒钟就写好了。
俺看不懂。。。
// 简洁精悍的 Flutter APP,支持连接 Discuz,查看帖子和发帖、登录功能
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() => runApp(DiscuzApp());
class DiscuzApp extends StatelessWidget {
@override
Widget build(BuildContext context) => MaterialApp(
title: 'Discuz Forum',
theme: ThemeData(primarySwatch: Colors.blue),
home: LoginPage(),
);
}
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final userCtrl = TextEditingController();
final passCtrl = TextEditingController();
bool loading = false;
String? error;
void login() async {
setState(() => loading = true);
final res = await http.post(
Uri.parse('https://your-discuz-site.com/api/login'),
body: {'username': userCtrl.text, 'password': passCtrl.text},
);
setState(() => loading = false);
if (res.statusCode == 200) {
final token = json.decode(res.body)['token'];
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (_) => HomePage(token: token)),
);
} else {
setState(() => error = '登录失败');
}
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: Text('Discuz 登录')),
body: Padding(
padding: EdgeInsets.all(16),
child: Column(children: [
TextField(controller: userCtrl, decoration: InputDecoration(labelText: '用户名')),
TextField(controller: passCtrl, decoration: InputDecoration(labelText: '密码'), obscureText: true),
SizedBox(height: 20),
ElevatedButton(
onPressed: loading ? null : login,
child: loading ? CircularProgressIndicator() : Text('登录'),
),
if (error != null) Text(error!, style: TextStyle(color: Colors.red))
]),
),
);
}
class HomePage extends StatefulWidget {
final String token;
HomePage({required this.token});
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
List posts = [];
bool loading = true;
@override
void initState() {
super.initState();
loadPosts();
}
void loadPosts() async {
final res = await http.get(
Uri.parse('https://your-discuz-site.com/api/posts'),
headers: {'Authorization': 'Bearer ${widget.token}'},
);
if (res.statusCode == 200) {
setState(() {
posts = json.decode(res.body);
loading = false;
});
}
}
void goToNewPost() => Navigator.push(
context,
MaterialPageRoute(builder: (_) => NewPostPage(token: widget.token)),
);
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: Text('Discuz 帖子'), actions: [
IconButton(onPressed: goToNewPost, icon: Icon(Icons.add))
]),
body: loading
? Center(child: CircularProgressIndicator())
: ListView(
children: posts
.map((post) => ListTile(
title: Text(post['title']),
subtitle: Text(post['author']),
))
.toList(),
),
);
}
class NewPostPage extends StatefulWidget {
final String token;
NewPostPage({required this.token});
@override
_NewPostPageState createState() => _NewPostPageState();
}
class _NewPostPageState extends State<NewPostPage> {
final titleCtrl = TextEditingController();
final contentCtrl = TextEditingController();
bool posting = false;
String? msg;
void submit() async {
setState(() => posting = true);
final res = await http.post(
Uri.parse('https://your-discuz-site.com/api/post'),
headers: {'Authorization': 'Bearer ${widget.token}'},
body: {'title': titleCtrl.text, 'content': contentCtrl.text},
);
setState(() => posting = false);
if (res.statusCode == 200) {
setState(() => msg = '发帖成功');
} else {
setState(() => msg = '发帖失败');
}
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: Text('新帖')),
body: Padding(
padding: EdgeInsets.all(16),
child: Column(children: [
TextField(controller: titleCtrl, decoration: InputDecoration(labelText: '标题')),
TextField(controller: contentCtrl, decoration: InputDecoration(labelText: '内容'), maxLines: 5),
SizedBox(height: 20),
ElevatedButton(
onPressed: posting ? null : submit,
child: posting ? CircularProgressIndicator() : Text('发帖'),
),
if (msg != null) Text(msg!)
]),
),
);
}
|
评分
-
查看全部评分
|