【问题标题】:Back Button duplicates, appearing without configuration FlutterBack Button 重复,出现没有配置 Flutter
【发布时间】:2021-02-08 17:27:25
【问题描述】:

我正在用 Flutter 开发一个应用程序,我在某个页面上添加了一个后退按钮,现在这个按钮显示在代码中的任何地方。如何确保此按钮仅显示在我想要的页面中?按下后退按钮后不在整个代码中?我正在发布主页面和创建后退按钮的页面,这是我添加后退按钮的唯一类。

代码

import 'package:MECTS_Mobile_Application/Home/routes.dart';
import 'package:flutter/material.dart';

class Home extends StatefulWidget{
@override
State<StatefulWidget> createState() => new _Home();
}

class _Home extends State<Home> {
 var services = [
'Track Routes',
'View Vehicles',
'View Reports',
 ];

 var images = [
'imageapp/cardone.png',
'imageapp/cardtwo.png',
'imageapp/cardthree.png',
];

@override
 Widget build(BuildContext context) {

 void move(){
   Navigator.of(context).pushReplacementNamed(Routes.routeName);
 }

return GridView.builder(
  padding: const EdgeInsets.symmetric(vertical:80.0),
  itemCount: 3,
  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
    crossAxisCount: 1,
    childAspectRatio: MediaQuery.of(context).size.width/(MediaQuery.of(context).size.height/6),
  ),
  itemBuilder: (BuildContext context, int index){
    return Card(
      child: Column(children: <Widget> [
        SizedBox(
          height: 10,
          width: 20,
        ),
          Image.asset(images[index],height:65,width: 750,
          ),
          Padding(
            padding: EdgeInsets.all(10),
            child: GestureDetector(
            onTap: move,
            child: Text(services[index], 
            style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), 
            textAlign: TextAlign.center,
            )
            )
            ),
          ],
         ),
        );
      },
   );
 }
}

主页:

      import 'package:MECTS_Mobile_Application/Authentication/login_page.dart';
      import 'package:MECTS_Mobile_Application/Authentication/register_page.dart';
      import 'package:MECTS_Mobile_Application/Home/reports.dart';
      import 'package:MECTS_Mobile_Application/Home/splashscreen.dart';
      import 'package:MECTS_Mobile_Application/Home/vehicles.dart';
      import 'Home/routes.dart';
      import 'root.dart';
      import 'package:flutter/material.dart';


       void main() {
     runApp(new MyApp());
     }

     class MyApp extends StatelessWidget {
     @override
     Widget build(BuildContext context) {
     return new MaterialApp(
      title: 'MECTS',
      theme: new ThemeData(primarySwatch: Colors.cyan),
      home: new SplashMECTSScreen(),
      routes: {
    LoginPage.routeName: (ctx) => LoginPage(),
    RegisterPage.routeName: (ctx) => RegisterPage(),
    Routes.routeName: (ctx) => Routes(),
    Reports.routeName: (ctx) => Reports(),
    Vehicles.routeName: (ctx) => Vehicles(),
    RootPage.routeName: (ctx) => RootPage(),
    },
   );
  }
 }

屏幕

【问题讨论】:

  • 可以添加按钮的代码
  • 我的猜测是您使用Navigator.push() 从跟踪路线页面返回到主页而不是Navigator.pop()Navigator.push() 会自动为您创建一个指向上一页的后退按钮。点击后退按钮会发生什么?
  • 谢谢,Navigator.push() 成功了

标签: flutter visual-studio-2010 mobile flutter-layout back


【解决方案1】:

解决我的问题的是使用:

Navigator.of(context).popAndPushNamed(RootPage.routeName);

【讨论】:

    猜你喜欢
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 2022-12-07
    • 2021-01-10
    • 2013-01-02
    • 2022-12-26
    • 1970-01-01
    相关资源
    最近更新 更多