[Flutter] 메모 11

2020. 12. 28. 23:40Flutter Mobile App/Flutter 메모 앱

반응형

수정 페이지 작성

 


뷰 페이지

[screens/view_page.dart]

 

더보기
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:memo/model/memo.dart';
import 'package:memo/repository/db_helper.dart';
import 'edit_page.dart';

class ViewPage extends StatelessWidget {
  ViewPage({Key key, this.id}) : super(key: key);
  final String id;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: [
          IconButton(
            icon: const Icon(Icons.delete),
            onPressed: () {
              print('삭제 아이콘 클릭 : $id');
              //showAlertDialog();
            },
          ),
          IconButton(
            icon: const Icon(Icons.edit),
            onPressed: () {
              print('수정');
              Navigator.push(context,
                  CupertinoPageRoute(builder: (context) => EditPage(id: id)));
            },
          )
        ],
      ),
      body: Padding(padding: EdgeInsets.all(20), child: loadBuilder()),
    );
  }

  Future<List<Memo>> loadMemo(String id) async {
    DBHelper dbHelper = DBHelper();
    return await dbHelper.selectMemo(id);
  }

  loadBuilder() {
    return FutureBuilder<List<Memo>>(
      future: loadMemo(id),
      builder: (context, snapshot) {
        if (!snapshot.hasData || snapshot.data.isEmpty) {
          return Container(
            child: Text('데이터를 불러올 수 없습니다.'),
          );
        } else {
          Memo memo = snapshot.data[0];
          return Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Text(
                memo.title,
                style: TextStyle(fontSize: 30, fontWeight: FontWeight.w500),
              ),
              Text(
                '메모 생성시간 : ${memo.createdTime.split(".")[0]}',
                style: TextStyle(fontSize: 11),
                textAlign: TextAlign.end,
              ),
              Text(
                '메모 수정시간 : ${memo.editedTime.split(".")[0]}',
                style: TextStyle(fontSize: 11),
                textAlign: TextAlign.end,
              ),
              Padding(
                padding: EdgeInsets.all(10),
              ),
              // Text(memo.text),
              Expanded(
                child: Text(memo.text),
              )
            ],
          );
        }
      },
    );
  }
}

 


수정 페이지

[screens/edit_page.dart]

 

더보기
import 'package:flutter/material.dart';
import 'package:memo/model/memo.dart';
import 'package:memo/repository/db_helper.dart';

class EditPage extends StatefulWidget {
  EditPage({Key key, this.id}) : super(key: key);
  final String id;
  @override
  _EditPageState createState() => _EditPageState();
}

class _EditPageState extends State<EditPage> {
  BuildContext _context;
  String title = '';
  String text = '';
  String createdTime = '';
  @override
  Widget build(BuildContext context) {
    _context = context;
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        actions: [
          IconButton(
            icon: const Icon(Icons.save),
            onPressed: updateDB,
          )
        ],
      ),
      body: Padding(
        padding: EdgeInsets.all(20),
        child: loadBuilder(),
      ),
    );
  }

  Future<List<Memo>> loadMemo(String id) async {
    DBHelper dbHelper = DBHelper();
    return await dbHelper.selectMemo(id);
  }

  loadBuilder() {
    return FutureBuilder<List<Memo>>(
      future: loadMemo(widget.id),
      builder: (context, snapshot) {
        if (!snapshot.hasData || snapshot.data.isEmpty) {
          return Container(
            child: Text('데이터를 불러올 수 없습니다.'),
          );
        } else {
          Memo memo = snapshot.data[0];
          title = memo.title;

          var tecTitle = TextEditingController();
          tecTitle.text = memo.title;

          text = memo.text;
          var tecText = TextEditingController();
          tecText.text = memo.text;

          createdTime = memo.createdTime;

          return Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              TextField(
                controller: tecTitle,
                maxLines: 2,
                onChanged: (String title) {
                  this.title = title;
                },
                style: TextStyle(
                  fontSize: 30,
                  fontWeight: FontWeight.w500,
                ),
                decoration: InputDecoration(
                  hintText: '메모의 제목을 입력하세요.',
                ),
              ),
              Padding(
                padding: EdgeInsets.all(10),
              ),
              TextField(
                controller: tecText,
                maxLines: 8,
                onChanged: (String text) {
                  this.text = text;
                },
                decoration: InputDecoration(hintText: '메모의 내용을 입력하세요.'),
              )
            ],
          );
        }
      },
    );
  }

  void updateDB() {
    DBHelper dbHelper = DBHelper();
    var memo = Memo(
      id: widget.id,
      title: this.title,
      text: this.text,
      createdTime: createdTime,
      editedTime: DateTime.now().toString(),
    );

    dbHelper.updateMemo(memo);
    Navigator.pop(_context);
  }
}

 

반응형

'Flutter Mobile App > Flutter 메모 앱' 카테고리의 다른 글

[Flutter] 메모 12  (0) 2020.12.29
[Flutter] 메모 10  (0) 2020.12.28
[Flutter] 메모 9  (0) 2020.12.28
[Flutter] 메모 8  (0) 2020.12.27
[Flutter] 메모 7  (0) 2020.12.27