flutter 系统widget归纳(持续更新)

2021-05-19  本文已影响0人  ProgrammerOz

SafeArea

Scaffold

Scaffold常用属性解析

  1. appBar
    • leading:appBar的左边按钮,默认是返回,如果自定义图片后需要自己设置下返回事件,代码示例:
    • title:标题内容。
    • centerTitle:标题是否居中,默认false,靠左
    • bottom:标题栏下方的View,一般可放个tabbar,以实现多页面切换效果

界面展示效果


image.png

代码示例

appBar: AppBar(
  leading: IconButton(
  icon: LoadAssetImage(imgPath + 'icon_left_black_24px',
    width: 24, height: 24),
    onPressed: () {
      Navigator.maybePop(context);
    },
  ),
  backgroundColor: Colors.white,
  centerTitle: true,
  title: Text(
    'Profile', //LAN
    style: TextStyle(fontSize: 18, color: Colors.black),
    ),
),

  1. bottomNavigationBar
    • items:直接放多个BottomNavigationBarItem子类的条目去实现批量重复布局,类似于原生的radiogroup+radiobutton,配合currentIndex,selectedItemColor等属性去使用
    • shape:可配合child属性,对导航栏上面的界面实现任意布局,相当于原生里用layout等布局去手动实现效果

注:配合floatingActionButton可实现底部按钮凹陷效果,此时需要设置个floatingActionButtonLocation组件,该组件跟floatingActionButton平级


页面展示效果:


image.png

示例代码

bottomNavigationBar: BottomNavigationBar(
  items: const <BottomNavigationBarItem>[
  BottomNavigationBarItem(
    icon: Icon(Icons.home),
    label: 'Home',
    ),
  BottomNavigationBarItem(
    icon: Icon(Icons.business),
    label: 'Business',
    ),
  BottomNavigationBarItem(
    icon: Icon(Icons.school),
    label: 'School',
    ),
  ],
  currentIndex: 0,
  selectedItemColor: Colors.amber[800],
  // onTap: _onItemTapped,
),

页面展示效果:


image.png

示例代码:

bottomNavigationBar: BottomAppBar(
  shape: const CircularNotchedRectangle(),
  child: Row(
    children: [
      IconButton(icon: Icon(Icons.home)),
      SizedBox(),
      IconButton(icon: Icon(Icons.business))
      ],
    mainAxisAlignment: MainAxisAlignment.spaceAround,
  )
),
floatingActionButton: FloatingActionButton(
  child: Icon(Icons.add),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,

Container

Container(
  color: Colours.dark_gray_f1,
  alignment: Alignment.centerLeft,
  padding: EdgeInsets.only(left: 15),
  decoration: new BoxDecoration(
    color: Colors.white,
    borderRadius: BorderRadius.all(Radius.circular(25.0)),
    border: new Border.all(width: 1, color: Colors.red),
  ),
  height: 30,
  child: Text(
    itle,
    style: TextStyle(fontSize: 13, color: Colours.dark_black_666),
  ),
);

GestureDetector

GestureDetector(
  behavior: HitTestBehavior.opaque,
  child: Container(),
  onTap: () {
    //do something
  }
)

Expanded


ListView

ListView.separated(
  itemBuilder: (BuildContext context, int index) =>_buildBankCardItem(cardData[index], controllers[index]),
  eparatorBuilder: (BuildContext context, int index) =>Container(),
  itemCount: cardData == null ? 0 : cardData.length
)

Tabbar

页面效果展示:


image.png

示例代码:

appBar: AppBar(
  flexibleSpace: Container(
  alignment: Alignment.center,
  child: TabBar(
    controller: _tabController,
    labelColor: Colours.purple_color,
    unselectedLabelColor: Colours.black_333,
    indicatorColor: Colours.purple_color,
    labelStyle: TextStyle(fontSize: 16),
    tabs: [
      Tab(
         text:'Deposit',
      ),
      Tab(
        text: 'Transfer',
      )
    ],
    isScrollable: true,
    ),
  ),
  backgroundColor: Colours.white_color,
)

TextField


showCupertinoDialog

页面效果展示:


image.png

示例代码:

showCupertinoDialog(context: widgetContext,builder: (context) {
  return CupertinoAlertDialog(
    title: Text('确认删除'),
    actions: [
      CupertinoDialogAction(
        child: Text('确认'),
        onPressed: () {
          Navigator.of(context).pop();
        },
      ),
      CupertinoDialogAction(
        child: Text('取消'),
        isDestructiveAction: true,
        onPressed: () {
          Navigator.of(context).pop();
        },
      ),
    ],
    );
  },
);

页面效果展示:


image.png

示例代码:

showCupertinoDialog(context: widgetContext,builder: (context) {
  return CupertinoAlertDialog(
    title: Text('请评价'),
    content: Text('\n我们很重视您的评价!'),
    actions: [
      CupertinoDialogAction(
        child: Text('非常好'),
        onPressed: () {
          Navigator.of(context).pop();
        },
      ),
      CupertinoDialogAction(
        child: Text('一般'),
        onPressed: () {
          Navigator.of(context).pop();
        },
      ),
      CupertinoDialogAction(
        child: Text('非常差'),
        isDestructiveAction: true,
        onPressed: () {
          Navigator.of(context).pop();
        },
      ),
    ],
    );
  },
);

showGeneralDialog

showModalBottomSheet

自定义dialog

页面显示效果:


image.png

示例代码:

import 'package:flutter/material.dart';
class LoadingDialog extends Dialog {
  String text;
  LoadingDialog({Key key, @required this.text}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return new Material(
      //创建透明层
      type: MaterialType.transparency, //透明类型
      child: new Center(
        //保证控件居中效果
        child: new SizedBox(
          width: 100.0,
          height: 100.0,
          child: new Container(
            decoration: ShapeDecoration(
              color: Color(0xffffffff),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(
                  Radius.circular(8.0),
                ),
              ),
            ),
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                new CircularProgressIndicator(),
                new Padding(
                  padding: const EdgeInsets.only(
                    top: 20.0,
                  ),
                  child: new Text(
                    text,
                    style: new TextStyle(fontSize: 12.0),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
上一篇 下一篇

猜你喜欢

热点阅读