前端技术Web前端之路前端开发

【Flutter 极速指南】基础篇

2019-05-11  本文已影响3人  一俢

这篇文章你能学习到:

创建 Flutter App

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Welcome to Flutter'),
        ),
        body: Center(
          child: Text('Hello World'),
        ),
      ),
    );
  }
}

使用外部包


dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^0.1.2
  english_words: ^3.1.0
[my_app] flutter packages get
Running "flutter packages get" in my_app...                      4.1s
exit code 0
import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
// lib/main.dart
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 = WordPair.random(); // use english_words package
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Welcome to Flutter'),
        ),
        body: Center(
          // child: Text('Hello World'), // Replace the highlighted text...
          child: Text(wordPair.asCamelCase), // With this highlighted text.
        ),
      ),
    );
  }
}

增加一个有状态部件

无状态部件是不可变的,这意味着无状态部件的属性不能改变,他们都是最终态。

有状态部件会在部件的生命周期内发生状态变化。实现有状态部件至少需要两个类:一个 StatefulWidget 类,它创建了一个 State 类的实例。StatefulWidget 类它本身是不变的,但是 State 类在整个生命周期中一直存在。

class RandomWordsState extends State<RandomWords> {
    // TODO Add build method
}
class RandomWords extends StatefulWidget {
    @override
    RandomWordsState createState() => new RandomWordsState();
}
class RandomWordsState extends State<RandomWords> {
    @override
    Widget build(BuildContext context) {
        final wordPair = WordPair.random();
        return Text(wordPair.asPascalCase);
    }
}
class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
        final wordPair = WordPair.random();  // Delete this line

        return MaterialApp(
            title: 'Welcome to Flutter',
            home: Scaffold(
                appBar: AppBar(
                    title: Text('Welcome to Flutter'),
                ),
                body: Center(
                    //child: Text(wordPair.asPascalCase), // Change the highlighted text to...
                    child: RandomWords(), // ... this highlighted text
                ),
            ),
        );
    }
}

创建一个无限滚动列表

这一步骤中,你会扩展 RandomWordsState 来生成一个单词列表。当用户滚动时,ListView 部件显示的列表会无限延长,ListView 部件允许你创建惰性列表。

class RandomWordsState extends State<RandomWords> {
    final _suggestions = <WordPair>[];

    final _biggerFont = const TextStyle(fontSize: 18.0);
    ...
}
class RandomWordsState extends State<RandomWords> {
    ...
    Widget _buildSuggestions() {
        return ListView.builder(
            padding: const EdgeInsets.all(16.0),
            // The itemBuilder callback is called once per suggested word pairing,
            // and places each suggestion into a ListTile row.
            // For even rows, the function adds a ListTile row for the word pairing.
            // For odd rows, the function adds a Divider widget to visually
            // separate the entries. Note that the divider may be difficult
            // to see on smaller devices.
            itemBuilder: (context, i) {
            // Add a one-pixel-high divider widget before each row in theListView.
            if (i.isOdd) return Divider();

            // The syntax "i ~/ 2" divides i by 2 and returns an integer result.
            // For example: 1, 2, 3, 4, 5 becomes 0, 1, 1, 2, 2.
            // This calculates the actual number of word pairings in the ListView,
            // minus the divider widgets.
            final index = i ~/ 2;
            // If you've reached the end of the available word pairings...
            if (index >= _suggestions.length) {
                // ...then generate 10 more and add them to the suggestions list.
                _suggestions.addAll(generateWordPairs().take(10));
            }
            return _buildRow(_suggestions[index]);
            }
        );
    }
}
class RandomWordsState extends State<RandomWords> {
    ...

    Widget _buildRow(WordPair pair) {
        return ListTile(
            title: Text(
            pair.asPascalCase,
            style: _biggerFont,
            ),
        );
    }
}
class RandomWordsState extends State<RandomWords> {
    ...
    @override
    Widget build(BuildContext context) {
        final wordPair = WordPair.random(); // Delete these two lines.
        return Text(wordPair.asPascalCase);
        return Scaffold (
        appBar: AppBar(
            title: Text('Startup Name Generator'),
        ),
        body: _buildSuggestions(),
        );
    }
    ...
}
class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
            return MaterialApp(
            title: 'Startup Name Generator',
            home: RandomWords(),
        );
    }
}

〖坚持的一俢〗

上一篇 下一篇

猜你喜欢

热点阅读