Flutter - 组件 - 从认识Text开始学习

刚熟悉了dart语法,创建对象不用关键字new了。创建Text还不容易。

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Text(),
    );
  }
}
复制代码

什么!报错了?我创建一个空的Text都不行???

image.png

需要一个参数,但是我没给。ctrl+P (window快捷键)

image.png

好吧,必选参数,需要一个字符串。

hello world。

完成!!!

IMG_20211118_150906_edit_84314007026194.jpg

搞什么???状态栏都遮住了,字还这么大,底下还有两道杠。

ctrl+p

image.png

调整字体,只能是你了——style。 需要一个,一个...ctrl+鼠标...————TextStyle

image.png

TextStyle需要什么

ctrl+P

ctrl+鼠标

需要......

ctrl+p

ctrl+鼠标

学习完成。

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Text("hello world", style: TextStyle(fontSize: 12, color: Colors.yellow)),
    );
  }
}
复制代码

总结:熟悉运用 ctrl+pctrl+鼠标 才是关键。一个组件那么多的参数,哪能一个一个一个的学。组件那么多,哪能一个一个的学。

摸着石头过河的感觉很好,能感觉到水的深浅,不至于看到水就恐惧。加油学习!

おすすめ

転載: juejin.im/post/7031819776083099662