Flutter第三弹:常用的Widget
在Flutter中,第三弹最常用的Widget包括但不限于以下这些:
Text
- 显示文本信息。Container
- 一个灵活的容器,可以装饰和定位其他Widget。Image
- 显示图片。Icon
- 显示图标,通常来自于Icons
类。Button
- 按钮,如RaisedButton
,FlatButton
,OutlineButton
等。ListView
- 创建一个滚动列表。Column
/Row
- 垂直/水平排列其他Widget。Stack
- 堆叠Widget。Card
- 创建卡片式布局。ProgressIndicator
- 显示进度指示器,如CircularProgressIndicator
。Checkbox
/Radio
- 复选框和单选按钮。TextField
- 输入框。Switch
- 开关按钮。Slider
- 滑块控件。
以下是这些Widget的简单使用示例:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Hello, Flutter!'),
SizedBox(height: 20.0),
Container(
color: Colors.blue,
child: Text('In a Container'),
padding: EdgeInsets.all(16.0),
),
SizedBox(height: 20.0),
Image.network('https://example.com/image.png'),
SizedBox(height: 20.0),
Icon(Icons.favorite),
SizedBox(height: 20.0),
RaisedButton(
child: Text('Press Me'),
onPressed: () {},
),
SizedBox(height: 20.0),
ListView(
children: <Widget>[
ListTile(title: Text('Item 1')),
ListTile(title: Text('Item 2')),
ListTile(title: Text('Item 3')),
],
),
SizedBox(height: 20.0),
Row(
children: <Widget>[
Text('Row Item 1'),
Text('Row Item 2'),
],
),
SizedBox(height: 20.0),
Stack(
alignment: Alignment.center,
children: <Widget>[
CircleAvatar(
backgroundColor: Colors.blue,
child: Text('A'),
),
CircleAvatar(
backgroundColor: Colors.red,
child: Text('B'),
),
],
),
评论已关闭