当前位置:网站首页>Flutter04 widget initial experience
Flutter04 widget initial experience
2022-04-22 02:48:00 【weixin_ forty-two million five hundred and eighty thousand six 】
First experience Widget
One . Widget
1.1. Everything is Widget
- Flutter Almost everything you see in the entire application is Widget, Even the setting of the inner margin is Widget;
- Flutter Medium Widget( Widget or component ), amount to iOS、Android Control in , amount to Vue、React Components in ; but Fullter in Widget More thorough .
1.2. Material Design style
- material yes Google A set of design styles promoted by the company , Or design language 、 Design specifications, etc ;
- There are many design specifications , For example, color. 、 Typesetting of words 、 Respond to animation and overreaction 、 Fill... Etc ;
- stay Flutter Medium and high integration Material Style Widget;
Two . Instance of a hello flutter
2.1. text
import 'package:flutter/material.dart';
void main() {
// runApp(const MyApp());
runApp(Text('hello flutter', textDirection: TextDirection.ltr));
}

2.2. In the middle
void main() {
// runApp(const MyApp());
runApp(
Center(
child: Text(
'hello flutter',
textDirection: TextDirection.ltr,
style: TextStyle(fontSize: 30),
)
)
);
}
2.3. Add navigation bar
void main() {
// runApp(const MyApp());
runApp(
MaterialApp(//material style
home: Scaffold(// The scaffold
appBar: AppBar(// The navigation bar
title: Text(" The first application "),
),
body: Center(// Main view
child: Text(
'hello flutter',
textDirection: TextDirection.ltr,
style: TextStyle(fontSize: 30),
)
),
)
)
);
}
2.4. improvement
- widget:
- Stateless Widget: StatelessWidget
- Stateful Widget: StatefulWidget
- StatefulWidget It's the state of it Widget. It's when the program is running , The state is allowed to change , To write a program that can be operated by users App, You have to use StatefulWidget.
void main() {
// runApp(const MyApp());
runApp(
MyApp()
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(//material style
home: HomePage()
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(// The scaffold
appBar: AppBar(// The navigation bar
title: Text(" The first application !"),
),
body: HHContentBody(),
);
}
}
class HHContentBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(// Main view
child: Text(
'hello flutter',
textDirection: TextDirection.ltr,
style: TextStyle(fontSize: 30),
)
);
}
}

3、 ... and . Case 2 Agree to the agreement
- be-all Widget Be annotated @immutable modification , All member variables are immutable
3.1. Error code
// be-all Widget Be annotated @immutable modification , Internal cannot contain variable attributes
class HHContentBody extends StatelessWidget {
var flag = true;// Wrong writing
@override
Widget build(BuildContext context) {
return Center(// Main view
child: Row(// That's ok
mainAxisAlignment: MainAxisAlignment.center,// In the middle
children: [
Checkbox(value: flag, onChanged: (flag){
ValueKey(flag);
}),
Text(' Agree to the agreement '),
],
)
);
}
}
3.2. Correct code
void main() {
// runApp(const MyApp());
runApp(
MyApp()
);
}
/** * widget: * Stateful Widget: StatefulWidget * Stateless Widget: StatelessWidget */
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(//material style
home: HomePage()
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(// The scaffold
appBar: AppBar(// The navigation bar
title: Text(" The first application !"),
),
body: HHContentBody(),
);
}
}
class HHContentBody extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return HHContentBodyState();
}
}
//flag: state
class HHContentBodyState extends State<HHContentBody> {
var flag = true;
@override
Widget build(BuildContext context) {
return Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Checkbox(value: flag, onChanged: (value){
// flag = value!;
print(flag);
setState(() {
flag = value!;
});
}),
Text(' Agree to the agreement ')
],
),
);
}
}

Four . Case three list list
- ListView Scroll view
- Column Vertical view
- decoration decorate
- padding Margin
- crossAxisAlignment Cross shaft
- SizedBox Middleware interval
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(),
);
}
}
//ListView Scroll view
class HomeBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return ListView(
children: [
HomeProductItem('title', 'desc', 'https://tva1.sinaimg.cn/large/006y8mN6gy1g72j6nk1d4j30u00k0n0j.jpg'),
SizedBox(height: 8),
HomeProductItem('title', 'desc', 'https://tva1.sinaimg.cn/large/006y8mN6gy1g72imm9u5zj30u00k0adf.jpg'),
SizedBox(height: 8),
HomeProductItem('title', 'desc', 'https://tva1.sinaimg.cn/large/006y8mN6gy1g72imqlouhj30u00k00v0.jpg'),
],
);
}
}
/** Column Vertical view decoration decorate padding Margin crossAxisAlignment Cross shaft SizedBox Middleware interval */
class HomeProductItem extends StatelessWidget {
final String title;
final String desc;
final String imgUrl;
HomeProductItem(this.title, this.desc, this.imgUrl);
@override
Widget build(BuildContext context) {
final style1 = TextStyle(fontSize: 30, color: Colors.blue);
final style2 = TextStyle(fontSize: 20, color: Colors.orange);
return Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
border: Border.all(
width: 5,
color: Colors.purple
)
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(title, style: style1,),
SizedBox(height: 8),
Text(desc, style: style2,),
SizedBox(height: 8),
Image.network(imgUrl)
],
),
);
}
}

5、 ... and . Case four Addition and subtraction
- mainAxisAlignment Spindle
- state.widget.params state obtain widget Properties of
import 'package:flutter/material.dart';
import 'package:flutter/rendering.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(),
);
}
}
class HomeBody extends StatefulWidget {
final params = 'params';
@override
State<StatefulWidget> createState() {
return _HomeBodyState();
}
}
//mainAxisAlignment Spindle
//state obtain widget Properties of
class _HomeBodyState extends State<HomeBody> {
var num = 0;
final style1 = TextStyle(fontSize: 20, color: Colors.red);
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(onPressed: (){
setState(() {
num--;
});
},
child: Text('-', style: style1)),
ElevatedButton(onPressed: (){
setState(() {
num++;
});
},
child: Icon(Icons.add)),
],
),
Text(' Current count :$num', style: style1),
Text(this.widget.params)
],
),
);
}
}

6、 ... and . StatefulWidget Life cycle
- StatefulWidget It consists of two classes :StatefulWidget and State

版权声明
本文为[weixin_ forty-two million five hundred and eighty thousand six ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220247503351.html
边栏推荐
- Five cool login pages that can be used
- (进阶用法) C语言字符串函数
- Share price plummeted Robinhood acquired British encryption company for expansion
- Authentication and access control
- 循环链表的创建及可控输出
- 刷题计划——动态规划dynamic programming(二)
- 二叉排序树基本性质详解
- What does defi need to become mainstream?
- Friends don't see how to add, write it down first.
- MySQL8. 0 zero foundation beginner level: from bronze to diamond
猜你喜欢
![[timing] dcrnn: a spatiotemporal prediction network for traffic flow prediction combining diffusion convolution and GNN](/img/65/6bb2892f4aabe47002ada72ed139db.png)
[timing] dcrnn: a spatiotemporal prediction network for traffic flow prediction combining diffusion convolution and GNN

Share price plummeted Robinhood acquired British encryption company for expansion

开发管理·华为IPD

What does defi need to become mainstream?

Training set: alexnet classification

【时序】DCRNN:结合扩散卷积和GNN的用于交通流量预测的时空预测网络

In depth study of MySQL

Install and deploy phpstudy + DVWA vulnerability drill platform

After four years of outsourcing, it was abandoned
![[论文阅读] Active Class Incremental Learning for Imbalanced Datasets](/img/20/ecfffcdeed6813a6bdb703794a2607.png)
[论文阅读] Active Class Incremental Learning for Imbalanced Datasets
随机推荐
class数组的类似常量定义
国何以立
【时序】DCRNN:结合扩散卷积和GNN的用于交通流量预测的时空预测网络
Use of swift generics
JS regular expression -- commonly used by individuals
Openshift enterprise test environment application deployment practice
Modem dial-up playback, originally written in the millionaire, summarized.
C指针和数组深度汇总
Stackoverflow:IActionContextAccessor Is Null
Analysis on the development status of meta universe
Or1k startup file analysis
软件测试·坏味道
Is it safe to open an account online? How to open an account better?
[TIANTI competition] l2-040 Zhezhi game (25 points (s)) (simulation)
(进阶用法) C语言字符串函数
Playing with ABP framework -- translating mastering ABP framework
Use of swift universal types anyobject and any
Dump mangodb data using Navicat
Pv-tsm principle and MATLAB simulation
I'm going to start learning canvas