【问题标题】:Navigator.pushNamed returns black screenNavigator.pushNamed 返回黑屏
【发布时间】:2020-06-14 08:12:18
【问题描述】:

我知道问题的根源,但我不知道如何解决。 当我单击转到另一个页面的按钮时,屏幕变黑了。

这里是调试控制台:

[38;5;248m════════ Exception caught by scheduler library ═════════════════════════════════[39;49m
There are multiple heroes that share the same tag within a subtree.
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m

这是主页:


class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text('Compilación de Flutter'),
    ),
  body:Stack(children: <Widget>[
   _fondoApp(),
   _listView(context),

  ],) ,
  floatingActionButton: Container(),
);
}

Widget _texto(){
return Text('Hola, bienvenido a mi compilacion de apps que yo mismo hice por un curso en flutter por internet.', style: TextStyle(
  color: Colors.white, fontSize: 30
),);
}
Widget _fondoApp(){
return Container(
  height: double.infinity,
  width: double.infinity,
  color: Color.fromRGBO(67, 107, 116, 1),
  );
}
Widget _listView(BuildContext context){
return ListView(
  padding: EdgeInsets.symmetric(horizontal: 10, vertical:15),
  children: <Widget>[
  _texto(),
  SizedBox(height:20),
  _targetadeApps(context, 'app1', Colors.green, "1", "primera", "una app contadora")
],);
}
Widget _targetadeApps(BuildContext context, String route, Color color, String no, String text, String description){
return Card(
  child: Column(
    children:<Widget>[
      ListTile(
        leading: Icon(Icons.check_circle_outline, color: color,),
        title: Text("App $no", style: TextStyle(fontSize: 20.0,),),
        subtitle: Text("Esta fue la $text app que cree trata sobre $description", style: TextStyle(fontSize: 18.0,),),
        ),
        FlatButton(child: Text("Entrar", style: TextStyle(fontSize: 18.0,),), onPressed: (){

          Navigator.pushNamed(context, route);
        },)
    ]
  ),
);
}
} 

这里是导航器需要去的页面


class ContadorPage extends StatefulWidget {
 @override 
 createState() {
  return _ContadorPageState();
}

}

class _ContadorPageState extends State<ContadorPage> {

final _estiloTexto=new TextStyle( fontSize: 27);
 int _conteo = 0;

@override
Widget build(BuildContext context) {
   return Scaffold(
      appBar: AppBar(
        title: Text('AppContadora(app1)'),
        centerTitle: true,
      ) ,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('Contador:', style: _estiloTexto,),
            Text('$_conteo', style: _estiloTexto),
          ],
          ) ,
        ),
      floatingActionButton: _crearbotones()

   );
 }
 Widget _crearbotones() {

  return Stack(
      children:<Widget>[ Row(
      mainAxisAlignment: MainAxisAlignment.end,
      children: <Widget>[
        SizedBox(width: 30.0,),
        FloatingActionButton(
          child: Icon(Icons.remove), 
        onPressed: () {
         _sustraer();
        },

        ),
        Expanded(
          child: SizedBox(),
          ),
              FloatingActionButton(
          child: Icon(Icons.exposure_zero), 
        onPressed: () {
         _reset();
        },

        ),
        Expanded(
          child: SizedBox(),
          ),
              FloatingActionButton(
          child: Icon(Icons.add), 
        onPressed: () {
        _agregar();
        },
        )
        ],

    ),
      ]);
 }
 void _agregar(){
   setState(() {
      _conteo++;
   });
 }

void _sustraer(){
  setState(() {
    _conteo--;
  });
}

void _reset(){
setState(() {
  _conteo = _conteo -_conteo;
});


      }




}

当我退出 2 个按钮时,导航器工作正常。 有人知道如何在不退出 2 个按钮的情况下使其工作吗?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    您需要为每个FloatingActionButton 设置herTag

    代码sn-p

    FloatingActionButton(
                    heroTag: "1",
                    child: Icon(Icons.remove),
    ...     
    FloatingActionButton(
                    heroTag: "2",
                    child: Icon(Icons.exposure_zero),
    ...         
    FloatingActionButton(
                    heroTag: "3",
                    child: Icon(Icons.add),
    

    工作演示

    完整代码

    import 'package:flutter/material.dart';
    
    class HomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Compilación de Flutter'),
          ),
          body: Stack(
            children: <Widget>[
              _fondoApp(),
              _listView(context),
            ],
          ),
          floatingActionButton: Container(),
        );
      }
    
      Widget _texto() {
        return Text(
          'Hola, bienvenido a mi compilacion de apps que yo mismo hice por un curso en flutter por internet.',
          style: TextStyle(color: Colors.white, fontSize: 30),
        );
      }
    
      Widget _fondoApp() {
        return Container(
          height: double.infinity,
          width: double.infinity,
          color: Color.fromRGBO(67, 107, 116, 1),
        );
      }
    
      Widget _listView(BuildContext context) {
        return ListView(
          padding: EdgeInsets.symmetric(horizontal: 10, vertical: 15),
          children: <Widget>[
            _texto(),
            SizedBox(height: 20),
            _targetadeApps(
                context, 'app1', Colors.green, "1", "primera", "una app contadora")
          ],
        );
      }
    
      Widget _targetadeApps(BuildContext context, String route, Color color,
          String no, String text, String description) {
        return Card(
          child: Column(children: <Widget>[
            ListTile(
              leading: Icon(
                Icons.check_circle_outline,
                color: color,
              ),
              title: Text(
                "App $no",
                style: TextStyle(
                  fontSize: 20.0,
                ),
              ),
              subtitle: Text(
                "Esta fue la $text app que cree trata sobre $description",
                style: TextStyle(
                  fontSize: 18.0,
                ),
              ),
            ),
            FlatButton(
              child: Text(
                "Entrar",
                style: TextStyle(
                  fontSize: 18.0,
                ),
              ),
              onPressed: () {
                print(route);
                Navigator.pushNamed(context, route);
              },
            )
          ]),
        );
      }
    }
    
    class ContadorPage extends StatefulWidget {
      @override
      createState() {
        return _ContadorPageState();
      }
    }
    
    class _ContadorPageState extends State<ContadorPage> {
      final _estiloTexto = new TextStyle(fontSize: 27);
      int _conteo = 0;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Text('AppContadora(app1)'),
              centerTitle: true,
            ),
            body: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text(
                    'Contador:',
                    style: _estiloTexto,
                  ),
                  Text('$_conteo', style: _estiloTexto),
                ],
              ),
            ),
            floatingActionButton: _crearbotones());
      }
    
      Widget _crearbotones() {
        return Stack(children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: <Widget>[
              SizedBox(
                width: 30.0,
              ),
              FloatingActionButton(
                heroTag: "1",
                child: Icon(Icons.remove),
                onPressed: () {
                  _sustraer();
                },
              ),
              Expanded(
                child: SizedBox(),
              ),
              FloatingActionButton(
                heroTag: "2",
                child: Icon(Icons.exposure_zero),
                onPressed: () {
                  _reset();
                },
              ),
              Expanded(
                child: SizedBox(),
              ),
              FloatingActionButton(
                heroTag: "3",
                child: Icon(Icons.add),
                onPressed: () {
                  _agregar();
                },
              )
            ],
          ),
        ]);
      }
    
      void _agregar() {
        setState(() {
          _conteo++;
        });
      }
    
      void _sustraer() {
        setState(() {
          _conteo--;
        });
      }
    
      void _reset() {
        setState(() {
          _conteo = _conteo - _conteo;
        });
      }
    }
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
            title: 'Flutter Demo',
            theme: ThemeData(
              primarySwatch: Colors.blue,
            ),
            initialRoute: '/',
            routes: {
              '/': (context) => HomePage(),
              'app1': (context) => ContadorPage(),
            });
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-18
      • 2013-09-24
      • 2021-07-31
      • 2019-05-12
      • 2019-10-01
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多