Last active
May 17, 2023 01:46
-
-
Save alann-maulana/ba2e943041d4f6051b2151d5f2529179 to your computer and use it in GitHub Desktop.
Multiple TextEditingController
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
final Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MaterialApp( | |
theme: ThemeData.dark().copyWith( | |
scaffoldBackgroundColor: darkBlue, | |
), | |
debugShowCheckedModeBanner: false, | |
home: ListData(), | |
)); | |
} | |
class ListData extends StatefulWidget { | |
@override | |
_ListDataState createState() => _ListDataState(); | |
} | |
class _ListDataState extends State<ListData> { | |
final List<List<String>> dataList = [ | |
<String>['Rose', 'SunFlower'], | |
<String>['SUV', 'CityCar', 'Jeep'], | |
]; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Data List'), | |
), | |
body: ListView( | |
children: ListTile.divideTiles( | |
context: context, | |
tiles: dataList.map( | |
(list) { | |
return ListTile( | |
leading: CircleAvatar( | |
child: Text('${list.length}'), | |
), | |
title: Text(list.join(', ')), | |
onTap: () async { | |
final editData = await Navigator.push( | |
context, | |
MaterialPageRoute( | |
builder: (context) => Form(data: list), | |
fullscreenDialog: true, | |
), | |
); | |
if (editData != null) { | |
final index = dataList.indexOf(list); | |
if (index != -1) { | |
setState(() { | |
dataList[index] = editData; | |
}); | |
} | |
} | |
}); | |
}, | |
), | |
).toList(), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () async { | |
final newData = await Navigator.push( | |
context, | |
MaterialPageRoute( | |
builder: (context) => Form(), | |
fullscreenDialog: true, | |
), | |
); | |
if (newData != null) { | |
setState(() { | |
dataList.add(newData); | |
}); | |
} | |
}, | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
} | |
class Form extends StatefulWidget { | |
final List<String> data; | |
Form({List<String> data}) : data = data ?? <String>[]; | |
@override | |
_FormState createState() => _FormState(); | |
} | |
class _FormState extends State<Form> { | |
List<TextEditingController> controllers; | |
@override | |
void initState() { | |
super.initState(); | |
setState(() { | |
if (widget.data.isNotEmpty) { | |
controllers = widget.data.map((s) { | |
return TextEditingController(text: s); | |
}).toList(); | |
} else { | |
controllers = []; | |
} | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text('Form'), actions: [ | |
IconButton( | |
icon: Icon(Icons.save), | |
onPressed: () { | |
if (controllers.isEmpty) { | |
print('Data is empty, cannot saved'); | |
return; | |
} | |
final list = controllers.map((c) => c.text).toList(); | |
Navigator.pop(context, list); | |
}, | |
), | |
]), | |
body: controllers == null | |
? CircularProgressIndicator() | |
: ListView( | |
children: controllers.map((controller) { | |
return Container( | |
padding: const EdgeInsets.all(16), | |
child: TextField( | |
controller: controller, | |
decoration: InputDecoration( | |
labelText: | |
'Type data index-${controllers.indexOf(controller) + 1}', | |
), | |
), | |
); | |
}).toList(), | |
), | |
floatingActionButton: Row( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
FloatingActionButton( | |
heroTag: 'min', | |
onPressed: () { | |
setState(() { | |
controllers.removeLast(); | |
}); | |
}, | |
child: Icon(Icons.remove), | |
), | |
SizedBox(width: 8), | |
FloatingActionButton( | |
heroTag: 'add', | |
onPressed: () { | |
setState(() { | |
controllers.add(TextEditingController()); | |
}); | |
}, | |
child: Icon(Icons.add), | |
) | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment