超过百万的StackOverflow Flutter 20大问题(推荐)

 更新时间:2020年04月17日 10:05:24   作者:老孟程序员  
这篇文章主要介绍了超过百万的StackOverflow Flutter 问题,有的问题在stackoverflow上有几十万的阅读量,说明很多人都遇到了这些问题,把这些问题整理分享给大家需要的朋友可以参考下

今天分享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数据呢:

  • 在_TestState也定义同样的参数,此方式比较麻烦,不推荐。
  • 直接使用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

总结

到此这篇关于超过百万的StackOverflow Flutter 20大问题的文章就介绍到这了,更多相关StackOverflow Flutter 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 两种Android打电话实现方法

    两种Android打电话实现方法

    这篇文章主要为大家详细介绍了两种Android打电话实现方法,具有一定的实用性,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • RxJava 1升级到RxJava 2过程中踩过的一些“坑”

    RxJava 1升级到RxJava 2过程中踩过的一些“坑”

    RxJava2相比RxJava1,它的改动还是很大的,那么下面这篇文章主要给大家总结了在RxJava 1升级到RxJava 2过程中踩过的一些“坑”,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下来要一起看看吧。
    2017-05-05
  • Android AlertDialog六种创建方式案例详解

    Android AlertDialog六种创建方式案例详解

    这篇文章主要介绍了Android AlertDialog六种创建方式案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • flutter监听app进入前后台状态的实现

    flutter监听app进入前后台状态的实现

    在开发app的过程中,我们经常需要知道app处于前后台的状态,本文主要介绍了flutter监听app进入前后台状态的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04
  • Android利用MediaRecorder实现录音功能

    Android利用MediaRecorder实现录音功能

    这篇文章主要为大家详细介绍了Android利用MediaRecorder实现录音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • 详解Flutter如何在单个屏幕上实现多个列表

    详解Flutter如何在单个屏幕上实现多个列表

    这篇文章主要为大家详细介绍了Flutter如何在单个屏幕上实现多个列表,这些列表可以水平排列、网格格式、垂直排列,甚至是这些常用布局的组合,感兴趣的小伙伴可以了解下
    2023-11-11
  • Android Studio 多层级 Module 对 aar 引用问题解决方法

    Android Studio 多层级 Module 对 aar 引用问题解决方法

    这篇文章主要介绍了Android Studio 多层级 Module 对 aar 引用问题的解决方法,需要的朋友参考下
    2017-12-12
  • Android中ViewPager实现滑动指示条及与Fragment的配合

    Android中ViewPager实现滑动指示条及与Fragment的配合

    这篇文章主要介绍了Android中ViewPager实现滑动指示条及与Fragment的配合,使用Fragment实现ViewPager的滑动是一种比较推荐的做法,需要的朋友可以参考下
    2016-03-03
  • VideoView实现视频无缝连续播放

    VideoView实现视频无缝连续播放

    这篇文章主要为大家详细介绍了VideoView实现视频无缝连续播放,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • 浅析安卓(Android)的性能优化

    浅析安卓(Android)的性能优化

    性能优化是一个大的范畴,如果有人问你在Android中如何做性能优化的,也许都不知道从哪开始说起。那么这篇文章我们从布局优化和内存优化两个方面来展开说如何进行Android的性能优化。
    2016-08-08

最新评论