第五节 Navigator导航、引用本地图片、Android客户端打包

实现跳转

main.dart文件

import 'package:flutter/material.dart';

void main(){
  runApp(MaterialApp(
      title:'导航演示',
      home: ProductListPage()
  ));
}

class ProductListPage extends StatelessWidget
{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('商品列表页面'),
        backgroundColor: Colors.blueAccent,
      ),

      body: Center(
        child: RaisedButton(
          onPressed: (){
            Navigator.push(context, MaterialPageRoute(
                builder: (context)=>ProductInfoPage())
            );
          },
          child: Text('点击跳转'),
        ),

      ),

    );
  }

}

class ProductInfoPage extends StatelessWidget
{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('商品详情页面'),
        backgroundColor: Colors.cyan,
      ),

      body: Center(
        child: RaisedButton(
          onPressed: (){
            Navigator.pop(context);
          },
          child: Text('点击返回'),
        ),

      ),

    );
  }

}


实现参数传递和接收

main.dart文件

import 'package:flutter/material.dart';

class Product{
  String name;
  String info;
  //注意构造函数参数带this
  Product(this.name,this.info);
}
void main(){
  runApp(MaterialApp(
      title:'导航演示',
      home: ProductListPage(
        products:List.generate(20, (i)=>Product('商品名称$i','商品详情$i'))
      ),
  ));
}

class ProductListPage extends StatelessWidget
{
  final List<Product> products;
  ProductListPage({Key key,@required this.products}):super(key:key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('商品列表页面'),
        backgroundColor: Colors.blueAccent,
      ),

      body: ListView.builder(itemBuilder: (context,i){
        return ListTile(
          title: Text(products[i].name),
          //列表点击
          onTap: (){
            Navigator.push(context, MaterialPageRoute(builder: (context)=>ProductInfoPage(product:products[i])));
          },
        );
      })

    );
  }

}

class ProductInfoPage extends StatelessWidget
{
  final Product product;
  ProductInfoPage({Key key,@required this.product}):super(key:key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('商品详情页面'),
        backgroundColor: Colors.cyan,
      ),

      body: Center(
        child: Text(
          '${product.info}'
        )

      ),

    );
  }

}


页面跳转并返回数据

main.dart文件

import 'package:flutter/material.dart';

class Product{
  String name;
  String info;
  //注意构造函数参数带this
  Product(this.name,this.info);
}
void main(){
  runApp(MaterialApp(
      title:'导航演示',
      home: ProductListPage(
        products:List.generate(20, (i)=>Product('商品名称$i','商品详情$i'))
      ),
  ));
}

class ProductListPage extends StatelessWidget
{
  final List<Product> products;
  ProductListPage({Key key,@required this.products}):super(key:key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('商品列表页面'),
        backgroundColor: Colors.blueAccent,
      ),

      body: ListView.builder(itemBuilder: (context,i){
        return ListTile(
          title: Text(products[i].name),
          //列表点击
          onTap: (){
            _navigateToProductInfoPage(context, products[i]);
          },
        );
      })

    );
  }

  //注意这里的async、await的使用
  _navigateToProductInfoPage(BuildContext context,Product product) async
  {
    final result=await Navigator.push(
        context,
        MaterialPageRoute(builder: (context)=>ProductInfoPage(product:product)));

        Scaffold.of(context).showSnackBar(SnackBar(content: Text('$result')));
  }


}


class ProductInfoPage extends StatelessWidget
{
  final Product product;
  ProductInfoPage({Key key,@required this.product}):super(key:key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('商品详情页面'),
        backgroundColor: Colors.cyan,
      ),

      body: RaisedButton(onPressed: (){

        Navigator.pop(context,product.info+"第二个页面");

      },
      child: Text('返回'),
      ),

    );
  }

}


加载本地图片
void main(){
  runApp(MaterialApp(
      title:'导航演示',
      home: ProductListPage()
  ));
}

class ProductListPage extends StatelessWidget
{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('商品列表页面'),
        backgroundColor: Colors.blueAccent,
      ),

      body: Image.asset('images/timg.jpeg')

    );
  }

}

项目根目录新建images文件夹
在pubspec.ymal文件中加入:


image.png

Android客户端打包

keytool -genkey -v -keystore ~/key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias key


修改build.gradle文件

signingConfigs{
        release {
            keyAlias 'key'
            keyPassword 'flutter'
            storeFile file('/Users/huozhenpeng/key.jks')
            storePassword 'flutter'
        }
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.release
        }
    }
打包成功
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容