当前位置:网站首页>Issue 99: flutter learning (2)
Issue 99: flutter learning (2)
2022-07-18 03:09:00 【terrence386】
Continue today flutter Some related knowledge , It mainly includes the following contents .
- State component
- Send a request dio
- The screen size fits
State component
flutter State component concept and react Basically the same , The component maintains its own state , And also by calling setState Make status changes .
We use vs code You can use shortcut commands to generate state components , Then change it to the component name we need .
The sample code is as follows :
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
var userInfo = '';
// initState
@override
void initState(){
getUserInfo().then((value) {
print('user-info=====>$value');
setState(() {
userInfo:value;
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('T-shop'),
),
body: Container(
child: Center(
child: Text('terrence'),
),
)
);
}
}
HomePage In the state component , Defined userInfo Variable , When we use getUserInfo Method to obtain user information , Through setState Method to assign it , So as to achieve the purpose of modifying the state .
Send a request
and web Same end development ,flutter The development of the project also needs to use the corresponding request Library , At present, the most commonly used is dio library .
dio Is a powerful Dart Http Request Library , Support Restful API、FormData、 Interceptor 、 Request cancellation 、Cookie management 、 Upload files / download 、 Overtime 、 Custom adapter, etc ...
Its usage is also very simple , Basically write with us web The development of the end is consistent .
The simple usage is as follows :
import 'package:dio/dio.dart';
void getHttp() async {
try {
var response = await Dio().get('http://www.google.com');
print(response);
} catch (e) {
print(e);
}
}
Introduce the corresponding package , Defined function , Then send the corresponding request .
For example, we can define a method to obtain user information getUserInfo:
// service_methods.dart
// Get user information
Future getUserInfo() async {
try{
print('get-userInfo--->');
Response response;
Dio dio = new Dio();
dio.options.contentType = Headers.jsonContentType;
response = await dio.post( servicePath['homePageContent']??'', data:{} );
if(response.statusCode == 200){
return response.data;
}else{
throw Exception('service error');
}
}catch(e){
return print(e);
}
}
When we use , and promise The usage of is basically the same . First, import the corresponding file package , And then call the method , stay then Method to receive the return value for processing .
// Import the corresponding package
import '../service/service_methods.dart';
// Call methods to process
@override
void initState(){
getUserInfo().then((value) {
print('user-info=====>$value');
setState(() {
userInfo:value;
});
});
super.initState();
}
Another thing to note is , The data returned by the interface may be a json character string , meanwhile dart and js Language is similar to , But it's not the same , It doesn't work . The point operator directly obtains the attributes of the object , We need to do some data format conversion , Or use it directly dart Linguistic Map The operation method of the data structure is to take values .
The screen size fits
The screen size configuration requires related dependent packages :flutter_screenutil.
We can do it in github I found this bag on , It is also essentially a component , It's easy to use .
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
// Set dimensions ( Fill in the screen size of the equipment in the design ) If the design is based on 360dp * 690dp The screen of
ScreenUtil.init(
BoxConstraints(
maxWidth: MediaQuery.of(context).size.width,
maxHeight: MediaQuery.of(context).size.height),
designSize: Size(360, 690),
context: context,
minTextAdapt: true,
orientation: Orientation.portrait);
return Scaffold();
}
}
It has some common properties , You can see :
attribute | type | The default value is | describe |
|---|---|---|---|
designSize | Size | Size(360, 690) | Dimensions of equipment in the design draft ( Unit random , Suggest dp, But it must be consistent in the process of use ) |
deviceSize | Size | null | The size of the physical device |
builder | Widget Function() | Container() | Generally return to a MaterialApp Type of Function() |
orientation | Orientation | portrait | Screen orientation |
splitScreenMode | bool | false | Support split screen size |
minTextAdapt | bool | false | According to the width / The minimum value in height fits the text |
context | BuildContext | null | Pass in context It will be more sensitive to change according to the screen changes |
child | Widget | null | builder Part of , The library is not used for its dependency properties |
rebuildFactor | Function | default | Return to whether to rebuild when the screen indicator changes . |
边栏推荐
- WebView loading URL prompt net:: error_ UNKNOWN_ URL_ SCHEME
- Web3离爆发还差什么?
- Fuxin software appeared at the 2022 national chemical enterprise digital intelligence transformation and Development Forum
- Hcip notes (2)
- 同花顺开户选哪个证券公司好?网上开户安全吗?
- Envoyez votre code dans l'espace et développez "le plus grand travail" avec Huawei Cloud
- 斗地主包含抢地主环节
- Differences and relations among RDD, dataframe and dataset in spark
- Get started with pyGame
- Listbox
猜你喜欢

10000 words detailed SSH (SSH login principle +ssh configuration + simulated SSH secret free login)

Tencent celebrities share | Tencent alluxio (DOP) landing and Optimization Practice in the financial scene

【流放之路-第一章】

使用 Terraform 在阿里云上快速部署 MQTT 集群

Interface development does not need to write controller, service, Dao, mapper, XML, VO, which is automatically generated!

Why is it difficult for girls to learn programming? Is it the wrong way of thinking or the wrong way of learning?

Wechat applet uni app uses uni Getuserprofile to realize wechat authorized login (with source code)

【MySQL】多表查询

Zhihu Gaozan: Data Center -- Alibaba, Daas

RobotFramework进阶(一)集成Pycharm及UI自动化用例编写
随机推荐
Detailed explanation of thread interrupt method
【详解】分页方式:Page Helper的使用
Introduction to esp8266 Lua (II)
Google proposes the reliability machine learning model plex: let the large model put forward appropriate prediction results for out of distribution data
【MySQL】多表查询
欧拉Talk | 开发者社区体验坦白局7月14日19:30约起
Hcip notes (2)
ClickPaaS马俊:模型驱动的低代码平台实践
51nod 1278 相离的圆
字符串拼接
“代码写的越急,程序跑的越慢”
Solve the comprehensive monitoring scheme for the dynamic ring state of small and medium-sized machine rooms
Listbox
js实现电子签名功能
什么是主动元数据?为什么Gartner预测它是元数据管理的新方向
Issue 90: the problem of a small program generating posters
To realize the remote direct electromagnetic communication of consciousness, the Dongda team, together with the National University of Singapore and others, is expected to build an electromagnetic bra
RS485接线 | Modbus智能LED三色声光报警器 机房警示灯带蜂鸣器
Shanghai port shipping AI container AI Unicorn gathers flying pupils, and the market share of port and shipping AI products is leading, driving the development of the whole industrial chain of port an
下载完PyQt5,发现找不到designer.exe问题解决方案