IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> flutter学习之基础组件(六) -> 正文阅读

[移动开发]flutter学习之基础组件(六)

flutter学习之基础组件(六)

本次主要学习一些Flutter中的常见的按钮组件以及自定义按钮组件


一、Flutter中的按钮组件介绍

Flutter里有很多的Button 组件很多,参考下表
在这里插入图片描述
1.0和2.0的区别就是,1.0的好的属性需要在外部配置,2.0都是放在style里面去配置了

二、各种按钮使用

效果:在这里插入图片描述
相关代码:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_view_demo/myProject/main/CurrentTab.dart';

class TestFifteen extends StatefulWidget {
  const TestFifteen({Key? key}) : super(key: key);

  @override
  _TestFifteenState createState() => _TestFifteenState();
}

class _TestFifteenState extends State<TestFifteen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("按钮组件"),
        leading: Icon(Icons.arrow_back_ios),
        actions: [IconButton(onPressed: () {}, icon: Icon(Icons.settings))],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.of(context).pushAndRemoveUntil(
              new MaterialPageRoute(builder: (context) => new CurrentTab(currentIndex:0)),
                  (route) => route == null);  //所有路由都置为空,在跳转到指定页面
        },
        child: Icon(Icons.add,color: Colors.black,),
        backgroundColor: Colors.yellow,
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat, //设置floatingActionButton位置
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              RaisedButton(
                onPressed: () {},
                color: Colors.blue,
                textColor: Colors.white,
                child: Text("RaisedButton按钮"),
              ),
              SizedBox(width: 20),
              ElevatedButton(
                onPressed: () {},
                child: Text("ElevatedButton按钮"),
              )
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Container(
                width: 150,
                height: 40,
                child: RaisedButton(
                  onPressed: () {},
                  color: Colors.blue,
                  textColor: Colors.white,
                  elevation: 5,
                  child: Text("设置按钮宽高阴影"),
                ),
              ),
              SizedBox(
                width: 10,
              ),
              RaisedButton.icon(
                  onPressed: () {},
                  icon: Icon(Icons.add),
                  label: Text("带图标按钮")),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Expanded(
                  child: Container(
                margin: EdgeInsets.all(5),
                height: 40,
                child: RaisedButton(
                  onPressed: () {},
                  color: Colors.blue,
                  textColor: Colors.white,
                  elevation: 5,
                  child: Text("自适应按钮"),
                ),
              )),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              RaisedButton(
                onPressed: () {},
                color: Colors.blue,
                textColor: Colors.white,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10)),
                child: Text("圆角按钮"),
              ),
              Container(
                width: 80,
                height: 80,
                child: RaisedButton(
                  onPressed: () {},
                  color: Colors.blue,
                  textColor: Colors.white,
                  splashColor: Colors.red,
                  //长按水波纹颜色
                  shape: CircleBorder(side: BorderSide(color: Colors.blue)),
                  child: Text("圆型按钮"),
                ),
              ),
              FlatButton(
                onPressed: () {},
                child: Text("FlatButton"),
                color: Colors.red,
                textColor: Colors.white,
              )
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              OutlineButton(onPressed: () {}, child: Text("OutlineButton")),
              MyBotton(
                width: 100.0,
                height: 40.0,
                pressed: () {},
                text: "自定义按钮",
              )
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ButtonBar(
                children: [
                  ElevatedButton(
                    onPressed: () {},
                    child: Text("ButtonBar1"),
                  ),
                  ElevatedButton(
                    onPressed: () {},
                    child: Text("ButtonBar2"),
                  )
                ],
              )
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: () {},
                child: Text("ElevatedButton配置"),
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Colors.red),
                  foregroundColor: MaterialStateProperty.all(Colors.yellow),
                ),
              ),
              ElevatedButton.icon(
                  onPressed: () {},
                  icon: Icon(Icons.print),
                  label: Text("ElevatedButton"))
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              TextButton(onPressed: () {}, child: Text("TextButton")),
              OutlinedButton(
                onPressed: () {},
                child: Text("OutlinedButton"),
                style: ButtonStyle(
                    foregroundColor: MaterialStateProperty.all(Colors.black),
                    side: MaterialStateProperty.all(
                        BorderSide(color: Colors.red, width: 1))),
              ),
            ],
          ),

        ],
      ),
    );
  }
}

//自定义按钮组件
class MyBotton extends StatelessWidget {
  final width;
  final height;
  final pressed;
  final text;

  const MyBotton(
      {this.width = 80.0,
      this.height = 30.0,
      this.pressed = null,
      this.text = ""})
      : super();

  @override
  Widget build(BuildContext context) {
    return Container(
      width: this.width,
      height: this.height,
      child: ElevatedButton(
        onPressed: this.pressed,
        child: Text(this.text),
      ),
    );
  }
}

三、FloatingActionButton实现底部中间bottomNavigationBar

效果图:
在这里插入图片描述

在Scaffold中创建floatingActionButton,并且设置floatingActionButton的位置,调整盖到中间bottomNavigationBar的item的图标,在设置点击floatingActionButton的时候,调用刷新页面属性


class _CurrentTabState extends State<CurrentTab> {
  int _currentIndex = 0;
  _CurrentTabState(currentIndex){
    this._currentIndex = currentIndex;
  }

  List<Widget> _pageList = [
    HomePage(),
    AddPage(),
    MyPage()
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // appBar: AppBar(
      //   title: Text("flutter学习"),
      // ),
      floatingActionButton: Container(
   //     margin: EdgeInsets.only(top: 10),   //修改上边距
        height: 70,
        width: 70,
        padding: EdgeInsets.all(8),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(40),
          color: Colors.white
        ),
        child: FloatingActionButton(
          onPressed: () {
            setState(() {
              this._currentIndex = 1;
            });
          },
          child: Icon(Icons.add,color: this._currentIndex == 1 ? Colors.white:Colors.black,size: 28,),
          backgroundColor: this._currentIndex == 1 ? Colors.red:Colors.yellow,
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      body: _pageList[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (int index){
          setState(() {
            _currentIndex = index;
          });
        },
        fixedColor: Colors.pinkAccent[100],
        type: BottomNavigationBarType.fixed,
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), title: Text("首页")),
          BottomNavigationBarItem(icon: Icon(Icons.home), title: Text("添加")),
          BottomNavigationBarItem(icon: Icon(Icons.home), title: Text("我的")),
        ],
      ),
    );
  }
}


总结

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-07-28 07:56:17  更:2021-07-28 07:58:00 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/2 22:32:57-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码