參考網址
1. pubspec.yaml檔案的dependencies中加入套件english_words: ^3.1.0
dev_dependencies:
flutter_test:
sdk: flutter
english_words: ^3.1.0
2.
import 'package:english_words/english_words.dart';
引入套件3.
final wordPair = WordPair.random();
final就是個不會改變的回傳值4.
body: Center(
child: Text(wordPair.asPascalCase)
)
asPascalCase就是大駝峰5.
class RandomWordsState extends State<RandomWords> {
Widget build(BuildContext context) {
final wordPair = WordPair.random();
return Text(wordPair.asPascalCase);
}
}
class RandomWords extends StatefulWidget{
RandomWordsState createState() => RandomWordsState();
}
先看結果6.
class RandomWordsState extends State<RandomWords> {
你今天要自己創一個組件(stateful)的話,就要產生兩個class,第一個是回傳外觀的State7.
class RandomWords extends StatefulWidget{
RandomWordsState createState() => RandomWordsState();
}
第二個是create state的StatefulWidget( 等於要用createSate這個方法來執行你自己創的class(做的事情是回傳一個外觀) )
8.
import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget{
Widget build(BuildContext context){
return MaterialApp(
title: 'My Title',
home: Scaffold(
appBar: AppBar(
title: Text('Bar Title')
),
body: Center(
child: RandomWords()
)
)
);
}
}
class RandomWordsState extends State<RandomWords> {
Widget build(BuildContext context) {
final wordPair = WordPair.random();
return Text(wordPair.asPascalCase);
}
}
class RandomWords extends StatefulWidget{
RandomWordsState createState() => RandomWordsState();
}
完全體
沒有留言:
張貼留言