From db2d079da303eda2dffb03478a54994571964dd4 Mon Sep 17 00:00:00 2001 From: lancegin Date: Sat, 22 Sep 2018 11:17:58 +0800 Subject: [PATCH] add: ListView --- lib/main.dart | 59 +++++++++++++++++++++++++++++++++++-------- pubspec.lock | 7 +++++ pubspec.yaml | 1 + test/widget_test.dart | 2 +- 4 files changed, 58 insertions(+), 11 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 83f40ce..d6a37ed 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,20 +1,59 @@ import 'package:flutter/material.dart'; +import 'package:english_words/english_words.dart'; -void main() => runApp(new MyApp()); +void main() => runApp(new MinaOTP()); -class MyApp extends StatelessWidget { +class MinaOTP extends StatelessWidget { @override Widget build(BuildContext context) { 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('Hello World'), - ), + title: 'MinaOTP', + home: new RandomWords(), + ); + } +} + +class RandomWords extends StatefulWidget { + @override + createState() => new RandomWordsState(); +} + +class RandomWordsState extends State { + final _suggestions = []; + final _biggerFont = const TextStyle(fontSize: 18.0); + + Widget _buildSuggestions() { + return new ListView.builder( + padding: const EdgeInsets.all(16.0), + itemBuilder: (context, i) { + if (i.isOdd) return new Divider(); + + final 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, + ), + ); + } + + @override + Widget build(BuildContext context) { + return new Scaffold( + appBar: new AppBar( + title: new Text('MinaOTP'), ), + body: _buildSuggestions(), ); } } \ No newline at end of file diff --git a/pubspec.lock b/pubspec.lock index 7e973e6..9886985 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -71,6 +71,13 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "0.1.2" + english_words: + dependency: "direct main" + description: + name: english_words + url: "https://pub.flutter-io.cn" + source: hosted + version: "3.1.3" flutter: dependency: "direct main" description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index de64c99..000fe78 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,6 +8,7 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^0.1.2 + english_words: ^3.1.0 dev_dependencies: flutter_test: diff --git a/test/widget_test.dart b/test/widget_test.dart index 7313115..519947e 100644 --- a/test/widget_test.dart +++ b/test/widget_test.dart @@ -12,7 +12,7 @@ import 'package:minaotp_flutter/main.dart'; void main() { testWidgets('Counter increments smoke test', (WidgetTester tester) async { // Build our app and trigger a frame. - await tester.pumpWidget(new MyApp()); + await tester.pumpWidget(new MinaOTP()); // Verify that our counter starts at 0. expect(find.text('0'), findsOneWidget);