我有一个问题,如果在 dart 声明,我希望用户点击列表瓷砖,并转到一个新的屏幕,如果该列表瓷砖包含医生,那么它会转到医生屏幕,如果选定的列表包含患者,它会转到患者屏幕。下面是代码,但这不是预期的工作
这是我的json文件:
{
"items": [
{
"id": "p1",
"name": "abacvj ",
"role": "nurse",
"phone_number":"8975412369",
"email_id":"abc@gmail.com",
"description": "DescriAHDLIAW JDHALOW EDWALIED EDOLJncxdlkv ption 1",
"imageUrl": "https://i.pinimg.com/originals/85/59/09/855909df65727e5c7ba5e11a8c45849a.png"
},
{
"id": "p2",
"name": " sjdbws",
"role": "nurse",
"phone_number": "8975412369",
"email_id": "abc@gmail.com",
"description": "Description 1",
"imageUrl": "https://i.pinimg.com/564x/99/24/3a/99243a96152578f9f945c3d3041f8330.jpg"
},
{
"id": "p3",
"name": "jbdlaks",
"role": "nurse",
"phone_number": "8975412369",
"email_id": "abc@gmail.com",
"description": "Description 1",
"imageUrl": "https://i.pinimg.com/564x/24/f4/6e/24f46ed3a8a94a7d9492e6b32892b94a.jpg"
},
{
"id": "p1",
"name": "Item 1",
"role": "nurse",
"phone_number": "8975412369",
"email_id": "abc@gmail.com",
"description": "Description 1",
"imageUrl": "https://i.pinimg.com/564x/2f/f8/4b/2ff84b77ba07ba8d5775dabf20cfaddc.jpg"
},
{
"id": "p2",
"name": "Item 2",
"role": "nurse",
"phone_number": "8975412369",
"email_id": "abc@gmail.com",
"description": "Description 1",
"imageUrl": "https://i.pinimg.com/564x/99/24/3a/99243a96152578f9f945c3d3041f8330.jpg"
},
{
"id": "p3",
"name": "Item 3",
"role": "nurse",
"phone_number": "8975412369",
"email_id": "abc@gmail.com",
"description": "Description 1",
"imageUrl": "https://i.pinimg.com/564x/a2/27/63/a22763d7536e8489602e182200fae27a.jpg"
},
{
"id": "p1",
"name": "Item 1",
"role": "nurse",
"phone_number": "8975412369",
"email_id": "abc@gmail.com",
"description": "Description 1",
"imageUrl": "https://i.pinimg.com/564x/2f/f8/4b/2ff84b77ba07ba8d5775dabf20cfaddc.jpg"
},
{
"id": "p2",
"name": "Item 2",
"role": "Patient",
"phone_number": "8975412369",
"email_id": "abc@gmail.com",
"description": "Description 1",
"imageUrl": "https://i.pinimg.com/564x/99/24/3a/99243a96152578f9f945c3d3041f8330.jpg"
},
{
"id": "p3",
"name": "Item 3",
"role": "nurse",
"phone_number": "8975412369",
"email_id": "abc@gmail.com",
"description": "Description 1",
"imageUrl": "https://i.pinimg.com/564x/24/f4/6e/24f46ed3a8a94a7d9492e6b32892b94a.jpg"
},{
"id": "p1",
"name": "Item 1",
"role": "Doctor",
"phone_number": "8975412369",
"email_id": "abc@gmail.com",
"description": "Description 1",
"imageUrl": "https://i.pinimg.com/originals/85/59/09/855909df65727e5c7ba5e11a8c45849a.png"
},
{
"id": "p2",
"name": "Item 2",
"role": "Doctor",
"phone_number": "8975412369",
"email_id": "abc@gmail.com",
"description": "Description edwey88e e92 i 2 g8ujeiehfhndlfaod 1",
"imageUrl": "https://i.pinimg.com/564x/24/f4/6e/24f46ed3a8a94a7d9492e6b32892b94a.jpg"
},
{
"id": "p3",
"name": "Item 3",
"role": "Doctor",
"phone_number": "8975412369",
"email_id": "abc@gmail.com",
"description": "Description fhsldkfeo;sdp keso;frsif ehf;oesfk hfeof1",
"imageUrl": "https://i.pinimg.com/564x/a2/27/63/a22763d7536e8489602e182200fae27a.jpg"
}
]
}
这是主文件:
class HomePage extends StatefulWidget {
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
List _items = [];
// Fetch content from the json file
Future<void> readJson() async {
final String response = await rootBundle.loadString('assets/sample.json');
final data = await json.decode(response);
setState(() {
_items = data["items"];
});
}
@override
void initState() {
super.initState();
// Call the readJson method when the app starts
readJson();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: const Icon(Icons.arrow_back, color: Colors.black),
onPressed: () => Navigator.of(context).pop(),
),
backgroundColor: Colors.white,
centerTitle: true,
elevation: 0,
title: const Text(
'Ask Help',
style: TextStyle(color: Colors.black),
),
),
body: Padding(
padding: const EdgeInsets.fromLTRB(12, 0, 12, 0),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
children: <Widget>[
ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
//change width and height on your need width = 200 and height = 50
minimumSize: Size(160, 50),
),
child: const Text('Patient'),
)
],
),
Column(
children: <Widget>[
OutlinedButton(
style: OutlinedButton.styleFrom(
side: const BorderSide(color: Colors.blue, width: 1),
minimumSize: Size(160, 50)),
onPressed: () {},
child: const Text(
"NGO",
style: TextStyle(color: Colors.blue),
),
),
],
)
],
),
// Display the data loaded from sample.json
Container(
child: _items.isNotEmpty
? Expanded(
child: ListView.separated(
itemCount: _items.length,
separatorBuilder: (BuildContext context, int index) =>
Divider(height: 1),
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
ListTile(
onTap: () {
print(_items[index]["role"]);
if (_items.contains(
_items[index]["role"] == 'nurse')) {
Navigator.push(
context,
MaterialPageRoute(
builder: (ctxt) => DetailPage()),
);
} else if (_items.contains(
_items[index]["role"] == 'Doctor')) {
Navigator.push(
context,
MaterialPageRoute(
builder: (ctxt) => nurse()));
}
},
leading: CircleAvatar(
child: Image.network(
_items[index]["imageUrl"]),
),
title: Text(_items[index]["name"]),
subtitle: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: <Widget>[
Text(_items[index]["phone_number"],
style: const TextStyle(
fontSize: 13.0,
fontWeight: FontWeight.normal)),
Text(_items[index]["email_id"],
style: const TextStyle(
fontSize: 13.0,
fontWeight: FontWeight.normal)),
Text(_items[index]["role"],
style: const TextStyle(
fontSize: 13.0,
fontWeight: FontWeight.normal)),
],
),
trailing: const Icon(Icons.more_vert),
),
const SizedBox(
height: 10,
),
const Align(
alignment: Alignment.centerLeft,
child: Text(
'Needs Help for',
style:
TextStyle(fontWeight: FontWeight.bold),
),
),
const SizedBox(
height: 10,
),
const Align(
alignment: Alignment.centerLeft,
child:
Text('Bluh bluh aaaaaaah I need help!'),
),
const SizedBox(
height: 10,
),
Row(
children: [
const Text(
'View Attachment',
style: TextStyle(
fontWeight: FontWeight.bold),
),
const Spacer(),
IconButton(
icon:
const Icon(Icons.play_arrow_outlined),
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) =>
DetailPage()));
},
),
],
),
],
),
);
},
),
)
: Container(),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
height: 70,
child: Center(
child: ElevatedButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => DetailPage()));
},
style: ElevatedButton.styleFrom(
//change width and height on your need width = 200 and height = 50
minimumSize: Size(300, 50),
),
child: const Text('Register'),
),
),
),
)
],
),
),
);
}
}
1条答案
按热度按时间83qze16e1#
1.使用
ListTile
的onTap
代替GestureDetector
1.使用
Column
小部件 PackageExpanded
下面是修改后的代码