flutter 学习笔记-容器与布局(1)
布局
Widget | 对应的Element | 用途 |
---|
LeafRenderObjectWidget | LeafRenderObjectElement | Widget树的叶子节点,用于没有子节点的widget,通常基础组件都属于这一类,如Image。 |
SingleChildRenderObjectWidget | SingleChildRenderObjectElement | 包含一个子Widget,如:ConstrainedBox、DecoratedBox等 |
MultiChildRenderObjectWidget | MultiChildRenderObjectElement | 包含多个子Widget,一般都有一个children参数,接受一个Widget数组。如Row、Column、Stack等 |
Row、Column、Container、Expanded、Flex、DefaultTextStyle、SizeBox、Padding
- Row 横排,对齐方法 CrossAxisAlignment,类似flex
- Column 纵排
- Container 为SingleChildRenderObjectWidget 只有一个child,类似div
- Expanded 扩展 Row、Column、Container高度和宽度为100%
- Flex 配合Expanded设置flex比例
- DefaultTextStyle 的style参数可以设置所有文笔颜色样式
- SizeBox 可以设置容器的大小,Containe 类似
- Padding
import 'dart:async';
import 'dart:ffi';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'view/header.dart';
void main() {
debugPaintSizeEnabled = true;
//debugPaintSizeEnabled=true;
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
int num = 0;
@override
Widget build(BuildContext context) {
return MaterialApp(title: "piccre", home: MyHomePage(title: "Home"));
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
App createState() => App();
}
class App extends State {
bool switchState = true;
FocusNode fn = new FocusNode();
FocusNode pw = new FocusNode();
TextEditingController tec = TextEditingController(text: "test");
GlobalKey forms = new GlobalKey<FormState>();
final sizeBox = DefaultTextStyle(style:TextStyle(color:Colors.deepOrange,fontSize:16),child: SizedBox(
height: 200,
child: Flex(direction: Axis.horizontal, children: <Widget>[
Expanded(
flex: 1,
child: Container(width: 100, child: Text("test1"))),
Expanded(
flex: 1,
child: Container(width: 100, child: Text("test2"))),
Expanded(
flex: 1,
child: Container(width: 100, child: Text("test2"))),
Expanded(
flex: 1,
child: Container(width: 100, child: Text("test2"))),
Expanded(
flex: 1,
child: Container(width: 100, child: Text("test2"))),
Expanded(
flex: 1,
child: Container(width: 100, child: Text("test2"))),
Expanded(
flex: 1,
child: Container(width: 100, child: Text("test2"))),
]),
));
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text("picc", textAlign: TextAlign.center),
centerTitle: true),
body: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
sizeBox,
Padding(
padding: EdgeInsets.all(10.0),
child: Form(
key: forms,
autovalidate: true, //开启自动校验
child: Column(children: <Widget>[
TextFormField(
focusNode: fn,
controller: tec,
//autofocus:true,
decoration: InputDecoration(
labelText: '姓名',
hintText: '请输入姓名',
prefixIcon: Icon(Icons.account_circle)),
onChanged: (val) {
print(val);
},
validator: (v) {
print(v);
return "test1";
},
),
TextFormField(
//autofocus:true,
focusNode: pw,
decoration: InputDecoration(
labelText: '密码',
hintText: '请输入密码',
prefixIcon: Icon(Icons.keyboard)),
validator: (val) {
return "test2";
},
),
RaisedButton(
padding: EdgeInsets.all(10.0),
child: Text("登录"),
color: Theme.of(context).primaryColor,
textColor: Colors.white,
onPressed: () {
(forms.currentState as FormState).validate();
})
]),
)),
Switch(value: switchState, onChanged: onChanged),
Text("$switchState"),
Flex(direction: Axis.horizontal,mainAxisAlignment:MainAxisAlignment.spaceEvenly, children: <Widget>[
Expanded(
flex: 1,
child: Container(
padding:EdgeInsets.all(20.0),
child: Image(
image: AssetImage('lib/image/img.png')))),
Expanded(
flex: 1,
child: Container(
padding:EdgeInsets.all(20.0),
child: Image(
image: NetworkImage(
'https://avatars2.githubusercontent.com/u/20411648?s=460&v=4'))))
])
//Header()
// Container(
// child:Text("test"),
// )
]));
}
@override
void initState() {
// TODO: implement initState
super.initState();
print("1==========================");
// print(FocusScope.of(context));
new Timer(Duration(seconds: 10), () {
(FocusScope.of(context)).requestFocus(pw);
print('=================2');
});
new Timer(Duration(seconds: 20), () {
pw.unfocus();
print('===============2.5');
});
print("============3");
}
// TODO:
void onChanged(bool value) {
setState(() {
switchState = value;
});
}
}