If you want to delete item from listview in flutter. With this solution you’ll still see the view change. If you want to prevent that, you’ll need a new stateful page, and a bit of refactoring:
1. Make your _saved items global (only for this example)
2. Remove the _pushSaved method
3. Update the onPressed function that used to call the _pushSaved function
4. Add the stateful DetailPage instead of the _pushSaved method
Like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | import 'package:flutter/material.dart'; import 'package:english_words/english_words.dart'; void main() => runApp(new MyApp()); // create a global saved set Set<WordPair> savedGlobal = new Set<WordPair>(); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return new MaterialApp( title: 'Startup Name Generator', home: new RandomWords(), ); } } class RandomWords extends StatefulWidget { @override RandomWordsState createState() => new RandomWordsState(); } class RandomWordsState extends State<RandomWords> { final List<WordPair> _suggestions = <WordPair>[]; final TextStyle _biggerFont = const TextStyle(fontSize: 18.0); @override Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar( title: const Text('Startup Name Generator'), actions: <Widget>[ // change the onPressed function new IconButton(icon: const Icon(Icons.list), onPressed: () { Navigator.push( context, MaterialPageRoute( builder: (context) => DetailPage() ) ); }), ], ), body: _buildSuggestions(), ); } Widget _buildSuggestions() { return new ListView.builder( padding: const EdgeInsets.all(16.0), itemBuilder: (BuildContext _context, int i) { if (i.isOdd) { return const Divider(); } final int index = i ~/ 2; if (index >= _suggestions.length) { _suggestions.addAll(generateWordPairs().take(10)); } return _buildRow(_suggestions[index]); }); } Widget _buildRow(WordPair pair) { final bool alreadySaved = savedGlobal.contains(pair); return new ListTile( title: new Text( pair.asPascalCase, style: _biggerFont, ), trailing: new Icon( alreadySaved ? Icons.favorite : Icons.favorite_border, color: alreadySaved ? Colors.red : null, ), onTap: () { setState(() { if (alreadySaved) { savedGlobal.remove(pair); } else { savedGlobal.add(pair); } }); }, ); } } // add a new stateful page class DetailPage extends StatefulWidget { @override _DetailPageState createState() => _DetailPageState(); } class _DetailPageState extends State<DetailPage> { final TextStyle _biggerFont = const TextStyle(fontSize: 18.0); @override Widget build(BuildContext context) { Iterable<ListTile> tiles = savedGlobal.map((WordPair pair) { return new ListTile( onLongPress: () { setState(() { savedGlobal.remove(pair); }); }, title: new Text( pair.asPascalCase, style: _biggerFont, ), ); }); final List<Widget> divided = ListTile.divideTiles( context: context, tiles: tiles, ).toList(); return new Scaffold( appBar: new AppBar( title: const Text('Saved Suggestions'), ), body: new ListView(children: divided), ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.