name27 / flutter

0 stars 0 forks source link

평가과제: 비밀 듣는 고양이 #79

Open name27 opened 1 year ago

name27 commented 1 year ago

image

image

image image image image

mian.dart

import 'package:flutter/material.dart';
import 'package:secret_app/Pages/main_page.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(fontFamily: 'neo'),
      home: MainPage(),
    );
  }
}

SecretCard.dart

import 'package:flutter/material.dart';
import 'package:secret_cat_sdk/secret_cat_sdk.dart';
import 'package:intl/intl.dart';

class SecretCard extends StatelessWidget {
  const SecretCard({
    super.key,
    required this.secret,
  });
  final Secret secret;

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          margin: EdgeInsets.all(8),
          decoration: BoxDecoration(color: Colors.white),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              ListTile(
                leading: secret.author == null
                    ? CircleAvatar(
                        backgroundColor: Colors.grey[50],
                        child: Text(
                          secret.author?.username[0] ?? '익',
                          style: TextStyle(color: Colors.black),
                        ),
                        radius: 25,
                      )
                    : CircleAvatar(
                        backgroundColor: Colors.transparent,
                        backgroundImage:
                            NetworkImage(secret.author!.avatar.toString()),
                        radius: 35,
                      ),
                title: Text(secret.author?.username ?? '익명의 누군가'),
                subtitle: Text(DateFormat('EE, MM / dd').format(secret.createdAt).toString()),
              ),
              Padding(
                padding: const EdgeInsets.all(30.0),
                child: Text(
                  secret.secret.toString(),
                  style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
                  textAlign: TextAlign.center,
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
}

main_page.dart

import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:secret_app/Pages/home_page.dart';

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

  @override
  Widget build(BuildContext context) {
    var _titleName = '내 비밀 네 비밀';
    var isLogin = false;
    return Scaffold(
      backgroundColor: Colors.indigo[900],
      extendBodyBehindAppBar: true,
      appBar: AppBar(
        backgroundColor: Colors.transparent,
        elevation: 0,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            CircleAvatar(
              backgroundImage: AssetImage('assets/marketing.png'),
              radius: 40,
            ),
            SizedBox(
              height: 20,
            ),
            FutureBuilder(
              future: Future.delayed(Duration(milliseconds: 2000)),
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.done) {
                  _titleName = '내 비 네 비';
                  return Column(
                    children: [
                      Animate(
                        effects: [
                          FadeEffect(duration: Duration(milliseconds: 800)),
                          ScaleEffect(
                              begin: Offset(0.8, 0.8),
                              duration: Duration(milliseconds: 800))
                        ],
                        child: Text(
                          _titleName,
                          style: TextStyle(color: Colors.white, fontSize: 22),
                        ),
                      ),
                      SizedBox(
                        height: 60,
                      ),
                      CircleAvatar(
                        child: IconButton(
                          onPressed: (() {
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => HomePage()));
                          }),
                          icon: Icon(Icons.navigate_next),
                          color: Colors.white,
                        ),
                      ),
                      SizedBox(
                        height: 8,
                      ),
                      Text(
                        '시작하기',
                        style: TextStyle(color: Colors.white),
                      )
                    ],
                  );
                } else {
                  _titleName = '내 비밀 네 비밀';
                  return Column(
                    children: [
                      Animate(
                        effects: [
                          TintEffect(
                              delay: Duration(milliseconds: 500),
                              duration: Duration(milliseconds: 1500),
                              color: Colors.indigo[900])
                        ],
                        child: Text(
                          _titleName,
                          style: TextStyle(color: Colors.white, fontSize: 22),
                        ),
                      ),
                      SizedBox(
                        height: 123,
                      ),
                    ],
                  );
                }
              },
            ),
          ],
        ),
      ),
    );
  }
}

home_page.dart

import 'package:animated_bottom_navigation_bar/animated_bottom_navigation_bar.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:secret_app/Pages/author_page.dart';
import 'package:secret_app/Pages/secret_page.dart';
import 'package:secret_cat_sdk/api/api.dart';

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

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final ValueNotifier<bool> _isaddSecret = ValueNotifier<bool>(false);
  var _bottomNavIndex = 0;
  var _inputController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('내 비 네 비'),
        foregroundColor: Colors.black,
        backgroundColor: Colors.transparent,
        elevation: 0,
      ),
      drawer: Drawer(
        child: ListView(
          padding: EdgeInsets.zero,
          children: [
            UserAccountsDrawerHeader(
              currentAccountPicture: CircleAvatar(
                backgroundColor: Colors.white,
                child: Text(''),
              ),
              accountName: Text('비밀 듣는 고양이(SNS 형)'),
              accountEmail: Text('스나이퍼팩토리 교육용앱'),
              onDetailsPressed: () {
                print('clicked');
              },
              decoration: BoxDecoration(
                color: Colors.grey,
              ),
            ),
            ListTile(
              leading: Icon(Icons.home),
              title: Text('홈'),
              subtitle: Text('홈으로 이동하기'),
            ),
            ListTile(
              leading: Icon(Icons.people),
              title: Text('Author'),
              subtitle: Text('Author로 이동하기'),
            ),
            SizedBox(
              height: 270,
            ),
            ListTile(
              title: Text('로그아웃'),
              subtitle: Text('로그아웃입니다'),
              trailing: Icon(Icons.logout),
            ),
          ],
        ),
      ),
      body: [SecretPage(), AuthorPage()][_bottomNavIndex],
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showModalBottomSheet( 
              context: context,
              builder: (BuildContext context) {
                return Padding(
                  padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
                  child: SingleChildScrollView(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        SizedBox(
                          height: 15,
                        ),
                        Text(
                          '비밀을 공유해볼까요?',
                          textAlign: TextAlign.center,
                        ),
                        Container(
                          margin: EdgeInsets.all(12),
                          child: TextField(
                            controller: _inputController,
                            decoration: InputDecoration(
                                filled: true,
                                fillColor: Colors.white,
                                hintText: '비밀을 입력하세요',
                                enabledBorder: OutlineInputBorder(
                                    borderSide: BorderSide(
                                        color: Colors.grey, width: 0.5))),
                          ),
                        ),
                        ElevatedButton(
                            style: ElevatedButton.styleFrom(
                                backgroundColor: Colors.red),
                            onPressed: () async {
                              if (_inputController.text != '') {
                                await SecretCatApi.addSecret(
                                    _inputController.text);
                                    _inputController.text='';
                                    Navigator.pop(context);
                              }
                            },
                            child: Text('비밀 공유')),
                      ],
                    ),
                  ),
                );
              });
        },
        backgroundColor: Colors.red,
        child: Icon(Icons.add),
      ),
      bottomNavigationBar: AnimatedBottomNavigationBar(
        icons: [FontAwesomeIcons.cat, FontAwesomeIcons.peopleGroup],
        activeIndex: _bottomNavIndex,
        gapLocation: GapLocation.center,
        notchSmoothness: NotchSmoothness.softEdge,
        leftCornerRadius: 32,
        rightCornerRadius: 32,
        onTap: (index) => setState(() => _bottomNavIndex = index),
      ),
    );
  }
}

secret_page.dart

import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:secret_app/SecretCard.dart';
import 'package:secret_cat_sdk/api/api.dart';

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

  @override
  State<SecretPage> createState() => _SecretPageState();
}

class _SecretPageState extends State<SecretPage> {
  bool addSecret = false;
  RefreshController _refreshController = RefreshController();
  void _onRefresh() async {
    await Future.delayed(Duration(milliseconds: 1200));
    _refreshController.refreshCompleted();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Container(
            decoration: BoxDecoration(color: Colors.grey[200]),
            child: FutureBuilder(
              future: SecretCatApi.fetchSecrets(),
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.done) {
                  var reversedList = snapshot.data!.toList();
                  return SmartRefresher(
                    header: WaterDropHeader(),
                    enablePullUp: false,
                    controller: _refreshController,
                    onRefresh: _onRefresh,
                    child: ListView.builder(
                        itemCount: snapshot.data?.length,
                        itemBuilder: (context, index) =>
                            SecretCard(secret: reversedList[index])),
                  );
                }
                return SizedBox();
              },
            )
          );
  }
}

author_page.dart

import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:secret_cat_sdk/secret_cat_sdk.dart';

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

  @override
  State<AuthorPage> createState() => _AuthorPageState();
}

class _AuthorPageState extends State<AuthorPage> {
  RefreshController _refreshController = RefreshController();
  void _onRefresh() async {
    setState(() {});
    await Future.delayed(Duration(milliseconds: 1500));
    _refreshController.refreshCompleted();
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: SecretCatApi.fetchAuthors(),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          return SmartRefresher(
            enablePullUp: false,
            header: WaterDropHeader(
                waterDropColor: Colors.transparent,
                idleIcon: Icon(
                  Icons.refresh_outlined,
                  size: 35,
                )),
            controller: _refreshController,
            onRefresh: _onRefresh,
            child: ListView.builder(
              itemCount: snapshot.data?.length,
              itemBuilder: (context, index) => Column(
                children: [
                  ListTile(
                    leading: CircleAvatar(
                      backgroundColor: Colors.grey,
                      backgroundImage:
                          NetworkImage(snapshot.data![index].avatar.toString()),
                      radius: 25,
                    ),
                    title: Text(
                      snapshot.data![index].name,
                    ),
                  ),
                ],
              ),
            ),
          );
        }
        return SizedBox();
      },
    );
  }
}