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目前的樣子:

flutter1

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)
 ];
...
image1

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'),
      ),

    );
  }
}

Python Scipy科學計算學習筆記

參考資料

資料分析與視覺化 ( 5 )-資料好幫手 Pandas 與 Scipy 基本操作

安裝

 pip install scipy

線性代數

import numpy as np
from scipy import linalg

# 解二元一次線性方程式
# 2x + 2y = 5
# 4x + 3y = 6

# 係數
A = np.array([[2,2],[4,3]])

# 答案
B = np.array([[5],[6]])

# 解方程式
ans = linalg.solve(A,B)

"answer: x=%s, y=%s" % (ans[0][0], ans[1][0])     

積分

from scipy import integrate

# 對 x 三次方從 0 到 2 進行定積分
ans= integrate.quad(lambda x: x**3, 0,2)

ans[0]

傅立葉轉換

from scipy.fftpack import fft, ifft

x = np.array([1.0, 1.5, 1.0, -1.0, 1.5])
y = fft(x)
y

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,
                  ),
                )
              ],
            ),
          ],
        ),
      ),
    );
  }
}

Flutter:Reponsive Design

Build Your Responsive Flutter Layout like A Pro

responsive_grid 1.0.1

Responsive grid properties include:

  • xs stands for extra small (for phones — screens less than 768px wide)
  • sm stands for small (for tablets — screens equal to or greater than 768px wide)
  • md stands for medium (for small laptops — screens equal to or greater than 992px wide)
  • lg stands for large (for laptops and desktops — screens equal to or greater than 1200px wide)

修改 pubspec.yaml

//在dependencies中加入responsive_grid: ^1.0.1
dependencies:
  responsive_grid: ^1.0.1

修改lib/main.dart

import 'package:flutter/material.dart';
import 'package:responsive_grid/responsive_grid.dart';

void main() => runApp(MaterialApp(
  title: 'Responsive Layout',
  home: HomePage(),
  debugShowCheckedModeBanner: false,
),
);

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Responsive Layout"),
        centerTitle: true,
      ),
      body: SingleChildScrollView(
              child: Container(
        child:

           ResponsiveGridRow(
                children: [
                     ResponsiveGridCol(
                    lg: 12,
                    child: Container(
                      height: 100,
                      alignment: Alignment.center,
                      color: Colors.purple,
                      child: Text('Colunm 1', style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500)),
                    ),
                  ),
                  ResponsiveGridCol(
                    xs: 6,
                    md: 3,
                    child: Container(
                      height: 100,
                      alignment: Alignment.center,
                      color: Colors.green,
                      child: Text("Colunm 2, Row 1", style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500)),
                    ),
                  ),
                  ResponsiveGridCol(
                    xs: 6,
                    md: 3,
                    child: Container(
                      height: 100,
                      alignment: Alignment.center,
                      color: Colors.orange,
                      child: Text("Colunm 2, Row 2", style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500)),
                    ),
                  ),
                  ResponsiveGridCol(
                    xs: 6,
                    md: 3,
                    child: Container(
                      height: 100,
                      alignment: Alignment.center,
                      color: Colors.red,
                      child: Text("Colunm 3, Row 1", style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500),),
                    ),
                  ),
                  ResponsiveGridCol(
                    xs: 6,
                    md: 3,
                    child: Container(
                      height: 100,
                      alignment: Alignment.center,
                      color: Colors.blue,
                      child: Text("Colunm 3, Row 2", style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500),),
                    ),
                  ),
                  ResponsiveGridCol(
                    child: Container(
                      height: 100,
                      alignment: Alignment.center,
                      color: Colors.yellow,
                      child: Text("Colunm 4", style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500),),
                    ),
                  ),
                  ResponsiveGridCol(
                    child: Container(
                      height: 100,
                      alignment: Alignment.center,
                      color: Colors.red,
                      child: Text("Colunm 5", style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500),),
                    ),
                  ),
                   ResponsiveGridCol(
                    xs: 6,
                    md: 3,
                    child: Container(
                      height: 100,
                      alignment: Alignment.center,
                      color: Colors.green,
                      child: Text("Colunm 6, Row 1", style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500)),
                    ),
                  ),
                  ResponsiveGridCol(
                    xs: 6,
                    md: 3,
                    child: Container(
                      height: 100,
                      alignment: Alignment.center,
                      color: Colors.orange,
                      child: Text("Colunm 6, Row 2", style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500)),
                    ),
                  ),
                ]
          ),

    ),
      ),
    );
  }
}

Flutter網路圖片APP

修改lib/main.dart

import 'package:flutter/material.dart';

void main(){
  runApp(
    MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.cyan[400],
        appBar: AppBar(
          title:Text('Network Image APP'),
          backgroundColor: Colors.cyan,
          ),
      body: Center(
        child: Image(
          image:
                NetworkImage('http://icons.iconarchive.com/icons/graphicloads/100-flat/256/home-icon.png'),
        ),
      ),
      ),
    ),
  );
}