2 글 보임 - 1 에서 2 까지 (총 2 중에서)
-
글쓴이글
-
2023년 8월 2일 15:00 #93193
오승준참가자import 'package:flutter/material.dart';
void main() { runApp( MaterialApp( home:MyApp() ) ); }
class MyApp extends StatefulWidget { MyApp({super.key});
@override State<MyApp> createState() => _MyAppState(); }
class _MyAppState extends State<MyApp> { var total = 3; var num = '완료!'; var a = 1; var name=['정유성', '김민서', '김도연']; var like=[0,0,0];
addName(){ setState(() { name.add('말숙'); }); }
addOne(){ setState((){ total++; }); }
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title : Text(total.toString()),), floatingActionButton: FloatingActionButton( child: Text(a.toString()), onPressed: (){ showDialog( context: context, builder: (context){ return DialogUI(addOne : addOne, addName: addName); }); },), body: ListView.builder( itemCount: name.length, itemBuilder: (context, i){ return ListTile( leading: Text(like[i].toString()), title: Text(name[i]), trailing: ElevatedButton(child: Text('좋아요'), onPressed: (){ setState(() { like[i]++; }); },), ); }, ),
bottomNavigationBar: BottomAppBar(), );
} }
class DialogUI extends StatelessWidget { DialogUI({super.key, this.addOne, this.addName}); final addOne; final addName; var inputData = TextEditingController();
@override Widget build(BuildContext context) { return AlertDialog( title: Text('Friend'), content: SingleChildScrollView( child: TextField( controller: inputData, decoration: InputDecoration(hintText: '친구이름'), ), ),
actions: [ TextButton(onPressed: (){addOne(); addName();}, child: Text('완료')), TextButton(onPressed: (){Navigator.pop(context);}, child: Text('취소')) ], ); } } 말숙을 추가시키려고 완료버튼을 누르면 RangeError(index): Index out of range: index should be less than 3:3 에러가 뜹니다. 무엇이 문제일까요ㅜ
-
글쓴이글
2 글 보임 - 1 에서 2 까지 (총 2 중에서)
- 답변은 로그인 후 가능합니다.