无法将参数类型“ JsObject”分配给参数类型“ BuildContext”

时间:2020-09-29 09:35:36

标签: flutter user-interface

我无法浏览页面 我如何在这里使用导航器,基本上我的要求是,每当用户单击列表项时,它都应该导航到其他页面,但是我在列表图块中使用列表图块,所以我无法使用导航器,在使用无状态窗口小部件

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: "Docker Controller",
      home: GradientTab(),
    );
  }
  DefaultTabController GradientTab(){
    return DefaultTabController(length: 2,
        child: Scaffold(
          appBar: AppBar(
            title: Text("Docker Controller"),
            backgroundColor: Colors.black,
            centerTitle: true,
            elevation: 0,
            bottom: TabBar(
              unselectedLabelColor: Colors.blueAccent,
              indicatorSize: TabBarIndicatorSize.label,
              indicator: BoxDecoration(
                  gradient: LinearGradient(colors: [Colors.lightBlue,Colors.blue[900]]),
                borderRadius: BorderRadius.circular(50),
                color: Colors.blueAccent
              ),
              tabs: [
                Tab(
                  child: Align(child: Icon(Icons.local_shipping)),
                ),
                Tab(
                  child: Align(child: Icon(Icons.image)),
                )
              ],

            ),

          ),
          body:TabBarView(children: [
             getcontainerbody(context),
            getimagebody(),
          ]),
        ));
  }
 Widget getcontainerbody(BuildContext context){
    var listview=ListView(

      children: [
        ListTile(
          leading: Image.asset("assets/cogwheels.png",height: 40,width: 40,),
          title: Text("Run The Container"),
          subtitle: Text("Require Field Container Name and Image Name"),
         onTap: () {
            Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => Run()));
          },
          ),
        ListTile(
          leading: Image.asset("assets/stop-sign.png",height: 40,width: 40,),
          title: Text("Stop The Container"),
          subtitle: Text("Require Field Container Name"),
        ),
      ],
    );
    return listview;

1 个答案:

答案 0 :(得分:0)

我认为,在StatelessWidget中启用功能可以使您拥有上下文并进行导航。例如getcontainerbody将变为:

class ContainerBody extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListView(

      children: [
        ListTile(
          leading: Image.asset("assets/cogwheels.png", height: 40, width: 40,),
          title: Text("Run The Container"),
          subtitle: Text("Require Field Container Name and Image Name"),
          onTap: () {
            Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => Run()));
          },
        ),
        ListTile(
          leading: Image.asset("assets/stop-sign.png", height: 40, width: 40,),
          title: Text("Stop The Container"),
          subtitle: Text("Require Field Container Name"),
        ),
      ],
    );
  }
}

请注意,您应使用驼峰式作为函数名称(例如:getcontainerbody-> getContainerBody),请参见dart style documentation