flutter - 我如何将变量 id 传递到颤振中的另一个页面
问题描述
大家好,我有两个页面,我想在第二个屏幕中使用变量“id”
我该做什么 ?
第一个屏幕:这是用户输入登录名和密码的登录屏幕,然后我得到所有订单列表,然后我需要选择 ID 并在第二个屏幕中使用它
屏幕二:我通过 id 显示该用户的订单
注意:我通过 api 获取所有数据
屏幕一:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'order_list_screen.dart';
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
bool _isLoading = false;
@override
Widget build(BuildContext context) {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light.copyWith(statusBarColor:
Colors.transparent));
return Scaffold(
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.blue, Colors.teal],
begin: Alignment.topCenter,
end: Alignment.bottomCenter),
),
child: _isLoading ? Center(child: CircularProgressIndicator()) : ListView(
children: <Widget>[
headerSection(),
textSection(),
buttonSection(),
],
),
),
);
}
signIn(String login, pass) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
Map data = {
'login': login,
'password': pass,
};
var jsonResponse = null;
var response = await http.post("https://api/ws/v4/Delivery/login.php", body: data);
if(response.statusCode == 200) {
jsonResponse = json.decode(response.body);
print(jsonResponse);
jsonResponse['token']="isLoged";
if(jsonResponse != null) {
setState(() {
_isLoading = false;
});
sharedPreferences.setString("token", jsonResponse['livreur']['nom']);
sharedPreferences.setInt("idliv", jsonResponse['livreur']['id']);
Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (BuildContext context) =>
OrderListScreen()), (Route<dynamic> route) => false);
}
}
else {
setState(() {
_isLoading = false;
});
print(response.body);
}
}
Container buttonSection() {
return Container(
width: MediaQuery.of(context).size.width,
height: 40.0,
padding: EdgeInsets.symmetric(horizontal: 15.0),
margin: EdgeInsets.only(top: 15.0),
child: RaisedButton(
onPressed: loginController.text == "" || passwordController.text == "" ? null : () {
setState(() {
_isLoading = true;
});
signIn(loginController.text, passwordController.text);
},
elevation: 0.0,
color: Colors.purple,
child: Text("Sign In", style: TextStyle(color: Colors.white70)),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5.0)),
),
);
}
final TextEditingController loginController = new TextEditingController();
final TextEditingController passwordController = new TextEditingController();
Container textSection() {
return Container(
padding: EdgeInsets.symmetric(horizontal: 15.0, vertical: 20.0),
child: Column(
children: <Widget>[
TextFormField(
controller: loginController,
cursorColor: Colors.white,
style: TextStyle(color: Colors.white70),
decoration: InputDecoration(
icon: Icon(Icons.person, color: Colors.white70),
hintText: "Email",
border: UnderlineInputBorder(borderSide: BorderSide(color: Colors.white70)),
hintStyle: TextStyle(color: Colors.white70),
),
),
SizedBox(height: 30.0),
TextFormField(
controller: passwordController,
cursorColor: Colors.white,
obscureText: true,
style: TextStyle(color: Colors.white70),
decoration: InputDecoration(
icon: Icon(Icons.lock, color: Colors.white70),
hintText: "Password",
border: UnderlineInputBorder(borderSide: BorderSide(color: Colors.white70)),
hintStyle: TextStyle(color: Colors.white70),
),
),
],
),
);
}
Container headerSection() {
return Container(
margin: EdgeInsets.only(top: 50.0),
padding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 30.0),
child: Text("Code Land",
style: TextStyle(
color: Colors.white70,
fontSize: 40.0,
fontWeight: FontWeight.bold)),
);
}
}
屏幕二:
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:deliveryman/screens/login_screen.dart';
Future<OrderListData> fetchOrderListData() async {
final response = await http.post(
'https://api/ws/v3/Delivery/orders.php',
body: {'livreurID': '1'});
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
return OrderListData.fromJson(json.decode(response.body));
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load album');
}
}
class OrderListData {
final orderID;
final orderDate;
final deliveryWishedTime;
final deliveryTime;
final deliveryZone;
final clientFirstName;
final clientLastName;
final restoName;
final restoImagePath;
final nbrProduct;
final status;
final latitude;
final longitude;
OrderListData({
this.orderID,
this.orderDate,
this.deliveryWishedTime,
this.deliveryTime,
this.deliveryZone,
this.clientFirstName,
this.clientLastName,
this.restoName,
this.restoImagePath,
this.nbrProduct,
this.status,
this.latitude,
this.longitude,
});
factory OrderListData.fromJson(Map<String, dynamic> json) {
return OrderListData(
orderID: json['Orders'][0]['orderID'],
orderDate: json['Orders'][0]['orderDate'],
deliveryWishedTime: json['Orders'][0]['deliveryWishedTime'],
deliveryTime: json['Orders'][0]['deliveryTime'],
deliveryZone: json['Orders'][0]['deliveryZone'],
clientFirstName: json['Orders'][0]['clientFirstName'],
clientLastName: json['Orders'][0]['clientLastName'],
restoName: json['Orders'][0]['restoName'],
restoImagePath: json['Orders'][0]['restoImagePath'],
nbrProduct: json['Orders'][0]['nbrProduct'],
status: json['Orders'][0]['status'],
latitude: json['Orders'][0]['latitude'],
longitude: json['Orders'][0]['longitude'],
);
}
}
解决方案
您应该在第二个屏幕中有一个构造函数:
class SecondScreen extends StatefulWidget {
final int id;
const SecondScreen({Key key, this.id}) : super(key: key);@override
_SecondScreenState createState() => _SecondScreenState();
}
class _SecondScreenState extends State<SecondScreen> {
@override
Widget build(BuildContext context) {
return Container(
);
}
}
并像这样通过它:
Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context)=>SecondScreen(id: 1,)), (route) => false)
推荐阅读
- python - Django postgres SplitArrayField 在提交表单时抛出错误
- javascript - redux item reducer的异步函数中的返回语句不改变状态
- nginx - Nginx 允许特定的 ip4 地址
- c++ - 如何设置我的构造函数以使头部和尾部指向一个哨兵?(在 C++ 中的单链表中)
- perl - 如何直接取消引用存储在另一个哈希中的哈希引用?
- asp.net-mvc - 如何在同一个解决方案中使用 Web API?
- oop - 我应该为每个不同的构造函数创建类还是应该只为一个类创建两个构造函数?
- python-3.x - 从具有多个条件的 Pandas 数据帧执行计算
- replace - How to create list of files containing only filename in one set_fact task
- postgresql - 将 JSON.RawMessage 转换为 JSON