当你的Flutter项目可以运行的时候就可以来摆弄控件了……
我们开始使用一个名为 english_words 的开源软件包,其中包含数千个最常用的英文单词以及一些实用功能。
你可以 在 pub.dartlang.org 上找到 english_words 软件包以及其他许多开源软件包。
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.0
english_words: ^3.1.0 # 新增了这一行,此处有格式限定,最好和上边对齐
import ‘package:english_words/english_words.dart’; // 新增了这一行
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final wordPair = new WordPair.random(); //新增了这一行
return new MaterialApp(
title: 'Welcome to Flutter',
home: new Scaffold(// 使用 Scaffold 类实现基础的 Material Design 布局
appBar: new AppBar(
title: const Text('Welcome to Flutter'),
),
// body: const Center(//conset指代常量
// child: const Text('Hello World'),我们不用这样的方式生成文字了
// ),
body: new Center(
// 这里把之前的 "const" 换成了 "new".
child: new Text(wordPair.asPascalCase), // 这是新的文字生成方式
),
),
);
}
}
/**
* 创建一个 state 类,这个类可以在任意地方创建而不一定非要在 MyApp 里,
* 我们的示例代码是放在 MyApp 类的最下面了
* 注意一下 State 的声明。
* 这表明我们在使用专门用于 RandomWords 的 State 泛型类。
* 应用的大部分逻辑和状态都在这里 —— 它会维护 RandomWords 控件的状态。
* 这个类会保存代码生成的单词对,这个单词对列表会随着用户滑动而无限增长,另外还会保存用户喜爱的单词对(第二部分),
* 也即当用户点击爱心图标的时候会从喜爱的列表中添加或者移除当前单词对。
* 重写 build 方法,该方法通过将生成单词对的代码从上边 MyApp 移动到 RandomWordsState 来生成单词对。
*/
class RandomWordsState extends State<RandomWords> {
@override
Widget build(BuildContext context) {
final WordPair wordPair=new WordPair.random();
return new Text(wordPair.asPascalCase););
/**
* 添加有状态的 RandomWords widget 到 main.dart,
* RandomWords widget 除了创建 State 类之外几乎没有其他任何东西:
*/
class RandomWords extends StatefulWidget {
@override
RandomWordsState createState() => new RandomWordsState();
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final WordPair wordPair = new WordPair.random(); // 删掉本行
return new MaterialApp(
title: 'Welcome to Flutter',
home: new Scaffold(
appBar: new AppBar(
title: new Text('Welcome to Flutter'),
),
body: new Center(
//child: new Text(wordPair.asPascalCase), // 修改本行内容
child: new RandomWords(), // 修改成本行代码
),
),
);
}
}
class RandomWordsState extends State<RandomWords> {
// 添加如下两行
final List _suggestions = [];
final TextStyle _biggerFont = const TextStyle(fontSize: 18.0);
...
}
/**
* 向 RandomWordsState 类添加 _buildSuggestions() 函数
* itemBuilder 值是一个匿名回调函数, 接受两个参数- BuildContext 和行迭代器 i。
* 迭代器从 0 开始, 每调用一次该函数,i 就会自增 1,对于每个建议的单词对都会执行一次。
* 该模型允许建议的单词对列表在用户滚动时无限增长。
*/
Widget _buildSuggestions() {
return new ListView.builder(
padding: const EdgeInsets.all(16.0),
// 对于每个建议的单词对都会调用一次 itemBuilder,
// 然后将单词对添加到 ListTile 行中
// 在偶数行,该函数会为单词对添加一个 ListTile row.
// 在奇数行,该函数会添加一个分割线 widget,来分隔相邻的词对。
// 注意,在小屏幕上,分割线看起来可能比较吃力。
itemBuilder: (BuildContext _content,int i){
if(i.isOdd){
// 在每一列之前,添加一个1像素高的分隔线widget
return const Divider();
}
// 语法 "i ~/ 2" 表示i除以2,但返回值是整形(向下取整)
// 比如 i 为:1, 2, 3, 4, 5 时,结果为 0, 1, 1, 2, 2,
// 这可以计算出 ListView 中减去分隔线后的实际单词对数量
final int index=i ~/2;
// 如果是建议列表中最后一个单词对
if(index>=_suggetions.length){
// ...接着再生成10个单词对,然后添加到建议列表
_suggetions.addAll(generateWordPairs().take(10));
}
return _buildRow(_suggetions[index]);
},
);
}
Widget _buildRow(WordPair pair){
return new ListTile(
title: new Text(
pair.asPascalCase,
style: _biggerFont,
),
);
}
class RandomWordsState extends State<RandomWords> {
// 在 Dart 语言中使用下划线前缀标识符,会强制其变成私有。
final List _suggetions = [];
final TextStyle _biggerFont = const TextStyle(fontSize: 18.0);
@override
Widget build(BuildContext context) {
// 使用 Scaffold 类实现基础的 Material Design 布局
return new Scaffold(
appBar: new AppBar(
title: const Text('Startup Name Generator'),
),
body: _buildSuggestions(),
);
// final WordPair wordPair=new WordPair.random();
// return new Text(wordPair.asPascalCase);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final wordPair = new WordPair.random(); //新增了这一行
return new MaterialApp(
title: 'Flutter 下的List列表',
home: new RandomWords(),
// title: 'Welcome to Flutter',
// home: new Scaffold(// 使用 Scaffold 类实现基础的 Material Design 布局
// appBar: new AppBar(
// title: const Text('Welcome to Flutter'),
// ),
//// body: const Center(//conset指代常量
//// child: const Text('Hello World'),我们不用这样的方式生成文字了
//// ),
// body: new Center(
// // 这里把之前的 "const" 换成了 "new".
//// child: new Text(wordPair.asPascalCase), // 这是新的文字生成方式
// child: new RandomWords(),
// ),
// ),
);
}
}