【Flutter】开发之功能篇(七)

栏目: ASP.NET · 发布时间: 7年前

内容简介:在如果有更进一步的需求,比如说要在启动页加载广告图,并延时几秒的功能,就需要自己实现了。

Android 启动页

android/app/src/main/res/drawable/launch_background.xml 中已经有写好的启动页,只需修改即可

<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white" />

    <!-- You can insert your own image assets here -->
     <item>
        <bitmap
            android:gravity="center"
            android:src="@mipmap/ic_launcher" />
    </item>
</layer-list>
复制代码

如果有更进一步的需求,比如说要在启动页加载广告图,并延时几秒的功能,就需要自己实现了。

思路如下

在main方法中返回自己定义的 SplashWidgit (名字随便起),在其中处理自己的逻辑,包括加载广告图、设置延时等,延时结束后,跳转至真正的主页即可。

状态栏

  • 状态栏颜色

1.使用 MaterialApptheme 属性, theme: ThemeData(primaryColor: Colors.green)请注意 :这个是全局的

【Flutter】开发之功能篇(七)
2.使用 AppBarbackgroundColor 属性, backgroundColor: Colors.deepOrange
【Flutter】开发之功能篇(七)
3.不使用 AppBar ,由于 Flutter 默认是从屏幕最顶部开始布局,所以,修改状态栏颜色简直太容易,只需要通过 MediaQueryData.fromWindow(WidgetsBinding.instance.window).padding.top

取得状态栏高度即可。

Container(
      color: Colors.white,
      child: Column(
        children: <Widget>[
          Container(
            //状态栏高度
            height: MediaQueryData.fromWindow(WidgetsBinding.instance.window)
                .padding
                .top,
            color: Colors.blue,
          ),
          Container(
            height: 200,
            color: Colors.brown,
          ),
        ],
      ),
    );
复制代码
【Flutter】开发之功能篇(七)
甚至你可以修改为渐变色,修改 Containerdecoration

属性

decoration: BoxDecoration(
              gradient: LinearGradient(
            colors: [Colors.cyan, Colors.brown, Colors.deepOrange],
          )),
复制代码
【Flutter】开发之功能篇(七)
  • 字体颜色

使用 AppBarbrightness 属性: Brightness.light 为黑色, Brightness.dark 为白色;

不使用 AppBar 时,可以通过嵌套 AnnotatedRegion<SystemUiOverlayStyle> 来实现。

class StatusDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: AnnotatedRegion<SystemUiOverlayStyle>(
        child: Column(
          children: <Widget>[
            Container(
              height: 200,
              color: Colors.brown,
            ),
          ],
        ),
        value: SystemUiOverlayStyle(
            statusBarColor: Colors.cyan,
            //有Appbar时,会被覆盖
            statusBarIconBrightness: Brightness.light,
            //底部navigationBar背景颜色
            systemNavigationBarColor: Colors.white),
      ),
    );
  }
}
复制代码
  • 渐变色

使用 AppBarflexibleSpace ,其中渐变方式还可选择 RadialGradientSweepGradient

appBar: AppBar(
        title: Text('DemoPage'),
        flexibleSpace: Container(
          decoration: BoxDecoration(
              gradient: LinearGradient(
            colors: [Colors.cyan, Colors.brown, Colors.deepOrange],
          )),
        ),
      )
复制代码
【Flutter】开发之功能篇(七)

返回键监听

可以 WillPopScope 嵌套,可以用于监听处理返回键的逻辑。其实 WillPopScope 并不是监听返回按键,是当前页面将要被 pop 时触发的回调。

class _StatusDemoState extends State<StatusDemo> {
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: StatusWidget(),
      onWillPop: () {
        ///如果返回 return new Future.value(false); popped 就不会被处理
        ///如果返回 return new Future.value(true); popped 就会触发
        ///这里可以通过 showDialog 弹出确定框,在返回时通过 Navigator.of(context).pop(true);决定是否退出
        return showExitDialog(context);
      },
    );
  }

  Future<bool> showExitDialog(BuildContext context) {
    return showDialog(
        context: context,
        builder: (BuildContext content) {
          return AlertDialog(
            title: Text("提示"),
            content: Text("确认退出吗?"),
            actions: <Widget>[
              GestureDetector(
                child: Container(
                  child: Text('退出'),
                ),
                onTap: () {
                  Navigator.of(context).pop(true);
                },
              ),
              GestureDetector(
                child: Container(
                  child: Text('取消'),
                ),
                onTap: () {
                  Navigator.of(context).pop();
                },
              ),
            ],
          );
        });
  }
}
复制代码

选取图片

使用的是 Flutter 提供的 image_picker

首先,在pubspec.yaml文件中添加依赖 image_picker: '0.6.0+8'

class TakeDemo extends StatefulWidget {
  @override
  _TakeDemoState createState() => _TakeDemoState();
}

class _TakeDemoState extends State<TakeDemo> {
  File _image;

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.only(top: 20),
      alignment: AlignmentDirectional.center,
      child: Column(
        children: <Widget>[
          FlatButton(
            onPressed: () {
              getImage();
            },
            child: Text('从相册选择'),
          ),
          Center(
            child: _image == null
                ? Text('No image selected.',style: TextStyle(fontSize: 14),)
                : Image.file(_image),
          ),
        ],
      ),
    );
  }

  Future getImage() async {
    //gallery相册,camera拍照
    var image = await ImagePicker.pickImage(source: ImageSource.gallery);

    setState(() {
      _image = image;
    });
  }
}
复制代码
【Flutter】开发之功能篇(七)

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Algorithms to Live By

Algorithms to Live By

Brian Christian、Tom Griffiths / Henry Holt and Co. / 2016-4-19 / USD 30.00

A fascinating exploration of how insights from computer algorithms can be applied to our everyday lives, helping to solve common decision-making problems and illuminate the workings of the human mind ......一起来看看 《Algorithms to Live By》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具