我想使用Push Name从第1页到第2页,并使用Disdisable Widget关闭第2页.

按下按钮时,关闭时会出现黑屏.

enter image description here

当我拉下关闭第2页时,我想看到第1页

我举了this个例子

class FirstScreen extends StatelessWidget {
  const FirstScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('First Screen'),
      ),
      body: Center(
        child: ElevatedButton(
          // Within the `FirstScreen` widget
          onPressed: () {
            // Navigate to the second screen using a named route.
            Navigator.pushNamed(context, '/second');
          },
          child: const Text('Launch screen'),
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  const SecondScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Dismissible(
      key: UniqueKey(),
      movementDuration: const Duration(milliseconds: 0),
      resizeDuration: const Duration(milliseconds: 1),
      onDismissed: (d) => Navigator.of(context).pop(),
      direction: DismissDirection.down,
      child: Scaffold(
        appBar: AppBar(
          title: const Text('Second Screen'),
        ),
        body: Center(
        child: ElevatedButton(
          // Within the SecondScreen widget
          onPressed: () {
            // Navigate back to the first screen by popping the current route
            // off the stack.
            Navigator.pop(context);
          },
          child: const Text('Go back!'),
        ),
        ),
        
      ),
    );
  }
}

如果我用这个,它就能用了!但我不想用它

Navigator.of(context).push(
              PageRouteBuilder(
              opaque: false,
              pageBuilder: (context, animation, secondaryAnimation) => const SecondScreen(),
            ));

void main() {
   runApp(
       MaterialApp( 
         title: 'Named Routes Demo',
         initialRoute: '/',
         routes: {
             '/': (context) => const FirstScreen(),
             '/second': (context) => const SecondScreen(), }, ), 
       );
 }

推荐答案

使用MaterialApp.onGenerateRoute而不是MaterialApp.routes,并使用已起作用的PageRouteBuilder.

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Named Routes Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      onGenerateRoute: (settings) {
        switch (settings.name) {
          case '/':
            return MaterialPageRoute(builder: (context) => const FirstScreen());
          case '/second':
            return PageRouteBuilder(
              opaque: false,
              pageBuilder: (context, animation, secondaryAnimation) =>
                  const SecondScreen(),
            );
        }
      },
    );
  }
}

Flutter相关问答推荐

如何为Android 12及以上设备以及Android 11及以下设备动态处理Splash Screen?如果可能的话没有包裹

壁炉有序在Flutter

在Flutter中,有没有一种方法可以点击页面到后面的页面?

从底部开始固定,然后使其可拖曳Flutter

从外部类动态更改IconButton的图标

你能go 掉Flutter 小部件测试中的样板吗?

框中的Flutter 适配图像

如何画三角形的圆角?

构建方法中的性能声明小部件

Dart 和 flutter 错误无法在您的 PATH 中找到 git

Flutter http 请求获取 null

要禁用按钮上的通知声音,请点击Flutter

如果有空间则居中CustomScrollView

更改 flutter listview 中的布局

使用 Riverpod 和 copyWith 方法更新数据类中列表中变量的值

状况不佳 - Cubit

Getx Flutter 在更新值时抛出空错误

如何获取 android 和 Ios (flutter) 的 CircularProgress 指示器?

如何使按钮被选中?

如何在 ElevatedButton 上设置背景 colored颜色 动画?