Flutter(Widget)基本组件
2022-07-05 本文已影响0人
aofeilin
Center Text MaterialApp组件 scaffold组件 Container组件 Image组件
![](https://img.haomeiwen.com/i636362/6bbcae48e424a0a9.png)
1.Center 水平垂直居中
![](https://img.haomeiwen.com/i636362/fa5a32ffe917b909.png)
class CenterTextWidget extends StatefulWidget {
@override
_CenterTextWidgetState createState() => _CenterTextWidgetState();
}
class _CenterTextWidgetState extends State<CenterTextWidget> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center( //上下垂直居中
child: Text('测试文字'),
),
);
}
}
2.AppBar
![](https://img.haomeiwen.com/i636362/7c235fddae329164.png)
import 'dart:ui';
import 'package:flutter/material.dart';
class AppBarWidget extends StatefulWidget {
@override
_AppBarWidgetState createState() => _AppBarWidgetState();
}
class _AppBarWidgetState extends State<AppBarWidget> {
@override
Widget build(BuildContext context) {
// return Scaffold(
// appBar: AppBar(
// title: Text('标题'),
// ),
// body: Container(
// ),
// );
return bgAppBar();
}
bgAppBar() {
String appbarBgAssetsPath = 'assets/images/appbar_normal_bg.png';
String backAssetsPath = 'assets/images/arrow_left.png';
double statusHeight = MediaQueryData.fromWindow(window).padding.top;
return Scaffold(
body: Container(
child: Column(
children: [
Container(
width: MediaQuery.of(context).size.width,
height: MediaQueryData.fromWindow(window).padding.top +48,
alignment: Alignment.center,
decoration: BoxDecoration(
image: DecorationImage(fit: BoxFit.cover, image: AssetImage(appbarBgAssetsPath))),
child: AppBar(
elevation: 0,
toolbarOpacity: 0.0,
leading: InkWell(
child: Container(
child:Image.asset(
backAssetsPath,
width: 20,
height: 20,
),
width: 40,
alignment: Alignment.center,
),
onTap: () {
}),
backgroundColor: Colors.transparent,
title: Text(
'导航条',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 20, color: Colors.white),
),
),
),
],
),
),
);
}
}
3.Container 容器 Text Widget
![](https://img.haomeiwen.com/i636362/79261d11aa5f6146.png)
class ZFLContainerWidget extends StatefulWidget {
@override
_ZFLContainerWidgetState createState() => _ZFLContainerWidgetState();
}
class _ZFLContainerWidgetState extends State<ZFLContainerWidget> {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
width: 300,
height: 200,
decoration: BoxDecoration(
color: Colors.yellow,
borderRadius: BorderRadius.circular(10),
),
alignment: Alignment.center,
child: Text(
'ahkfhkhfkdshfh',
style: TextStyle(
fontSize: 16.0,
color: Colors.green,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic,
decoration: TextDecoration.underline,
decorationColor: Colors.red,
decorationStyle: TextDecorationStyle.dashed,
letterSpacing: 5.0),
),
),
);
}
}
4.图片widget
![](https://img.haomeiwen.com/i636362/2d15943c1bf1a840.png)
class ZFLImageWidget extends StatefulWidget {
@override
_ZFLImageWidgetState createState() => _ZFLImageWidgetState();
}
class _ZFLImageWidgetState extends State<ZFLImageWidget> {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
width: 300,
height: 200,
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(10),
image: DecorationImage(
fit: BoxFit.fill,
image: NetworkImage(
"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202111%2F20%2F20211120112619_16cad.thumb.1000_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1659579107&t=51e2cc7eb2ac5f851728893c95009e65"))),
child: ClipOval(
child: Image.asset(
'assets/images/version_logo.png',
width: 100,
height: 100,
fit: BoxFit.fill,
),
// child: Image.network(
// "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202111%2F20%2F20211120112619_16cad.thumb.1000_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1659579107&t=51e2cc7eb2ac5f851728893c95009e65",
// width: 100,
// height: 100,
// fit: BoxFit.fill,
// ),
)));
}
}