name27 / flutter

0 stars 0 forks source link

월말평가: 뉴스 앱 #84

Open name27 opened 1 year ago

name27 commented 1 year ago

image image image image

main.dart

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:one_month_test_app/main_page.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        theme: ThemeData.dark().copyWith(
          textTheme: GoogleFonts.nunitoSansTextTheme(
            const TextTheme(),
          ),
        ),
        home: MainPage());
  }
}

main_page.dart

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/container.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:one_month_test_app/NewsCard.dart';

class MainPage extends StatelessWidget {
  const MainPage({super.key});

  @override
  Widget build(BuildContext context) {
    var dio = Dio();

    var url = "http://52.79.115.43:8090/api/collections/news/records";

    Future<List<dynamic>> getData() async {
      var res = await dio.get(url);
      if (res.statusCode == 200) {
        return res.data["items"];
      }
      return [];
    }

    return Scaffold(
      appBar: AppBar(
        title: Text('News'),
        backgroundColor: Colors.transparent,
        elevation: 0,
      ),
      body: FutureBuilder(
        future: getData(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return Padding(
              padding: const EdgeInsets.all(8.0),
              child: ListView.separated(
                physics: BouncingScrollPhysics(),
                itemCount: snapshot.data?.length ?? 0,
                itemBuilder: (context, index) => NewsCard(
                  imgUrl: snapshot.data![index]["thumbnail"],
                  title: snapshot.data![index]["title"],
                  content: snapshot.data![index]["content"],
                  author: snapshot.data![index]["author"],
                  newsUrl: snapshot.data![index]["news_url"],
                ),
                separatorBuilder: (context, index) => SizedBox(
                  height: 12,
                ),
              ),
            );
          }
          return LinearProgressIndicator();
        },
      ),
    );
  }
}

NewsCard.dart

import 'package:animate_do/animate_do.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/container.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/widgets.dart';
import 'package:one_month_test_app/new_page.dart';

class NewsCard extends StatelessWidget {
  const NewsCard({
    super.key,
    required this.imgUrl,
    required this.title,
    required this.content,
    required this.author,
    required this.newsUrl,
  });
  final String imgUrl;
  final String title;
  final String content;
  final String author;
  final String newsUrl;

  @override
  Widget build(BuildContext context) {
    return Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
      ZoomIn(
        delay: Duration(milliseconds: 300),
        duration: Duration(milliseconds: 800),
        child: InkWell(
          onTap: () {
            Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => NewsPage(
                    imgUrl: imgUrl,
                    title: title,
                    content: content,
                    author: author,
                    newsUrl: newsUrl,
                  ),
                ));
          },
          child: Stack(children: [
            Hero(
              tag: imgUrl,
              child: Container(
                height: 250,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(8),
                    image: DecorationImage(
                        image: NetworkImage(imgUrl), fit: BoxFit.cover)),
              ),
            ),
            Positioned(
                bottom: 0,
                child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          title,
                          style: TextStyle(
                              fontSize: 18, fontWeight: FontWeight.w600),
                        ),
                        Container(
                          width: 350,
                          child: RichText(
                            overflow: TextOverflow.clip,
                            maxLines: 2,
                            strutStyle: StrutStyle(fontSize: 16.0),
                            text: TextSpan(
                                text: content,
                                style: TextStyle(
                                  fontSize: 15,
                                )),
                          ),
                        ),
                      ],
                    )))
          ]),
        ),
      )
    ]);
  }
}

news_page.dart

import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/container.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:url_launcher/url_launcher_string.dart';

class NewsPage extends StatelessWidget {
  const NewsPage(
      {super.key,
      required this.imgUrl,
      required this.title,
      required this.content,
      required this.author,
      required this.newsUrl});
  final String imgUrl;
  final String title;
  final String content;
  final String author;
  final String newsUrl;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      body: ListView(
        physics: BouncingScrollPhysics(),
        children: [
          Hero(
            tag: imgUrl,
            child: Container(
              height: 250,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(8),
                  image: DecorationImage(
                      image: NetworkImage(imgUrl), fit: BoxFit.cover)),
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(12.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: TextStyle(fontSize: 22, fontWeight: FontWeight.w600),
                ),
                Divider(),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(author,
                        style: TextStyle(
                            fontSize: 18, fontWeight: FontWeight.w600)),
                    TextButton(
                        onPressed: () {
                          launchUrlString(newsUrl);
                        },
                        child: Text('Full Article'))
                  ],
                ),
                Divider(),
                Text(content),
              ],
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.pop(context);
        },
        child: Icon(Icons.arrow_back_outlined),
      ),
    );
  }
}