在_WidgetsAppState中找不到路由RouteSettings(“ SecondScreens”,null)的生成器

时间:2020-10-22 10:45:13

标签: flutter

我试图将多个FloatingActionButton移至第二页,但是在按任意FloatingActionButton弹出窗口后,我面临以下问题:在以下位置找不到路由RouteSettings(“ SecondScreens”,null)的生成器_WidgetsAppState。

问题出在哪里,如果有人知道它对我有帮助,如何解决。

完整代码


import 'package:flutter/material.dart';
import 'dart:math' as math;

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  State createState() => new MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
  AnimationController _controller;

  static const List<IconData> icons = const [
    Icons.sms,
    Icons.mail,
    Icons.phone
  ];

  @override
  void initState() {
    _controller = new AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 500),
    );
  }

  Widget build(BuildContext context) {
    Color backgroundColor = Theme.of(context).cardColor;
    Color foregroundColor = Theme.of(context).accentColor;
    return new Scaffold(
      appBar: new AppBar(title: new Text('Speed Dial Example')),
      floatingActionButton: new Column(
        mainAxisSize: MainAxisSize.min,
        children: new List.generate(icons.length, (int index) {
          Widget child = new Container(
            height: 70.0,
            width: 56.0,
            alignment: FractionalOffset.topCenter,
            child: new ScaleTransition(
              scale: new CurvedAnimation(
                parent: _controller,
                curve: new Interval(0.0, 1.0 - index / icons.length / 2.0,
                    curve: Curves.easeOut),
              ),
              child:
              fabWidget(context, index, backgroundColor, foregroundColor),
            ),
          );
          return child;
        }).toList()
          ..add(
            new FloatingActionButton(
              heroTag: null,
              child: new AnimatedBuilder(
                animation: _controller,
                builder: (BuildContext context, Widget child) {
                  return new Transform(
                    transform: new Matrix4.rotationZ(
                        _controller.value * 0.5 * math.pi),
                    alignment: FractionalOffset.center,
                    child: new Icon(
                        _controller.isDismissed ? Icons.share : Icons.close),
                  );
                },
              ),
              onPressed: () {
                if (_controller.isDismissed) {
                  _controller.forward();
                } else {
                  _controller.reverse();
                }
              },
            ),
          ),
      ),
    );
  }

  Widget fabWidget(BuildContext context, int index, Color backgroundColor,
      Color foregroundColor) {
    String route = "";
    switch (index) {
      case 0:
        route = "SecondScreens";
        break;
      case 1:
        route = "thirdScreen";
        break;
      default:
    }

    return FloatingActionButton(
      heroTag: null,
      backgroundColor: backgroundColor,
      mini: true,
      child: new Icon(icons[index], color: foregroundColor),
      onPressed: () {
        Navigator.pushNamed(context, route);
      },
    );
  }
}


1 个答案:

答案 0 :(得分:0)

您正在使用Navigator.pushNamed,它要求将您作为第二个参数放置的路由在“应用程序路由”属性中进行定义,如下所示:

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) => FirstScreen(),
    // When navigating to the "/second" route, build the SecondScreen widget.
    '/second': (context) => SecondScreen(),
  },
);

每个路由都定义为MapEntry,第一部分是路由名称,因此您称为“ routeName”的参数,第二个参数是Widget构造函数,即您希望该路由对应的页面。

在您的情况下,您将添加以下内容:

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) => MyHomePage(),
    // When navigating to the "/second" route, build the SecondScreens widget.
    '/second': (context) => SecondScreens(),
    // When navigating to the "/third" route, build the thirdScreens widget.
    '/third': (context) => SecondScreens(),
    ///All the other screens in your app.

  },
);

然后可以将fabWidget函数更改为此:

Widget fabWidget(BuildContext context, int index, Color backgroundColor,
      Color foregroundColor) {
    String route = "";
    switch (index) {
      case 0:
        route = "/second";
        break;
      case 1:
        route = "/third";
        break;
      default:
    }

    return FloatingActionButton(
      heroTag: null,
      backgroundColor: backgroundColor,
      mini: true,
      child: new Icon(icons[index], color: foregroundColor),
      onPressed: () {
        Navigator.pushNamed(context, route);
      },
    );
  }