首页 > 解决方案 > 如何在 Flutter 中使用 ListTile 导航到新页面/路由?

问题描述

在 Flutter 中,当我按下 ListTile 小部件时,我试图导航到新页面。我正在尝试使用 ontap() 函数,但是它似乎不起作用。

ListTile(
                leading: Icon(Icons.border_color),
                title: Text('Freelance English Native needed'),
                subtitle: Text('Requirements: Native English'),
                onTap: () {
                  Navigator.pushNamed(context, '/second');
                },
              ),

MaterialApp(
  // Start the app with the "/" named route. In this case, the app starts
  // on the FirstScreen widget.
  initialRoute: '/',
  routes: {
    // When navigating to the "/" route, build the FirstScreen widget.
    '/': (context) => MyApp(),
    // When navigating to the "/second" route, build the SecondScreen widget.
    '/second': (context) => SecondScreen(),
  },
);

标签: flutter

解决方案


尝试这个

ListTile(
                leading: Icon(Icons.border_color),
                title: Text('Freelance English Native needed'),
                subtitle: Text('Requirements: Native English'),
                onTap: () {
                 Navigator.push(
                   context,
                    MaterialPageRoute(builder: (context) => SecondScreen()),
        );
                },
              ),

推荐阅读