实现跳转
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文件中加入:
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
}
}