老孟導讀:一個月前分享的《超過百萬的StackOverflow Flutter 問題-第一期》受到不少朋友的喜歡,很是感謝你們的支持,在文章末尾有第一期的連接,但願此文能對你有所幫助。html
這個問題估計大部分都遇到過,解決方法以下:android
執行flutter doctor
ios
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.12.13+hotfix.9, on Mac OS X 10.14.6 18G1012,
locale zh-Hans-CN)
[!] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
! Some Android licenses not accepted. To resolve this, run: flutter doctor
--android-licenses
[✓] Xcode - develop for iOS and macOS (Xcode 11.3.1)
[✓] Android Studio (version 3.5)
[✓] Connected device (1 available)
! Doctor found issues in 1 category.
複製代碼
保證沒有紅色的叉。git
啓動手機或者模擬器(Android系統大於16),開啓USB 調試
模式,不一樣手機開啓方法略有不一樣,以華爲手機爲例:進入設置->系統->關於手機,快速連續點擊版本號5次,提示打開開發者模式
,返回設置,此時會出現開發人員選項
菜單,進入,打開開發人員選項
和USB 調試
,彈出受權菜單,贊成便可。bash
打開Android Studio,查看鏈接的手機:微信
若是依然沒法鏈接手機,打開Android Studio設置界面:app
選擇最近的API。less
到此基本就能夠解決了,若是還沒法鏈接,那基本就是adb
的問題,極可能是adb
端口被佔用,關於adb
的解決方案可百度,引發adb
問題有不少種狀況。async
在Material Design設計規範中Snackbars就是Toast提示,Snackbar用法以下:ide
Scaffold.of(context).showSnackBar(SnackBar(
content: Text("Sending Message"),
));
複製代碼
這個效果在國內來不是很接受,因此通常使用第三方插件fluttertoast
Fluttertoast.showToast(
msg: "This is Toast messaget",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIos: 1
);
複製代碼
建立圓角Button的方式有不少種,下面介紹幾種簡單的:
使用 FlatButton
和 RaisedButton
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
side: BorderSide(color: Colors.red)
),
複製代碼
使用ClipRRect
ClipRRect(
borderRadius: BorderRadius.circular(40),
child: RaisedButton(
onPressed: () {},
child: Text("Button"),
),
)
複製代碼
使用ButtonTheme
ButtonTheme(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
child: RaisedButton(
onPressed: () {},
child: Text("Button"),
),
)
複製代碼
Flutter應用程序啓動時會出現一段時間的白屏,由於程序要啓動引擎,因此App第一次啓動比較慢,在原生端會顯示一段時間的白色啓動頁,咱們把這個白色啓動頁作爲應用程序的啓動頁,替換爲本身的圖片,此方案的啓動頁只能是一張圖片,沒法交互,若是須要啓動頁有交互效果建議使用Flutter作。
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/launch_image" />
</item> -->
</layer-list>
複製代碼
修改成:
<?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>
<bitmap
android:gravity="center"
android:src="@drawable/splash" />
</item>
</layer-list>
複製代碼
將splash.png
圖片拷貝到drawable文件夾下。
iOS端,打開ios/Runner/Assets.xcassets/LaunchImage.imageset下面的3張LaunchImage.png圖片替換,保持名稱不變。
Android平臺上打開android/app/build.gradle:
defaultConfig {
applicationId "com.example.fluttersample"
minSdkVersion 16
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
複製代碼
修改applicationId屬性便可。
iOS平臺打開ios/Runner/Info.plist,修改CFBundleIdentifier的值:
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
複製代碼
new Container(
margin: const EdgeInsets.all(15.0),
padding: const EdgeInsets.all(3.0),
decoration: BoxDecoration(
border: Border.all(color: Colors.blueAccent)
),
child: Text("My Awesome Border"),
)
複製代碼
SizedBox.expand(
child: RaisedButton(...),
)
複製代碼
或者
SizedBox(
width: double.infinity,
// height: double.infinity,
child: RaisedButton(...),
)
複製代碼
或者
ConstrainedBox(
constraints: const BoxConstraints(minWidth: double.infinity),
child: RaisedButton(...),
)
複製代碼
或者
ButtonTheme(
minWidth: double.infinity,
child: MaterialButton(
onPressed: () {},
child: Text('Raised Button'),
),
),
複製代碼
給ListView指定高度:
Column(
children: <Widget>[
Container(
height: 50,
child: ListView(),
)
],
)
複製代碼
或者鋪滿Column:
Column(
children: <Widget>[
Expanded(
child: horizontalList,
)
],
);
複製代碼
ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Image.network(
'',
),
)
複製代碼
或者
CircleAvatar(
radius: 20,
backgroundImage: NetworkImage('https://via.placeholder.com/140x100')
)
複製代碼
或者
ClipOval(
child: Image.network(
"image_url",
height: 100,
width: 100,
fit: BoxFit.cover,
),
),
複製代碼
或者
Container(
width: 100.0,
height: 150.0,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage('Path to your image')
),
borderRadius: BorderRadius.all(Radius.circular(8.0)),
color: Colors.redAccent,
),
複製代碼
InputDecoration(
border: InputBorder.none,
hintText: 'Username',
),
),
複製代碼
設置支持的方向:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
return new MaterialApp(...);
}
}
複製代碼
打開ios/Runner/Info.plist,設置支持的方向:
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
複製代碼
使用Opacity
Opacity(
opacity: .0,
child: ,
)
複製代碼
或者
Visibility(
visible: false,
child: ,
)
複製代碼
或者
Offstage(
offstage: true,
child: ,
)
複製代碼
@override
Widget build(BuildContext context) {
return new WillPopScope(
onWillPop: () async => false,
child: new Scaffold(
appBar: new AppBar(
title: new Text("data"),
leading: new IconButton(
icon: new Icon(Icons.ac_unit),
onPressed: () => Navigator.of(context).pop(),
),
),
),
);
}
複製代碼
ButtonTheme(
minWidth: 200.0,
height: 100.0,
child: RaisedButton(
onPressed: () {},
child: Text("test"),
),
);
複製代碼
或者
SizedBox(
width: 100, // specific value
child: RaisedButton(...)
)
複製代碼
使用PreferredSize:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Example',
home: Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(50.0), // here the desired height
child: AppBar(
// ...
)
),
body: // ...
)
);
}
}
複製代碼
Dart API自己沒有格式化時間的接口,使用intl:
import 'package:intl/intl.dart';
DateTime now = DateTime.now();
String formattedDate = DateFormat('yyyy-MM-dd – kk:mm').format(now);
複製代碼
Widget getTextWidgets(List<String> strings)
{
List<Widget> list = new List<Widget>();
for(var i = 0; i < strings.length; i++){
list.add(new Text(strings[i]));
}
return new Row(children: list);
}
複製代碼
或者
Row(children: strings.map((item) => new Text(item)).toList())
複製代碼
或者
var list = ["one", "two", "three", "four"];
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
for(var item in list ) Text(item)
],
),
複製代碼
使用childAspectRatio,設置以下:
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<String> widgetList = ['A', 'B', 'C'];
@override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
/*24 is for notification bar on Android*/
final double itemHeight = (size.height - kToolbarHeight - 24) / 2;
final double itemWidth = size.width / 2;
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Container(
child: new GridView.count(
crossAxisCount: 2,
childAspectRatio: (itemWidth / itemHeight),
controller: new ScrollController(keepScrollOffset: false),
shrinkWrap: true,
scrollDirection: Axis.vertical,
children: widgetList.map((String value) {
return new Container(
color: Colors.green,
margin: new EdgeInsets.all(1.0),
child: new Center(
child: new Text(
value,
style: new TextStyle(
fontSize: 50.0,
color: Colors.white,
),
),
),
);
}).toList(),
),
),
);
}
}
複製代碼
import 'package:flutter_statusbarcolor/flutter_statusbarcolor.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
FlutterStatusbarcolor.setStatusBarColor(Colors.white);
return MaterialApp(
title: app_title,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(title: home_title),
);
}
}
複製代碼
或者
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.white
));
複製代碼
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
//your elements here
],
);
複製代碼
老孟Flutter博客地址(近200個控件用法):laomengit.com
歡迎加入Flutter交流羣(微信:laomengit)、關注公衆號【老孟Flutter】: