Flutter05-基础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(),
      );
  }
}

一.Text

  1. 普通文本 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
      ),
    );
  }
}
  1. 富文本 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 按钮组
  1. 创建基础视图
/**
 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'),
        ),

      ],
    );
  }
}

在这里插入图片描述

  1. 升级视图

/**
 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

  1. 基础
/**
 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,);
  1. 圆角头像
  • CircleAvatar
  • ClipOval
  • Container+BoxDecoration
  1. 圆角图片
  • ClipRRect
  • Container+BoxDecoration

四.Textfield

  1. 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'),
            
          ),
        ],
    );

  }

}

请添加图片描述

  1. 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(),
      );
  }
}
  1. 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,
    );
  }
}
  1. 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
        ),
      )
    );
  }
}
  1. 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,),
      )
    );
  }
}
  1. 阴影圆角图片
 //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
            ]
          )
        ),

      )
    );

*/

  }
}
  1. 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,//弹性系数
        )
      ],
    );
  }
}

  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,//弹性系数
        )
      ],
    );
  }
}
  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,
        )
      ],
    );
  }
}

在这里插入图片描述

六.滚动Widget

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值