not able to use conditional statement within listview onTap

49 Views Asked by At

I have a problem regarding if statement in dart, I want the user to tap the list tile and go to a new screen if that list tile contains doctor then it will go to doctor screen if selected list contains patient it will go to patient screen. below is the code but thats not working as expected

this is my json file:

{
"items": [
    {
        "id": "p1",
        "name": "abacvj ",
        "role": "nurse",
        "phone_number":"8975412369",
        "email_id":"[email protected]",
        "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": "[email protected]",
        "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": "[email protected]",
        "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": "[email protected]",
        "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": "[email protected]",
        "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": "[email protected]",
        "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": "[email protected]",
        "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": "[email protected]",
        "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": "[email protected]",
        "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": "[email protected]",
        "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": "[email protected]",
        "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": "[email protected]",
        "description": "Description fhsldkfeo;sdp keso;frsif ehf;oesfk hfeof1",
        "imageUrl": "https://i.pinimg.com/564x/a2/27/63/a22763d7536e8489602e182200fae27a.jpg"
    }
]

}

and this is main file:

 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

There are 1 best solutions below

7
Neil-NotNeo On
  1. Instead of GestureDetector use onTap of ListTile

  2. Wrap your Expanded with a Column widget

Here's the modified code

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: _items.isNotEmpty
            ? Column(
                children: [
                  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[index]["role"] == 'nurse') {
                                     Navigator.push(
                                      context,
                                      MaterialPageRoute(builder: (ctxt) => DetailPage()),
                                     );
                                  } else if (_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) => Form(
                                            child: DetailPage(),
                                          ),
                                        ),
                                      );
                                    },
                                  ),
                                ],
                              ),
                            ],
                          ),
                        );
                      },
                    ),
                  ),
                ],
              )
            : Container(),
      ),
    );
  }