import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

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

  @override
  State<News> createState() => _NewsState();
}

class _NewsState extends State<News> {
  void _gotohomePage() {
    Navigator.pushNamed(context, "/home");
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: rootBundle
          .loadString("assets/最新消息.json")
          .then((s) => jsonDecode(s)),
      builder: (context, snapshot) {
        final List<dynamic>? data = snapshot.data as List<dynamic>?;
        return Stack(
          children: [
            Positioned.fill(
              child: Image.asset("assets/background.png", fit: BoxFit.cover),
            ),
            Scaffold(
              backgroundColor: Colors.transparent,
              appBar: AppBar(
                backgroundColor: Colors.transparent,
                leading: IconButton(
                  onPressed: _gotohomePage,
                  icon: Image.asset("assets/f6.png"),
                ),
                title: Row(children: [SizedBox(width: 100), Text("最新消息")]),
              ),
              body: ListView.builder(
                itemCount: data?.length,
                itemBuilder: (context, index) {
                  return InkWell(
                    onTap: () {
                      showDialog(context: context, builder: (context){
                        return AlertDialog(
                          title: Text(data[index]["title"],style: TextStyle(fontSize: 20),),
                          content: SingleChildScrollView(
                            child: Column(
                              mainAxisSize: MainAxisSize.min,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(data[index]["release_date"]),
                                Text(data[index]["content"]),
                              ],
                            ),
                          )
                        );
                      });
                    },
                    child: Container(
                      margin: EdgeInsets.symmetric(
                        horizontal: 20,
                        vertical: 15,
                      ),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(10),
                      ),
                      child: Padding(
                        padding: EdgeInsets.all(10),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              data![index]["release_date"],
                              style: TextStyle(
                                color: Color(0xff6B6B6B),
                                fontSize: 15,
                              ),
                            ),
                            Text(
                              data![index]["title"],
                              style: TextStyle(
                                color: Color(0xff1D1AC7),
                                fontSize: 15,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  );
                },
              ),
            ),
          ],
        );
      },
    );
  }
}
