2019年11月18日 星期一

[windows/flutter] 無限拉的List


1.
final _suggestion = <WordPair>[];
宣告一個不會變的list,然後list裡面每個值都是WordPair資料型態

2.
final _biggerFont = const TextStyle(fontSize: 18.0);
宣告一個常數,內容是文字的大小

3.
final index = i ~/ 2;
The expression i ~/ 2 divides i by 2 and returns an integer result. For example: 1, 2, 3, 4, 5 becomes 0, 1, 1, 2, 2. This calculates the actual number of word pairings in the ListView, minus the divider widgets.
也就是說除以2的整數部分
4.
final index = i ~/ 2;
if(index >= _suggestions.length){
    _suggestions.addAll(generateWordPairs().take(10));
}
4-1. i會一直增加
4-2. i是基數的時候就畫個分隔線
4-3. i除以2無條件捨去後,若大於List的長度,List就一次加十個隨機單字


5.
class RandomWordsState extends State<RandomWords> {
  final _suggestions = <WordPair>[];
  final _biggerFont = const TextStyle(fontSize: 18.0);
  Widget _buildRow(WordPair pair){
    return ListTile(
      title: Text(
        pair.asPascalCase,
        style: _biggerFont
      )
    );
  }
  Widget _buildSuggestion() {
    return ListView.builder(
      padding: const EdgeInsets.all(16.0),
      itemBuilder: (context, i){
        if(i.isOdd) return Divider();

        final index = i ~/ 2;
        if(index >= _suggestions.length){
          _suggestions.addAll(generateWordPairs().take(10));
        }
        return _buildRow(_suggestions[index]);
      }
    );
  }
  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('StartUp Name Generator')
      ),
      body: _buildSuggestion()
    );
  }
}
5-1.ListView.builder裡面的itemBuilder屬性,為一個需要回傳ListTile的function


6.什麼是iterable
The List and Set classes are both Iterable, as are most classes in the dart:collection library.
參考網址

沒有留言:

張貼留言