import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:sport/bean/forum.dart';
import 'package:sport/bean/post.dart';
import 'package:sport/pages/social/post_page.dart';
import 'package:sport/router/navigator_util.dart';
import 'package:sport/services/api/inject_api.dart';
import 'package:sport/services/api/resp.dart';
import 'package:sport/widgets/appbar.dart';
import 'package:sport/widgets/image.dart';
import 'package:sport/widgets/loading.dart';

class PostSharePage extends StatefulWidget {
  final Post post;

  const PostSharePage(this.post);

  @override
  State<StatefulWidget> createState() => _PageState();
}

class _PageState extends State<PostSharePage> with InjectApi {
  @override
  void initState() {
    super.initState();
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        leading: buildBackButton(context),
        title: Text(
          "选择分享到的吧",
          style: titleStyle,
        ),
        centerTitle: false,
        titleSpacing: 0,
      ),
      body: FutureBuilder<RespList<Forum>>(
        future: api.getForumIndex(),
        builder: (_, snapshot) {
          if (snapshot.connectionState != ConnectionState.done) return RequestLoadingWidget();
          var list = snapshot.data?.results ?? [];
          return ListView.separated(
            itemBuilder: (BuildContext context, int index) => ListTile(
              leading: CircleAvatar(backgroundImage: CachedNetworkImageProvider(list[index].cover), radius: 22.0),
              title: Text(
                "${list[index].name}",
                style: Theme.of(context).textTheme.subtitle1.copyWith(fontSize: 16),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
              onTap: () async {
                bool result = await NavigatorUtil.goPage(
                    context,
                    (context) => PostPage(
                          list[index].forumId,
                          post: widget.post,
                      forum: list[index],
                        ));
                if (result == true) {
                  Navigator.of(context).pop(true);
                }
              },
              trailing: arrowRight(),
            ),
            itemCount: list.length,
            separatorBuilder: (BuildContext context, int index) => Divider(),
          );
        },
      ),
    );
  }
}
