老孟导读:今天分享StackOverflow上高访问量的20大问题,这些问题给我一种特别熟悉的感觉,我想你一定或多或少的遇到过,有的问题在stackoverflow上有几十万的阅读量,说明很多人都遇到了这些问题,把这些问题整理分享给大家,每期20个,每隔2周分享一次。

如何实现Android平台的wrap_content 和match_parent

你可以按照如下方式实现:

1、Width = Wrap_content Height=Wrap_content:

Wrap(
children: <Widget>[your_child])

2、Width = Match_parent Height=Match_parent:

Container(
height: double.infinity,
width: double.infinity,child:your_child)

3、Width = Match_parent ,Height = Wrap_conten:

Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[*your_child*],
);

4、Width = Wrap_content ,Height = Match_parent:

Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[your_child],
);

如何避免FutureBuilder频繁执行future方法

错误用法:

@override
Widget build(BuildContext context) {
return FutureBuilder(
future: httpCall(),
builder: (context, snapshot) { },
);
}

正确用法:

class _ExampleState extends State<Example> {
Future<int> future; @override
void initState() {
future = Future.value(42);
super.initState();
} @override
Widget build(BuildContext context) {
return FutureBuilder(
future: future,
builder: (context, snapshot) { },
);
}
}

底部导航切换导致重建问题

在使用底部导航时经常会使用如下写法:

Widget _currentBody;

@override
Widget build(BuildContext context) {
return Scaffold(
body: _currentBody,
bottomNavigationBar: BottomNavigationBar(
items: <BottomNavigationBarItem>[
...
],
onTap: (index) {
_bottomNavigationChange(index);
},
),
);
} _bottomNavigationChange(int index) {
switch (index) {
case 0:
_currentBody = OnePage();
break;
case 1:
_currentBody = TwoPage();
break;
case 2:
_currentBody = ThreePage();
break;
}
setState(() {});
}

此用法导致每次切换时都会重建页面。

解决办法,使用IndexedStack

int _currIndex;

@override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: _currIndex,
children: <Widget>[OnePage(), TwoPage(), ThreePage()],
),
bottomNavigationBar: BottomNavigationBar(
items: <BottomNavigationBarItem>[
...
],
onTap: (index) {
_bottomNavigationChange(index);
},
),
);
} _bottomNavigationChange(int index) {
setState(() {
_currIndex = index;
});
}

TabBar切换导致重建(build)问题

通常情况下,使用TabBarView如下:

TabBarView(
controller: this._tabController,
children: <Widget>[
_buildTabView1(),
_buildTabView2(),
],
)

此时切换tab时,页面会重建,解决方法设置PageStorageKey

var _newsKey = PageStorageKey('news');
var _technologyKey = PageStorageKey('technology'); TabBarView(
controller: this._tabController,
children: <Widget>[
_buildTabView1(_newsKey),
_buildTabView2(_technologyKey),
],
)

Stack 子组件设置了宽高不起作用

在Stack中设置100x100红色盒子,如下:

Center(
child: Container(
height: 300,
width: 300,
color: Colors.blue,
child: Stack(
children: <Widget>[
Positioned.fill(
child: Container(
height: 100,
width: 100,
color: Colors.red,
),
)
],
),
),
)

此时红色盒子充满父组件,解决办法,给红色盒子组件包裹Center、Align或者UnconstrainedBox,代码如下:

Positioned.fill(
child: Align(
child: Container(
height: 100,
width: 100,
color: Colors.red,
),
),
)

如何在State类中获取StatefulWidget控件的属性

class Test extends StatefulWidget {
Test({this.data});
final int data;
@override
State<StatefulWidget> createState() => _TestState();
} class _TestState extends State<Test>{ }

如下,如何在_TestState获取到Test的data数据呢:

  1. 在_TestState也定义同样的参数,此方式比较麻烦,不推荐。
  2. 直接使用widget.data(推荐)。

default value of optional parameter must be constant

上面的异常在类构造函数的时候会经常遇见,如下面的代码就会出现此异常:

class BarrageItem extends StatefulWidget {
BarrageItem(
{ this.text,
this.duration = Duration(seconds: 3)});

异常信息提示:可选参数必须为常量,修改如下:

const Duration _kDuration = Duration(seconds: 3);

class BarrageItem extends StatefulWidget {
BarrageItem(
{this.text,
this.duration = _kDuration});

定义一个常量,Dart中常量通常使用k开头,_表示私有,只能在当前包内使用,别问我为什么如此命名,问就是源代码中就是如此命名的。

如何移除debug模式下右上角“DEBUG”标识

MaterialApp(
debugShowCheckedModeBanner: false
)

如何使用16进制的颜色值

下面的用法是无法显示颜色的:

Color(0xb74093)

因为Color的构造函数是ARGB,所以需要加上透明度,正确用法:

Color(0xFFb74093)

FF表示完全不透明。

如何改变应用程序的icon和名称

链接:https://blog.csdn.net/mengks1987/article/details/95306508

如何给TextField设置初始值

class _FooState extends State<Foo> {
TextEditingController _controller; @override
void initState() {
super.initState();
_controller = new TextEditingController(text: '初始值');
} @override
Widget build(BuildContext context) {
return TextField(
controller: _controller,
);
}
}

Scaffold.of() called with a context that does not contain a Scaffold

Scaffold.of()中的context没有包含在Scaffold中,如下代码就会报此异常:

class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('老孟'),
),
body: Center(
child: RaisedButton(
color: Colors.pink,
textColor: Colors.white,
onPressed: _displaySnackBar(context),
child: Text('show SnackBar'),
),
),
);
}
} _displaySnackBar(BuildContext context) {
final snackBar = SnackBar(content: Text('老孟'));
Scaffold.of(context).showSnackBar(snackBar);
}

注意此时的context是HomePage的,HomePage并没有包含在Scaffold中,所以并不是调用在Scaffold中就可以,而是看context,修改如下:

_scaffoldKey.currentState.showSnackBar(snackbar);

或者:

Scaffold(
appBar: AppBar(
title: Text('老孟'),
),
body: Builder(
builder: (context) =>
Center(
child: RaisedButton(
color: Colors.pink,
textColor: Colors.white,
onPressed: () => _displaySnackBar(context),
child: Text('老孟'),
),
),
),
);

Waiting for another flutter command to release the startup lock

在执行flutter命令时经常遇到上面的问题,

解决办法一:

1、Mac或者Linux在终端执行如下命令:

killall -9 dart

2、Window执行如下命令:

taskkill /F /IM dart.exe

解决办法二:

删除flutter SDK的目录下/bin/cache/lockfile文件。

无法调用setState

不能在StatelessWidget控件中调用了,需要在StatefulWidget中调用。

设置当前控件大小为父控件大小的百分比

1、使用FractionallySizedBox控件

2、获取父控件的大小并乘以百分比:

MediaQuery.of(context).size.width * 0.5

Row直接包裹TextField异常:BoxConstraints forces an infinite width

解决方法:

Row(
children: <Widget>[
Flexible(
child: new TextField(),
),
],
),

TextField 动态获取焦点和失去焦点

获取焦点:

FocusScope.of(context).requestFocus(_focusNode);

_focusNode为TextField的focusNode:

_focusNode = FocusNode();

TextField(
focusNode: _focusNode,
...
)

失去焦点:

_focusNode.unfocus();

如何判断当前平台

import 'dart:io' show Platform;

if (Platform.isAndroid) {
// Android-specific code
} else if (Platform.isIOS) {
// iOS-specific code
}

平台类型包括:

Platform.isAndroid
Platform.isFuchsia
Platform.isIOS
Platform.isLinux
Platform.isMacOS
Platform.isWindows

Android无法访问http

其实这本身不是Flutter的问题,但在开发中经常遇到,在Android Pie版本及以上和IOS 系统上默认禁止访问http,主要是为了安全考虑。

Android解决办法:

./android/app/src/main/AndroidManifest.xml配置文件中application标签里面设置networkSecurityConfig属性:

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
<application android:networkSecurityConfig="@xml/network_security_config">
<!-- ... -->
</application>
</manifest>

./android/app/src/main/res目录下创建xml文件夹(已存在不用创建),在xml文件夹下创建network_security_config.xml文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>

IOS无法访问http

./ios/Runner/Info.plist文件中添加如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
...
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</dict>
</plist>

交流

Github地址:https://github.com/781238222/flutter-do

170+组件详细用法:http://laomengit.com

如果你对Flutter还有疑问或者技术方面的疑惑,欢迎加入Flutter交流群(微信:laomengit)。

同时也欢迎关注我的Flutter公众号【老孟程序员】,公众号首发Flutter的相关内容。

Flutter生态建设离不开你我他,需要大家共同的努力,点赞也是其中的一种,如果文章帮助到了你,希望点个赞。

超过百万的StackOverflow Flutter 问题的更多相关文章

  1. 超过百万的StackOverflow Flutter 问题-第二期

    老孟导读:一个月前分享的<超过百万的StackOverflow Flutter 问题-第一期>受到很多朋友的喜欢,非常感谢大家的支持,在文章末尾有第一期的链接,希望此文能对你有所帮助. N ...

  2. KAFKA:如何做到1秒发布百万级条消息

    http://rdcqii.hundsun.com/portal/article/709.html KAFKA是分布式发布-订阅消息系统,是一个分布式的,可划分的,冗余备份的持久性的日志服务.它主要用 ...

  3. [转帖]“腾百万”之后,腾讯的云操作系统VStation单集群调度达10万台

    “腾百万”之后,腾讯的云操作系统VStation单集群调度达10万台 https://www.leiphone.com/news/201909/4BsKCJtvvUCEb66c.html 腾讯有超过1 ...

  4. 【老孟Flutter】Flutter 2.0 重磅更新

    老孟导读:昨天期待已久的 Flutter 2.0 终于发布了,Web 端终于提正了,春季期间我发布的一篇文章,其中的一个预测就是 Web 正式发布,已经实现了,还有一个预测是:2021年将是 Flut ...

  5. 无限可能 | Flutter 2 重点更新一览

    我们非常高兴在本周发布了 Flutter 2.自 Flutter 1.0 发布至今已有两年多的时间,在如此短暂的时间内,我们解决了 24,541 个 issue,合并了来自 765 个贡献者的 17, ...

  6. 【老孟Flutter】Flutter 2的新功能

    老孟导读:昨天期待已久的 Flutter 2.0 终于发布了, Flutter Web和Null安全性趋于稳定,Flutter桌面安全性逐渐转向Beta版! 原文链接:https://medium.c ...

  7. Flutter 2.8 更新详解

    北半球的冬意已至,黄叶与气温均随风而落.年终的最后一个 Flutter 稳定版本 已悄然来到你的面前.让我们向 Flutter 2.8 打声招呼- 本次更新包含了 207 位贡献者和 178 位审核者 ...

  8. mysql 数据表备份导出,恢复导入操作实践

    因为经常跑脚本的关系, 每次跑完数据之后,相关的测试服数据库表的数据都被跑乱了,重新跑脚本恢复回来速度也不快,所以尝试在跑脚本之前直接备份该表,然后跑完数据之后恢复的方式,应该会方便一点.所以实践一波 ...

  9. Storm介绍(一)

    作者:Jack47 PS:如果喜欢我写的文章,欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 内容简介 本文是Storm系列之一,介绍了Storm的起源,Storm ...

随机推荐

  1. 【Weiss】【第03章】练习3.26:双端队列

    [练习3.26] 双端队列(deque)是由一些项的表组成的数据结构,对该数据结构可以进行下列操作: Push(X,D):将项X插入到双端队列D的前端. Pop(D):从双端队列D中删除前端项并返回. ...

  2. 使用WireShark进行网络流量安全分析

    WireShark的过滤规则 伯克利包过滤(BPF)(应用在wireshark的捕获过滤器上) ** 伯克利包过滤中的限定符有下面的三种:** Type:这种限定符表示指代的对象,例如IP地址,子网或 ...

  3. c# 使用Newtonsoft.Json解析JSON数组

    一.获取JSon中某个项的值 要解析格式: [{"VBELN":"10","POSNR":"10","RET_ ...

  4. 初探elasticsearch

    目录 安装elasticsearch elasticsearch中的层级结构与关系型数据库的对比 elasticsearch的分布式特性 集群和节点 为java用户提供的两种内置客户端 节点客户端(n ...

  5. 毕业设计——基于ZigBee的智能窗户控制系统的设计与实现

    题目:基于物联网的智能窗户控制系统的设计与实现 应用场景:突降大雨,家里没有关窗而进水:家中燃气泄漏,不能及时通风,威胁人身安全,存在火灾的隐患:家中窗户没关,让坏人有机可乘.长时间呆在人多.封闭的空 ...

  6. 解决WSL在执行32位程序时报错“Exec format error”的问题

    当你尝试在WSL上运行32位的程序时,shell将会报错:cannot execute binary file: Exec format error. 这是因为WSL目前暂不支持32位的ELF可执行文 ...

  7. 《大空头》与A股内幕消息

    目录 <大空头>简介 投行人士透露内幕消息不划算 <大空头>里合规性的一些解释 相信A股内幕消息的一些惨痛教训. 风险提示. <大空头>简介 <大空头> ...

  8. Slam笔记I

    视觉Slam笔记I 第二讲-三位空间刚体运动 点与坐标系: 基础概念: 坐标系:左手系和右手系.右手系更常用.定义坐标系时,会定义世界坐标系,相机坐标系,以及其他关心对象的坐标系.空间中任意一点可由空 ...

  9. Verbal Arithmetic Puzzle

    2020-01-02 12:09:09 问题描述: 问题求解: 这个问题不就是小学奥数题么?都知道要暴力枚举,但是如何巧妙的枚举才是问题的关键.在打比赛的时候,我用了全排列算法,TLE了. 借鉴了别人 ...

  10. Layui-admin-iframe通过页面链接直接在iframe内打开一个新的页面,实现单页面的效果

    前言: 使用Layui-admin做后台管理框架有很长的一段时间了,但是一直没有对框架内iframe菜单栏切换跳转做深入的了解.今天有一个这样的需求就是通过获取超链接中传递过来的跳转地址和对应的tab ...