共用代码
import 'package:flutter/material.dart';
void main() {
// runApp(const MyApp());
runApp(
MyApp()
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage()
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(title: Text('title'),),
body: HomeBody(),
);
}
}
一.Text
- 普通文本 normalText
class HomeBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text(
'《定风波》 苏轼 \n莫听穿林打叶声,何妨吟啸且徐行。\n竹杖芒鞋轻胜马,谁怕?一蓑烟雨任平生。',
textAlign: TextAlign.center,
maxLines: 3,
overflow: TextOverflow.ellipsis,//超出部分显示“……”
style: TextStyle(
fontSize: 25,
color: Colors.purple
),
);
}
}
- 富文本 richText
class HomeBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text.rich(
TextSpan(
children: [
TextSpan(text:'《定风波》 苏轼', ),
TextSpan(text: '莫听穿林打叶声,何妨吟啸且徐行。'),
TextSpan(text: '竹杖芒鞋轻胜马,谁怕?一蓑烟雨任平生。')
]
),
textAlign: TextAlign.center,
style: TextStyle(color: Colors.green, fontSize: 15),
);
}
}
二.Button
- ElevatedButton 带背景色按钮
- TextButton 无背景色按钮
- OutlinedButton 带边框按钮
- IconButton
- FloatingActionButton 浮动按钮
- ButtonBar 按钮组
- 创建基础视图
/**
ElevatedButton
TextButton
OutlinedButton
IconButton
FloatingActionButton
*/
class HomeBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(
children: [
ElevatedButton(
onPressed: (){
print('ElevatedButton');
},
style: ButtonStyle(
backgroundColor:MaterialStateProperty.all(Colors.green),
// shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(10.0)),
),
child: Text('ElevatedButton')
),
TextButton(
onPressed: (){
print('TextButton');
},
child: Text('TextButton')
),
OutlinedButton(
onPressed: (){
print('OutlinedButton');
},
child: Text('OutlinedButton')
),
IconButton(
onPressed: (){
},
icon: Icon(Icons.add)
),
FloatingActionButton(
onPressed: (){
print('FloatingBtn');
},
child: Text('FloatingBtn'),
),
],
);
}
}
- 升级视图
/**
ElevatedButton 带背景色按钮
TextButton 无背景色按钮
OutlinedButton 带边框按钮
IconButton
FloatingActionButton 浮动按钮
ButtonBar 按钮组
*/
class HomeBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(
children: [
ElevatedButton(
onPressed: (){
print('ElevatedButton代替旧版RaisedButton');
},
style: ButtonStyle(
backgroundColor:MaterialStateProperty.resolveWith((states) {
if (states.contains(MaterialState.pressed)) {
return Colors.green;
} else {
return Colors.yellow;
}
}),
foregroundColor: MaterialStateProperty.all(Colors.blueGrey),
// shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(10.0)),
),
child: Text('ElevatedButton')
),
ElevatedButton.icon(
onPressed: (){},
icon: Icon(Icons.delete),
label: Text('Icon_ElevatedButton')
),
TextButton(
onPressed: (){
print('TextButton代替旧版FlatButton');
},
child: Text('TextButton')
),
OutlinedButton(
onPressed: (){
print('OutlinedButton');
},
child: Text('OutlinedButton'),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.grey),
//圆角
shape: MaterialStateProperty.all(
RoundedRectangleBorder(borderRadius: BorderRadius.circular(20))
),
//边框
side: MaterialStateProperty.all(
BorderSide(width: 5, color: Colors.red)
),
),
),
IconButton(
onPressed: (){
},
icon: Icon(Icons.add)
),
FloatingActionButton(
onPressed: (){
print('FloatingBtn');
},
child: Icon(Icons.add),
backgroundColor: Colors.green,
foregroundColor: Colors.yellow,
//阴影偏移量
elevation: 5,
tooltip: 'tooltip',
mini: false,
),
ButtonBar(
children: [
IconButton(onPressed: (){}, icon: Icon(Icons.arrow_back)),
IconButton(onPressed: (){}, icon:Icon(Icons.add)),
IconButton(onPressed: (){}, icon: Icon(Icons.next_plan))
],
)
],
);
}
}
三.Image
- 基础
/**
image
NetworkImage 加载网络图片
AssetImage 加载本地图片
*/
class HomeBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
return
Column(
children: [
Image(
image: NetworkImage('https://tva1.sinaimg.cn/large/006y8mN6gy1g72j6nk1d4j30u00k0n0j.jpg'),
width: 200,
height: 200,
fit: BoxFit.fitWidth,
// alignment: Alignment.bottomCenter,
//-1 -> 1
// alignment: Alignment(-1, -1),
repeat: ImageRepeat.repeatY,
),
Image(
image: AssetImage('images/iconSun.png'),
width: 200,
height: 200,
)
],
);
}
}
- 带占位图image
FadeInImage(
placeholder: AssetImage('images/iconSun.png'),
image: NetworkImage('https://tva1.sinaimg.cn/large/006y8mN6gy1g72j6nk1d4j30u00k0n0j.jpg'),
fadeInDuration: Duration(seconds: 3),
fadeOutDuration: Duration(seconds: 2),
)
- icon 字体图标
Icon(Icons.arrow_left, size: 300, color: Colors.green,);
- 圆角头像
- CircleAvatar
- ClipOval
- Container+BoxDecoration
- 圆角图片
- ClipRRect
- Container+BoxDecoration
四.Textfield
- textfield
class HomeBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFiledDemo()
],
),
);
}
}
class TextFiledDemo extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return TextFiledState();
}
}
class TextFiledState extends State<TextFiledDemo> {
final textEditingController = TextEditingController();
@override
void initState() {
super.initState();
textEditingController.text = 'Hello World';
textEditingController.addListener(() {
print('textEditingController---${textEditingController.text}');
});
}
@override
Widget build(BuildContext context) {
// TODO: implement build
return Column(
children: [
TextField(
controller: textEditingController,
decoration: InputDecoration(
icon: Icon(Icons.people),
labelText: 'username',
hintText: '请输入用户名',
border: InputBorder.none,
filled: true,
fillColor: Colors.lightGreen
),
onChanged: (value) => print('onChanged $value'),
onSubmitted: (value) => print('onSubmitted $value'),
),
SizedBox(height: 10,),
TextField(
decoration: InputDecoration(
icon: Icon(Icons.security),
labelText: 'password',
hintText: '请输入密码',
border: UnderlineInputBorder(),
filled: true,
),
onChanged: (value) => print('onChanged $value'),
onSubmitted: (value) => print('onSubmitted $value'),
),
],
);
}
}
- TextFormField
class TextFiledState extends State<TextFiledDemo> {
final registerKey = GlobalKey<FormState>();
late String userName, password;
pressedSaveBtnAction (){
registerKey.currentState!.save();
print('${this.userName},${this.password}');
}
@override
Widget build(BuildContext context) {
return Form(
key: registerKey,
child: Column(
// mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(
decoration: InputDecoration(
icon: Icon(Icons.people),
labelText: 'name'
),
onSaved: (newValue) {
this.userName = newValue!;
},
),
TextFormField(
decoration: InputDecoration(
icon: Icon(Icons.password),
labelText: 'password'
),
onSaved: (newValue) {
this.password = newValue!;
},
),
SizedBox(height: 60),
Container(
width: 300,
height: 40,
child: ElevatedButton(onPressed: (){
pressedSaveBtnAction();
},
style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Colors.green)),
child: Text('Button')),
),
],
)
);
}
}
//flutter: 1234,66666666
五. 组件Widget
基础代码
import 'package:flutter/material.dart';
void main() {
// runApp(const MyApp());
runApp(
MyApp()
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage()
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(title: Text('title'),),
body: HomeBody(),
);
}
}
- algin center
//algin center
class HomeBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Icon(Icons.pets, size: 50, color: Colors.red,),
// alignment: Alignment.bottomRight,
widthFactor: 3,
heightFactor: 3,
);
}
}
- Padding
//Padding
class HomeBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(30),
child: Text(
'莫听穿林打叶声,何妨吟啸且徐行。竹杖芒鞋轻胜马,谁怕?一蓑烟雨任平生。',
style: TextStyle(
color: Colors.red,
fontSize: 18
),
)
);
}
}
- container
//container
class HomeBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
// alignment: Alignment.bottomRight,
width: 200,
height: 200,
color: Colors.grey[300],
child: Icon(Icons.pets, size: 50, color: Colors.red,),
)
);
}
}
- 阴影圆角图片
//decoration borderRadius boxShadow gradient
class HomeBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
width: 200,
height: 200,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
//阴影
boxShadow: [
BoxShadow(
offset: Offset(5, 5),
color: Colors.purple,
blurRadius: 5
)
],
image: DecorationImage(
// image: AssetImage('images/iconSun.png')
image: NetworkImage('https://tva1.sinaimg.cn/large/006y8mN6gy1g7aa03bmfpj3069069mx8.jpg')
)
),
),
);
/**
// 渐变阴影圆角图片
return Center(
child: Container(
// alignment: Alignment.bottomRight,
width: 200,
height: 200,
child: Icon(Icons.pets, size: 50, color: Colors.white,),
// color: Colors.grey[300],
//装饰
decoration: BoxDecoration(
color: Colors.amber,
border: Border.all(
color: Colors.redAccent,
width: 3,
style: BorderStyle.solid
),
//圆角
borderRadius: BorderRadius.circular(20),
//阴影
boxShadow: [
BoxShadow(
offset: Offset(5, 5),
color: Colors.purple,
blurRadius: 5
)
],
//坡度,颜色渐变
gradient: LinearGradient(
colors: [
Colors.green,
Colors.red
]
)
),
)
);
*/
}
}
- Row 横排视图
//Row 横排 Expanded设置弹性系数flex
class HomeBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisSize: MainAxisSize.max,//最大宽度分布
children: [
Container(color: Colors.red, width: 60, height: 60),
Container(color: Colors.blue, width: 80, height: 80),
Expanded(
child: Container(color: Colors.green, width: 90, height: 90),
flex: 1,
),
Expanded(
child: Container(color: Colors.yellow, width: 70, height: 70),
flex: 1,//弹性系数
)
],
);
}
}
- Column 纵排视图
//Column 纵排 Expanded设置弹性系数flex
class HomeBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisSize: MainAxisSize.max,//最大宽度分布
children: [
Container(color: Colors.red, width: 60, height: 60),
Container(color: Colors.blue, width: 80, height: 80),
Expanded(
child: Container(color: Colors.green, width: 90, height: 90),
flex: 1,
),
Expanded(
child: Container(color: Colors.yellow, width: 70, height: 70),
flex: 1,//弹性系数
)
],
);
}
}
- stack 叠加视图
//stack 叠加视图 stack常与Positioned一起使用,用来决定组件在Stack中的位置
class HomeBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Stack(
children: [
Container(
color: Colors.purple,
width: 300,
height: 300,
),
Positioned(
child: Icon(Icons.favorite, size: 50, color: Colors.white,),
width: 30,
height: 30,
top: 30,
left: 30,
),
Positioned(
child: Text('hello', style: TextStyle(color: Colors.white, fontSize: 20),),
bottom: 30,
right: 30,
)
],
);
}
}