瀏覽分類:
Flutter
Flutter:底部導航列換頁(Bottom Navigation)
How to use Flutter to build an app with bottom navigation
Step 1. 定義進入點
開啟lib/main.dart。刪除所有程式碼,在首行寫下:
import 'package:flutter/material.dart';
新增Main方法:
void main() => runApp(App());
添加以下方法
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My Flutter App',
home: Home(),
);
}
}
上面程式新增了叫作 App 的stateless widget。
並且設定home 作為APP的首頁屬性,我們設定 Home作為首個出現的畫面
Step 2. 建立home頁面
在 lib資料夾中,新增home_widget.dart
import 'package:flutter/material.dart';
class Home extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _HomeState();
}
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('My Flutter App'),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: 0, // this will be set when a new tab is tapped
items: [
BottomNavigationBarItem(
icon: new Icon(Icons.home),
title: new Text('Home'),
),
BottomNavigationBarItem(
icon: new Icon(Icons.mail),
title: new Text('Messages'),
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
Title: Text('Profile')
)
],
),
);
}
}
回到main.dart,在第二行寫下:
import 'home_widget.dart';
APP目前的樣子:

Step 3. 準備Navigation
回到 home_widget.dart :
class _HomeState extends State<Home> {
int _currentIndex = 0;
final List<Widget> _children = [];
...
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('My Flutter App'),
),
body: _children[_currentIndex], // new
bottomNavigationBar: BottomNavigationBar(
onTap: onTabTapped, // new
currentIndex: _currentIndex, // new
items: [
new BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
new BottomNavigationBarItem(
icon: Icon(Icons.mail),
title: Text('Messages'),
),
new BottomNavigationBarItem(
icon: Icon(Icons.person),
title: Text('Profile')
)
],
),
);
}
新增的程式用// new來表示
Step 4. 處理 navigation
在_HomeState 的底部新增以下內容:
void onTabTapped(int index) {
setState(() {
_currentIndex = index;
});
}
Step 5. 新增child widgets
在lib資料夾中新增placeholder_widget.dart. 這個檔案負責管理背景顏色(以展示我們確實切換到不同的頁面。
import 'package:flutter/material.dart';
class PlaceholderWidget extends StatelessWidget {
final Color color;
PlaceholderWidget(this.color);
@override
Widget build(BuildContext context) {
return Container(
color: color,
);
}
}
回到 home_widget.dart 新增以下內容:
import 'placeholder_widget.dart';
在 _children中新增下面list內容
class _HomeState extends State<Home> {
int _currentIndex = 0;
final List<Widget> _children = [
PlaceholderWidget(Colors.white),
PlaceholderWidget(Colors.deepOrange),
PlaceholderWidget(Colors.green)
];
...

Flutter:前往新視窗並且返回


導航到新頁面並返回
在上面提供的參考資料連結中,兩個screen都寫在main.dart中
本文嘗試將:
(1)第二個screen寫在第二個檔案”SecondScreen.dart”裡面
(2)將SecondScreen裡面的RaisedButton按鈕改成網路圖片的顯示
(3)AppBar標題改成置中顯示(centerTitle:true)
lib/main.dart
import 'package:flutter/material.dart';
import 'SecondScreen.dart';
void main() {
runApp(
new MaterialApp(
title: 'Navigation Basics',
home: new FirstScreen(),
));
}
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
centerTitle:true,
title: new Text('First Screen'),
),
body: new Center(
child: new RaisedButton(
child: new Text('Launch new screen'),
onPressed: () {
Navigator.push(
context,
new MaterialPageRoute(builder: (context) => new SecondScreen()),
);
},
),
),
);
}
}
lib/SecondScreen.dart
import 'package:flutter/material.dart';
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Second Screen"),
centerTitle:true,
),
body: new Center(
child: Image.network('https://cdn.jsdelivr.net/gh/flutterchina/website@1.0/images/homepage/header-illustration.png'),
),
);
}
}
Flutter:新增splash screen
Updating the launch screen
Flutter:如何將AppBar標題的文字置中?

//在text下方多加一行
centerTitle: true,

Flutter常用指令
flutter create app //create a flutter project named "app"
cd app //change directory to folder "app"
flutter run //start the flutter app
flutter pub get //install new package from "pubspec.yaml"
r //hot reload
R //hot restart
Flutter個人簡介APP

修改lib/main.dart
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
home: YanweiLiu(),
));
class YanweiLiu extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue[900],
appBar: AppBar(
title: Text('個人簡介'),
centerTitle: true,
backgroundColor: Colors.blue[850],
elevation: 0.0,
),
body: Padding(
padding: const EdgeInsets.fromLTRB(30.0, 40.0, 30.0, 0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Center(
child: CircleAvatar(
radius: 100.0,
backgroundImage: NetworkImage('https://scontent-tpe1-1.xx.fbcdn.net/v/t1.0-9/14492368_1468759719807087_7594963659703372073_n.jpg?_nc_cat=102&_nc_oc=AQn_97oc5hI1pyHGmb8F4UdEaWQ8qR38iD0R0furVD_3Ag8wRuQMcVdAsOO5IW65c5s&_nc_ht=scontent-tpe1-1.xx&oh=b97782923c72a7d4dda4d29efece14c0&oe=5DFE8FDC'),
),
),
Divider(
color: Colors.blue[700],
height: 40.0,
),
Text(
'名稱',
style: TextStyle(
color: Colors.grey[300],
letterSpacing: 2.0,
),
),
SizedBox(height: 10.0),
Text(
'Yanwei Liu',
style: TextStyle(
color: Colors.amberAccent[200],
fontWeight: FontWeight.bold,
fontSize: 16.0,
letterSpacing: 2.0,
),
),
SizedBox(height: 10.0),
Text(
'地點',
style: TextStyle(
color: Colors.grey[300],
letterSpacing: 2.0,
),
),
SizedBox(height: 10.0),
Text(
'高雄',
style: TextStyle(
color: Colors.amberAccent[200],
fontWeight: FontWeight.bold,
fontSize: 16.0,
letterSpacing: 2.0,
),
),
SizedBox(height: 10.0),
Text(
'研究領域',
style: TextStyle(
color: Colors.grey[300],
letterSpacing: 2.0,
),
),
SizedBox(height: 10.0),
Text(
'ML&DL / Front End/ Web Scraping',
style: TextStyle(
color: Colors.amberAccent[200],
fontWeight: FontWeight.bold,
fontSize: 16.0,
letterSpacing: 2.0,
),
),
SizedBox(height: 10.0),
Row(
children: <Widget>[
Icon(
Icons.email,
color: Colors.grey[400],
),
SizedBox(width: 10.0),
Text(
'e96031413@hotmail.com',
style: TextStyle(
color: Colors.grey[400],
fontSize: 18.0,
letterSpacing: 1.0,
),
)
],
),
],
),
),
);
}
}
- 1
- 2