Created
November 15, 2019 06:33
-
-
Save ryanlid/a16536a4f63c9cec7929d1b5bcbaae39 to your computer and use it in GitHub Desktop.
flutter 创建一个无限滚动的 ListView
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'; | |
import 'package:english_words/english_words.dart'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
final wordPair = new WordPair.random(); | |
// return new MaterialApp( | |
// title: 'Welcome to Flutter', | |
// home: new Scaffold( | |
// appBar: new AppBar( | |
// title: const Text('Welcome to Flutter'), | |
// ), | |
// body: new Center( | |
//// child: const Text('Hello World'), | |
//// child: new Text(wordPair.asPascalCase), | |
// child: new RandomWords(), | |
// ), | |
// ), | |
// ); | |
return new MaterialApp( | |
title: 'Startup name', | |
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) { | |
// final WordPair wordPair = new WordPair.random(); | |
// return new Text(wordPair.asPascalCase); | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text('Startup Name Generator'), | |
), | |
body: _buildSuggestions(), | |
); | |
} | |
Widget _buildSuggestions() { | |
return new ListView.builder( | |
padding: const EdgeInsets.all(6.0), | |
itemBuilder: (BuildContext _context, int i) { | |
if (i.isOdd) { | |
return new Divider(); | |
} | |
final int index = i ~/ 2; | |
if (index >= _suggestions.length) { | |
_suggestions.addAll(generateWordPairs().take(10)); | |
} | |
return _buildRow(_suggestions[index]); | |
}); | |
} | |
Widget _buildRow(WordPair pair) { | |
return new ListTile( | |
title: new Text( | |
pair.asPascalCase, | |
style: _biggerFont, | |
), | |
); | |
} | |
} |
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
name: startup_namer | |
description: A new Flutter application. | |
version: 1.0.0+1 | |
environment: | |
sdk: ">=2.1.0 <3.0.0" | |
dependencies: | |
flutter: | |
sdk: flutter | |
cupertino_icons: ^0.1.2 | |
english_words: ^3.1.0 | |
dev_dependencies: | |
flutter_test: | |
sdk: flutter | |
flutter: | |
uses-material-design: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment