【问题标题】:The return type '_MainPage' isn't a 'Widget', as required by the closure's context根据闭包上下文的要求,返回类型“_MainPage”不是“Widget”
【发布时间】:2021-05-04 02:27:31
【问题描述】:

我用 bottom_nav_bar 创建页面,但现在我无法在模拟器中打开它,bcs 错误错误:无法将“_MainPage”类型的值分配给“Widget”类型的变量。

这里的问题:'/Main': (BuildContext context) => _MainPage(),

这里代码:

import 'package:flutter/material.dart';

class LogInPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Log In")),
      body: Center(
          child: Row(
        children: <Widget>[
          RaisedButton(
              onPressed: () {
                Navigator.pushNamed(context, '/Main');
              },
              child: Text("Log In")),
          RaisedButton(
              onPressed: () {
                Navigator.pushNamed(context, '/LogIn/Registr');
              },
              child: Text("Registration")),
        ],
      )),
    );
  }
}

class QRCodePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text("QR Scan")),
        body: Center(
          child: RaisedButton(
              onPressed: () {
                Navigator.pushNamed(context, '/Main');
              },
              child: Text("Scan")),
        ));
  }
}

class RegistrPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text("Registration")),
        body: Center(
          child: RaisedButton(
              onPressed: () {
                Navigator.pushNamed(context, '/Main');
              },
              child: Text("Log In")),
        ));
  }
}

class MyApp extends StatefulWidget {
  @override
  _MainPage createState() => _MainPage();
}

class _MainPage extends State<MyApp> {
  int _currentIndex = 0;
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(title: Text("Main")),
      body: Container(),
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(
              icon: Icon(Icons.home), backgroundColor: Colors.blue),
          BottomNavigationBarItem(
              icon: Icon(Icons.qr_code), backgroundColor: Colors.blue),
          BottomNavigationBarItem(
              icon: Icon(Icons.restaurant_menu), backgroundColor: Colors.blue),
          BottomNavigationBarItem(
              icon: Icon(Icons.shopping_basket), backgroundColor: Colors.blue),
        ],
      onTap: (index){
          setState(() {
            _currentIndex = index;
          });
      },
      ),
    );
  }
}

void main()
{
  runApp(MaterialApp(
    initialRoute: '/LogIn',
    routes: {
      '/LogIn': (BuildContext context) => LogInPage(),
      '/QRScan': (BuildContext context) => QRCodePage(),
      '/LogIn/Registr': (BuildContext context) => RegistrPage(),
      '/Main': (BuildContext context) => _MainPage(),
    },
  ));
}

【问题讨论】:

  • 好吧,主页不是小部件,请使用以 StatelessWidget 或 stateFul 小部件结尾的名称。
  • 如果我设置了 StatelessWidget 或 StatefulWidget,那么在 bottom_nav_bar 中的图标之间导航将不起作用,因为我们在 StatelessWidget 或 StatefulWidge 中没有 setState 方法

标签: flutter dart


【解决方案1】:

不应该是MyApp 而不是_MainPage 吗? _MainPage 不是 Widget 而是 State。

void main() {
  runApp(MaterialApp(
    initialRoute: '/Main',
    routes: {
      '/LogIn': (BuildContext context) => LogInPage(),
      '/QRScan': (BuildContext context) => QRCodePage(),
      '/LogIn/Registr': (BuildContext context) => RegistrPage(),
      '/Main': (BuildContext context) => MyApp(),
    },
  ));
}

您可能应该将 MyApp 重命名为 MainPage 以更清晰。

此外,您必须定义每个BottomNavigationBarItemlabeltitle

items: [
  BottomNavigationBarItem(
    label: 'Home',
    icon: Icon(Icons.home),
    backgroundColor: Colors.blue
  ),
  ...
],

【讨论】:

    猜你喜欢
    • 2021-10-29
    • 2020-10-14
    • 2021-10-30
    • 2021-03-08
    • 1970-01-01
    • 2022-01-12
    • 2021-08-26
    • 2021-06-27
    • 2021-07-08
    相关资源
    最近更新 更多