Flutter 学习之旅

本文只针对个人学习所遇问题,以及解决方案进行记录,不深刨原理。

不深刨原理是因为我也才开始学习,讲不明白,有可能还误导大家 ,希望多多包涵。

问题一:

如何通过appBar去设置状态栏字体颜色以及状态栏透明?

解决方法:

1、通过 backgroundColor: Colors.transparent设置透明,以及对应的build函数设置extendBodyBehindAppBar: true

Dart 复制代码
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        extendBodyBehindAppBar: true,
        backgroundColor: Colors.transparent,
        appBar: null,
        body: Container(child: Row(children: [])));
  }

2、通过appBar的systemOverlayStyle属性,配置SystemUiOverlayStyle相关属性

Dart 复制代码
AppBar(
        backgroundColor: Colors.transparent,
        systemOverlayStyle: const SystemUiOverlayStyle(
          statusBarColor: Colors.transparent,
          statusBarIconBrightness: Brightness.dark,
          systemNavigationBarColor: Colors.white,
          systemNavigationBarDividerColor: Colors.transparent,
          systemNavigationBarIconBrightness: Brightness.dark,
        ),
        title: Container(
            margin: EdgeInsets.only(left: 15.r, right: 15.r, top: 15.r),
            child: Row(children: [])));
问题二:

如何解决SingleChildScrollView滑动会影响appBar背景颜色?

解决方法:

通过NotificationListener来阻止SingleChildScrollView的滑动事件在向上传递

Dart 复制代码
Scaffold(
        extendBodyBehindAppBar: true,
        backgroundColor: Colors.transparent,
        body: /*通过NotificationListener来阻止SingleChildScrollView的滑动事件在向上传递*/
            NotificationListener<ScrollNotification>(
                onNotification: (ScrollNotification notification) {
                  // 返回true来阻止事件向上传递
                  return true;
                },
                child: SingleChildScrollView(child: Column(children: [
                  
                ]))));
问题三:

如何解决SingleChildScrollView嵌套GridView报错?

解决方法:

这个报错主要是因为GridView高度原因,给GridView在包裹一层Container(容器),设置高度即可

Dart 复制代码
Container(
        margin: EdgeInsets.only(left: 0.r, right: 0.r, top: 0.r),
        height: 75 * (iconList.length / 2).h,
        child: GridView.builder(
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2,
                crossAxisSpacing: 10,
                mainAxisSpacing: 10,
                childAspectRatio: 176 / 65),
            //宽高比
            itemCount: iconList.length,
            itemBuilder: (BuildContext context, int index) {
              return Container(
                  margin: EdgeInsets.only(left: 8.r, right: 8.r, top: 5.r),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(12.r)),
                      color: Colors.white),
                  child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: []));
            }));
问题四:

如何解决SingleChildScrollView嵌套GridView滑动冲突问题?

解决方法:

SingleChildScrollView嵌套GridView滑动冲突主要是应为两个组件都是可滑动的,解决方式是禁止GridView滑动,设置physics: const NeverScrollableScrollPhysics(), shrinkWrap: true属性即可。

Dart 复制代码
GridView.builder(
            physics: const NeverScrollableScrollPhysics(),
            shrinkWrap: true,
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2,
                crossAxisSpacing: 10,
                mainAxisSpacing: 10,
                childAspectRatio: 176 / 65),
            //宽高比
            itemCount: iconList.length,
            itemBuilder: (BuildContext context, int index) {
              return Container(
                  margin: EdgeInsets.only(left: 8.r, right: 8.r, top: 5.r),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(12.r)),
                      color: Colors.white),
                  child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: []));
            });

问题五:

通过GestureDetector获取Container的点击事件,使用const Expanded(child: SizedBox())区域点击无效?

解决方法:

设置Container的decoration属性BoxDecoration()即可。

Dart 复制代码
GestureDetector(
        onTap: onTap,
        child: Container(
            width: double.infinity,
            decoration: const BoxDecoration(),
            height: 50.h,
            child: Row(mainAxisAlignment: MainAxisAlignment.start, children: [
              Image.asset("assets/images/${imageName}.png", width: 20.r, height: 20.r),
              SizedBox(width: 15.w),
              Text(name, style: TextStyle(color: Colors.black, fontSize: 14.sp)),
              const Expanded(child: SizedBox()),
              Image.asset("assets/images/rightward.png", width: 24.w, height: 14.h)
            ])));

此博客会持续更新,大家有什么问题或者有更好的解决办法,可留言沟通。

相关推荐
其实防守也摸鱼16 分钟前
渗透--损坏的对象级别鉴权漏洞(Broken Object Level Authorization, BOLA)
大数据·网络·数据库·学习·tcp/ip·安全·安全威胁分析
铅笔侠_小龙虾42 分钟前
Rust 学习(2)-变量、常量与 shadowing
学习·算法·rust
it小生01011 小时前
从底层思维到高效行动,顶尖人才进阶核心修炼指南
学习·学习方法
一枚菜鸟_1 小时前
Flutter + Cursor 一天出 App?这 3 个坑让开发者多花了 3 小时
flutter·cursor
恣逍信点1 小时前
《凌微经》通俗解读——哲学第一因
学习·职场和发展·创业创新·学习方法·业界资讯·交友·哲学
red_redemption2 小时前
自由学习记录(207)
学习
AOwhisky2 小时前
Python 学习笔记(第四期)——字符串:格式化、操作与文本处理——核心知识点自测与详解
开发语言·笔记·python·学习·列表·元组·字典
光学补偿2 小时前
微服务学习 --- Spring Cloud概述
学习·spring cloud·微服务
AOwhisky3 小时前
Python 学习笔记(第六期)——函数:定义、参数传递与作用域
笔记·python·学习·云原生·运维开发·函数·参数传递
网小鱼的学习笔记3 小时前
Agent学习001
学习·迁移学习