前提
一番上のTextFieldで入力したものをElevatedButtonを押した時にListViewで表示したいです。
setStateの上までは作動していることが確認できているのでsetStateがおかしいのだと思いますが解決方法がわかりません。。
実現したいこと
ここに実現したいことを箇条書きで書いてください。
- setStateを動作させてリストを作成できるようにする
該当のソースコード
Flutter
1import 'package:flutter/material.dart'; 2 3class MyScreen extends StatelessWidget { 4 const MyScreen({Key? key}) : super(key: key); 5 6 @override 7 Widget build(BuildContext context) { 8 return Scaffold( 9 floatingActionButton: FloatingActionButton( 10 onPressed: () { 11 Navigator.push( 12 context, 13 MaterialPageRoute(builder: (context) => AddList()), 14 ); 15 }, 16 child: Icon(Icons.add)), 17 ); 18 } 19} 20 21class AddList extends StatefulWidget { 22 const AddList({Key? key}) : super(key: key); 23 24 @override 25 State<AddList> createState() => _AddListState(); 26} 27 28class _AddListState extends State<AddList> { 29 Widget build(BuildContext context) { 30 String subsubjectName = ""; 31 String subQuestion = ""; 32 String subAnswer = ""; 33 List<String> subjectName = []; 34 List<String> Question = []; 35 List<String> Answer = []; 36 37 return Scaffold( 38 appBar: AppBar(), 39 body: ListView( 40 children: [ 41 TextField( 42 maxLines: 1, 43 decoration: InputDecoration( 44 icon: Icon(Icons.book_sharp), 45 labelText: 'First', 46 ), 47 onSubmitted: (text) { 48 subsubjectName = text; 49 }, 50 ), 51 TextField( 52 decoration: InputDecoration( 53 icon: Icon(Icons.edit), 54 labelText: 'Second', 55 ), 56 onSubmitted: (text) { 57 subQuestion = text; 58 }, 59 ), 60 TextField( 61 decoration: InputDecoration( 62 icon: Icon(Icons.task_alt), 63 labelText: 'third', 64 ), 65 onSubmitted: (text) { 66 subAnswer = text; 67 }, 68 ), 69 ElevatedButton( 70 child: Text('登録'), 71 onPressed: () { 72 Navigator.of(context).pop(); 73 subjectName.add(subsubjectName); 74 Answer.add(subAnswer); 75 Question.add(subQuestion); 76 setState(() { 77 ListView.builder( 78 itemCount: subjectName.length, 79 itemBuilder: (BuildContext context, int index3) { 80 return Column( 81 children: [ 82 ListTile( 83 leading: Icon(Icons.edit), 84 title: Text(subjectName[index3]), 85 ), 86 ], 87 ); 88 }); 89 }); 90 }, 91 ), 92 ], 93 ), 94 ); 95 } 96}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。