Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use onTap from ListTile to go to new screen?

Tags:

flutter

dart

I'm new to flutter and dart language and this is my first real project which basically have lots of screen, I was wondering if it possible to use onTap from ListTile to go to new screen? if not I hope someone direct me. Thanks

here's my code so far:

import 'package:flutter/material.dart';
import 'package:rate_your_professor/screens/firstScreen.dart';

void main() {
  runApp(MaterialApp(
    debugShowCheckedModeBanner: false,
    title: 'Rate ',
    home: Scaffold(
      appBar: AppBar(
        title: Text("XXXXXXXXXX", textDirection: TextDirection.rtl),
      ),
      body: RaisedBookButton(),
    ),
  ));
}
Widget getListView(){
  var listView = ListView(
    children: <Widget>[


      Text("XXXXXXXXXXXXXXX", textDirection: TextDirection.rtl,textAlign: TextAlign.center,),
      ListTile(
        leading: Icon(Icons.location_city),
        title: Text("XXXXX ", textDirection: TextDirection.rtl),
        subtitle: Text("XXXXXXXXXX", textDirection: TextDirection.rtl,),

        onTap: (){
        //Here where I would like to go to new screen

        },
      ),
    ],
  );
  return listView;
}
like image 473
Moe Sul Avatar asked Jan 17 '19 02:01

Moe Sul


1 Answers

In order to use Navigator to go to different pages, you need the BuildContext of your app. Here is an example of how you can get it:

import 'package:flutter/material.dart';
import 'package:rate_your_professor/screens/firstScreen.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Some App',
      home: SomeApp(),
    );
  }
}

class SomeApp extends StatelessWidget {
  Widget getListView(BuildContext context) {
    var listView = ListView(
      children: <Widget>[
        Text(
          "XXXXXXXXXXXXXXX",
          textDirection: TextDirection.rtl,
          textAlign: TextAlign.center,
        ),
        ListTile(
          leading: Icon(Icons.location_city),
          title: Text("XXXXX ", textDirection: TextDirection.rtl),
          subtitle: Text(
            "XXXXXXXXXX",
            textDirection: TextDirection.rtl,
          ),
          onTap: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => YourNewPage(),
                  ),
            );
          },
        ),
      ],
    );
    return listView;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(body: getListView(context));
  }
}
like image 178
dshukertjr Avatar answered Sep 19 '22 01:09

dshukertjr