写自己第一个页面
有一点命名规则:
文件名用小写+_线。
类名用骆驼名字
私类或方法用_开头。
rand_page.dart 文件:
import 'package:english_words/english_words.dart';
import 'package:flutter/material.dart';
class RandPage extends StatefulWidget {
const RandPage({Key? key}) : super(key: key);
@override
State<RandPage> createState() => _RandPageState();
}
class _RandPageState extends State<RandPage> {
final _suggestions = <WordPair>[];
final _biggerFont = const TextStyle(fontSize: 18.0);
Widget _buildSuggestions() {
return ListView.builder(
padding: const EdgeInsets.all(16.0),
itemBuilder: /*1*/ (context, i) {
if (i.isOdd) return const Divider(); /*2*/
final index = i ~/ 2; /*3*/
if (index >= _suggestions.length) {
_suggestions.addAll(generateWordPairs().take(10)); /*4*/
}
return _buildRow(_suggestions[index]);
});
}
Widget _buildRow(WordPair pair) {
return ListTile(
title: Text(
pair.asPascalCase,
style: _biggerFont,
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Startup Name Generator'),
),
body: _buildSuggestions(),
);
}
// #docregion RWS-class-only
}
main.dart 文件:
import 'package:flutter/material.dart';
import 'package:flutter_application_1/rand_page.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Startup Name Generator',
home: RandPage(),
);
}
}