티스토리 뷰

개발

플러터 목록 출력 샘플

코지식 2024. 9. 23. 01:04

목차



    반응형

     

    import 'package:archive_idea/database/database_helper.dart';
    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter/widgets.dart';
    import 'package:flutter_rating_bar/flutter_rating_bar.dart';
    import 'package:intl/intl.dart';

    import '../data/idea_info.dart';

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

    @override
    State<MainScreen> createState() => _MainScreenState();
    }

    class _MainScreenState extends State<MainScreen> {
    var dbHelper = DatabaseHelper(); // 데이터베이스 접근을 용이하게 해주는 유틸 객체
    List<IdeaInfo> lstideaInfo = []; //아이디어 목록 데이터 담길 공간

    @override
    void initState() {
    // TODO: implement initState
    super.initState();
    //아이디어 목록들 가져오기
    getIdeaInfo();

    //샘플 데이터 넣기
    //setInsertIdeaInfo();
    }

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(
    backgroundColor: Colors.white,
    elevation: 0,
    title: Text(
    'Archive Idea',
    style: TextStyle(
    color: Colors.black,
    fontSize: 32,
    fontWeight: FontWeight.bold,
    ),
    ),
    ),
    body: Container(
    margin: EdgeInsets.all(16),
    child: ListView.builder(
    itemCount: lstideaInfo.length,
    itemBuilder: (context, index) {
    return listItem(index);
    },
    ),
    ),
    floatingActionButton: FloatingActionButton(
    onPressed: () {
    // 새아이디어 작성 화면으로 이동
    },
    child: Image.asset(
    'assets/informative.png',
    width: 48,
    height: 48,
    ),
    backgroundColor: Color(0xff7f52fd).withOpacity(0.7),
    ),
    );
    }

    Widget listItem(int index) {
    return Container(
    height: 82,
    margin: EdgeInsets.only(top: 16),
    decoration: ShapeDecoration(
    shape: RoundedRectangleBorder(
    side: BorderSide(color: Color(0xffd9d9d9), width: 1),
    borderRadius: BorderRadius.circular(10),
    )),
    child: Stack(
    alignment: Alignment.centerLeft,
    children: [
    /// 아이디어 제목
    Container(
    margin: EdgeInsets.only(left: 16, bottom: 16),
    child: Text(
    lstideaInfo[index].title,
    style: TextStyle(fontSize: 16),
    ),
    ),

     

     

     

    ///일시
    Align(
    alignment: Alignment.bottomRight,
    child: Container(
    margin: EdgeInsets.only(right: 16, bottom: 8),
    child: Text(
    DateFormat('yyyy.MM.dd HH:mm').format(
    DateTime.fromMillisecondsSinceEpoch(
    lstideaInfo[index].createdAt),
    ),
    style: TextStyle(
    color: Color(0xffaeaeae),
    fontSize: 10,
    ),
    ),
    ),
    ),

    /// 아이디어 중요도 점수(별형태)
    Container(
    margin: EdgeInsets.only(left: 16, bottom: 8),
    child: Align(
    alignment: Alignment.bottomLeft,
    child: RatingBar.builder(
    initialRating: lstideaInfo[index].priority.toDouble(),
    minRating: 1,
    itemCount: 5,
    direction: Axis.horizontal,
    itemSize: 16,
    itemPadding: EdgeInsets.symmetric(horizontal: 0),
    itemBuilder: (context, index) {
    return Icon(
    Icons.star,
    color: Colors.amber,
    );
    },
    ignoreGestures: true,
    updateOnDrag: false,
    onRatingUpdate: (value) {},
    ),
    ),
    )
    ],
    ),
    );
    }

    Future<void> getIdeaInfo() async {
    await dbHelper.initDatabase();
    lstideaInfo = await dbHelper.getAllIdeaInfo();
    // 리스트 객체 역순으로 정렬
    lstideaInfo.sort((a, b) => b.createdAt.compareTo(a.createdAt));
    setState(() {});
    }

    Future<void> setInsertIdeaInfo() async {
    await dbHelper.initDatabase();
    await dbHelper.insertIdeaInfo(IdeaInfo(
    title: '# 가나다라마바사',
    motive: '길가다가 쓰레게 줍기',
    content: '상세 내용 입니다',
    priority: 5,
    feedback: '피드백 사항',
    createdAt: DateTime.now().millisecondsSinceEpoch,
    ));
    }
    }

     

    반응형