shared_preferencesで保存したデータをbuild時に読み込んでListViewに表示したい

dart

1 TextButton(2 onPressed: () {3 Navigator.pop(context);4 _addDoneList();5 _setStudyTime();6 _addStudyTimeList();7 _addSubjectNameList();8 _dateText = '${now.month}月${now.day}日';9 _addDateList();10 spm.saveDoneList(doneList);11 spm.saveStudyTimeList(studyTimeList);12 spm.saveSubjectNameList(subjectNameList);13 spm.saveDateList(dateList);14 },15 child: const Text('追加する'),16 )17 ],18 )19 );20 }21 22 void _addDoneList() {23 setState(() {24 doneList.add(_doneText);25 });26 }27 28 void _setStudyTime() {29 _studyTimeText = '$dropdown1Value時間$dropdown2Value分';30 }31 32 void _addStudyTimeList () {33 studyTimeList.add(_studyTimeText);34 }35 36 void _addSubjectNameList() {37 subjectNameList.add(_subjectNameText);38 }39 40 void _addDateList() {41 dateList.add(_dateText);42 }43 44 45 Widget build(BuildContext context) {46 47 return DefaultTabController(48 initialIndex: 0,49 length: 2,50 child: Scaffold(51 appBar: AppBar(52 backgroundColor: Theme.of(context).colorScheme.inversePrimary,53 title: const Text('ForStudents'),54 bottom: const TabBar(55 tabs: <Widget>[56 Tab(57 child: Row(58 children: [59 Icon(Icons.checklist),60 Text('今日の取り組み'),61 ]62 )63 ),64 Tab(65 child: Row(66 children: [67 Icon(Icons.event_note),68 Text('予定'),69 ],70 ),71 ),72 ],73 ),74 ),75 body: TabBarView(76 children: <Widget>[77 Column(78 children: <Widget>[79 const Padding(80 padding: EdgeInsets.only(top: 8),81 child: Text('~まであと〇日'),82 ),83 84 Expanded(85 child: FutureBuilder (86 future: spm.load(doneList, studyTimeList, subjectNameList, dateList),87 builder: (BuildContext context, AsyncSnapshot snapshot) {88 if (snapshot.hasData && snapshot.data) {89 return ListView.builder(90 padding: const EdgeInsets.all(8),91 itemCount: doneList.length,92 itemBuilder: (BuildContext context, int index) {93 return Column(94 children: <Widget>[95 if(index == 0 || index > 0 && dateList.elementAt(index) != dateList.elementAt(index -1))96 Text(dateList.elementAt(index)),97 Card(98 shape: RoundedRectangleBorder( //枠線99 borderRadius: BorderRadius.circular(10),100 ),101 color: Theme.of(context).colorScheme.inversePrimary,102 child: Column(103 children: [104 ListTile(105 title: Text(doneList.elementAt(index)),106 subtitle: Text(studyTimeList.elementAt(index)),107 ),108 Padding(109 padding: EdgeInsets.only(bottom: 12),110 child: Text(subjectNameList.elementAt(index)),111 )112 ],113 ),114 )115 ],116 );117 }118 );119 } else if(snapshot.hasError) {120 return Text(snapshot.error.toString());121 } else {122 return const Text('データが見つかりません');123 }124 },125 )126 ),127 ]128 ),129 const Center(130 child: Text('予定が並ぶページです。'),131 ),132 ],133 ),134 floatingActionButton: FloatingActionButton(135 onPressed: _myDialog,136 tooltip: 'Increment',137 child: const Icon(Icons.add),138 ),139 ),140 );141 }

コメントを投稿

0 コメント