提问者:小点点

Flutter从小部件获取导航路径


我制作了一个自定义按钮,我想用它来导航,我把它设置成一个小部件,给它一个标题和一个路径提示,但是它似乎不接受路径作为字符串给出错误。

class NavigationButton extends StatelessWidget {
  NavigationButton({@required this.path, @required this.btnTitle});
  final String path;
  final String btnTitle;


  @override
  Widget build(BuildContext context) {
    return BouncingWidget(
      duration: Duration(milliseconds: 50),
      scaleFactor: 1.5,
      child: GestureDetector(
      onTap: () {
      Navigator.of(context)
          .pushReplacementNamed(path);
      },
      child: Container(
          margin: const EdgeInsets.only(left: 10.0 , right: 10.0 , top : 5.0 , bottom: 5.0 ),
          constraints: BoxConstraints.expand(),
          decoration: BoxDecoration(
              image: DecorationImage(
                  image: AssetImage("assets/home-btn.png"),
                  fit: BoxFit.fill)),
      child: Center(
            child: Text(
              this.btnTitle,
              style: TextStyle(color: Colors.white, fontSize: 20.0),
            ),
          )
        ),
      )
    );
  }
}

然后我试着这么叫它

 NavigationButton(
                          path: "(context, /gjuhaime)",
                          btnTitle: "Gjuha ime"
                      ),

但是我一直发现这个错误:在_WidgetsAppState中找不到路由路由设置的生成器(“/gjuhaime”,null)。


共1个答案

匿名用户

这并不是处理导航的最好方法,但它应该可以工作。

 NavigationButton(
                          path: "/gjuhaime",
                          btnTitle: "Gjuha ime"
                      ),

如果您想使用来自父小部件的上下文,那么您需要将其作为单独的参数传递,而不是在“字符串”中传递。