【问题标题】:Flutter get a navigation path from widgetFlutter 从小部件获取导航路径
【发布时间】:2021-01-24 15:56:26
【问题描述】:

我已经制作了一个我想用于导航的自定义按钮,我已经设置了它,以便您可以将它作为一个小部件调用,并为它提供一个标题和一个应该引导的路径,但它没有似乎接受路径作为给出错误的字符串。

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 中找不到路由 RouteSettings("/gjuhaime", null) 的生成器。

【问题讨论】:

    标签: java android flutter dart mobile


    【解决方案1】:

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

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

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

    【讨论】:

    • 我应该用什么来代替字符串?
    • 如果您想使用上下文,则将其作为上下文传递。但是,据我所知,它适用于我上面写的示例而不传递上下文。
    猜你喜欢
    • 2020-09-14
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 2021-06-19
    • 2018-10-13
    • 2022-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多