设为首页收藏本站

爱吱声

 找回密码
 注册
搜索
查看: 203|回复: 4
打印 上一主题 下一主题

[其他] ChatGPT写了个爱坛app

[复制链接]
  • TA的每日心情
    开心
    2020-1-2 23:51
  • 签到天数: 2 天

    [LV.1]炼气

    跳转到指定楼层
    楼主
     楼主| 发表于 3 天前 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

    要求它写个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!)
              ]),
            ),
          );
    }




    评分

    参与人数 1爱元 +2 收起 理由
    住在乡下 + 2 淡定

    查看全部评分

  • TA的每日心情
    开心
    2023-2-8 04:51
  • 签到天数: 1811 天

    [LV.Master]无

    沙发
    发表于 3 天前 | 只看该作者
    看不懂没事啊,试试不就得了?能不能达到你的要求啊?
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2020-1-2 23:51
  • 签到天数: 2 天

    [LV.1]炼气

    板凳
     楼主| 发表于 3 天前 | 只看该作者
    moletronic 发表于 2025-7-25 08:31
    看不懂没事啊,试试不就得了?能不能达到你的要求啊?


    不知道怎么试啊。。。
    回复 支持 反对

    使用道具 举报

  • TA的每日心情

    2022-1-1 00:00
  • 签到天数: 793 天

    [LV.10]大乘

    地板
    发表于 3 天前 | 只看该作者
    五月 发表于 2025-7-24 21:30
    不知道怎么试啊。。。

    把code喂给AI问怎么执行啦
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2020-1-2 23:51
  • 签到天数: 2 天

    [LV.1]炼气

    5#
     楼主| 发表于 3 天前 | 只看该作者
    indy 发表于 2025-7-25 11:21
    把code喂给AI问怎么执行啦

    算啦,不研究了。好奇心满足了
    回复 支持 反对

    使用道具 举报

    手机版|小黑屋|Archiver|网站错误报告|爱吱声   

    GMT+8, 2025-7-28 05:04 , Processed in 0.034374 second(s), 18 queries , Gzip On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表