首页 > 解决方案 > 根据闭包上下文的要求,返回类型“_MainPage”不是“Widget”

问题描述

我使用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(),
    },
  ));
}

标签: flutterdart

解决方案


不应该MyApp代替_MainPage吗?_MainPage不是小部件而是状态。

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

为了更清楚起见,您可能应该重命名MyAppMainPage

此外,您必须定义每个的labelor :titleBottomNavigationBarItem

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

推荐阅读