Advertisement
Guest User

to do

a guest
Feb 21st, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.09 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. void main() => runApp(MaterialApp(
  4.   home: Scaffold(
  5.     appBar: AppBar(
  6.       title: Text('demo App'),
  7.       centerTitle: true,
  8.     ),
  9.     body: mainApp(),
  10.   ),
  11. ));
  12.  
  13. class mainApp extends StatefulWidget {
  14.   @override
  15.   _mainAppState createState() => _mainAppState();
  16. }
  17.  
  18. class _mainAppState extends State<mainApp> {
  19.  
  20.   var txt = TextEditingController();
  21.   final List<String> names = <String>['Paweł','Ania','Michał','Władek'];
  22.   int size = 4;
  23.  
  24.   @override
  25.   Widget build(BuildContext context) {
  26.     return Center(
  27.       child: Container(
  28.         child: Column(
  29.           children: <Widget>[
  30.             Container(
  31.               color: Colors.teal,
  32.               padding: EdgeInsets.all(30),
  33.               margin: EdgeInsets.only(
  34.                 bottom: 20,
  35.               ),
  36.               child: TextField(
  37.                 controller: txt,
  38.                 onSubmitted: (String str)
  39.                 {
  40.                   setState(() {
  41.                     names.insert(0, str);
  42.                     size++;
  43.                     txt.text = "";
  44.                   });
  45.                 },
  46.                 decoration: InputDecoration(
  47.                     hintText: 'Insert text here!'
  48.                 ),
  49.               ),
  50.             ),
  51.             Expanded(
  52.               child: ListView.builder(
  53.                   itemCount: size,
  54.                   itemBuilder: (_context, index)
  55.                   {
  56.                     return Container(
  57.                       margin: EdgeInsets.all(10),
  58.                       padding: EdgeInsets.all(12),
  59.                       decoration: BoxDecoration(
  60.                           color: Colors.teal,
  61.                           borderRadius: BorderRadius.only(
  62.                             topRight: Radius.circular(10),
  63.                             topLeft: Radius.circular(10),
  64.                             bottomRight: Radius.circular(10),
  65.                             bottomLeft: Radius.circular(10),
  66.                         )
  67.                       ),
  68.                       child: Row(
  69.                         mainAxisAlignment: MainAxisAlignment.spaceBetween,
  70.                         children: <Widget>[
  71.                           Container(
  72.                             child: Text('Id: $index Name: ${names[index]}'),
  73.                           ),
  74.                           GestureDetector(
  75.                             onTap: ()
  76.                             {
  77.                               //remove
  78.                               setState(() {
  79.                                 names.removeAt(index);
  80.                                 size--;
  81.                               });
  82.                             },
  83.                             child: Container(
  84.                               child: Icon(Icons.remove),
  85.                               color: Colors.greenAccent,
  86.                             ),
  87.                           )
  88.                         ],
  89.                       ),
  90.                     );
  91.                   }
  92.               ),
  93.             )
  94.           ],
  95.         ),
  96.       )
  97.     );
  98.   }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement