如何调用MyApp()中包含的另一个文件或main.dart文件的include函数,>

问题描述 投票:0回答:1

main.dart文件源代码👇但是我想在bottomNavigationBar();中调用bottom_navbar.dart函数>>

import 'package:flutter/material.dart';
import 'bottom_navbar.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "BookKeep",
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Color(0xFF212121),
          title: Center(
            child: Text("BookKeep"),
          ),
          leading: GestureDetector(
            onTap: (){},
            child: Icon(
              Icons.menu,
            ),
          ),
          actions: <Widget>[
            Padding(
              padding: EdgeInsets.only(right: 20.0),
              child: GestureDetector(
                onTap: (){},
                child: Icon(
                  Icons.search
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

[bottom_navbar.dart文件👇

import 'package:flutter/material.dart';

class bottomNavigationBar extends StatefulWidget {
  @override
  _bottomNavigationBarState createState() => _bottomNavigationBarState();
}

class _bottomNavigationBarState extends State<bottomNavigationBar> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold (
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(
            icon: new Icon(Icons.home),
            title: new Text("Home")
          ),
          BottomNavigationBarItem(
            icon: new Icon(Icons.favorite,),
            title: new Text("Favourites")
          ),
        ],
      ),
    );
  }
}

在尝试过类似这样的东西后,👇enter image description here

然后它正在执行这样的错误👇enter image description here

main.dart文件源代码👇但我想调用bottomNavigationBar();在bottom_navbar.dart内的函数import'package:flutter / material.dart';导入'bottom_navbar.dart'; void main(){...

android flutter dart flutter-layout flutter-web
1个回答
0
投票

这是main.dart:

import 'package:flutter/material.dart';
import 'bottom_navbar.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "BookKeep",
      home: Scaffold(
        bottomNavigationBar: BottomNavBar(),
        appBar: AppBar(
          backgroundColor: Color(0xFF212121),
          title: Center(
            child: Text("BookKeep"),
          ),
          leading: GestureDetector(
            onTap: (){},
            child: Icon(
              Icons.menu,
            ),
          ),
          actions: <Widget>[
            Padding(
              padding: EdgeInsets.only(right: 20.0),
              child: GestureDetector(
                onTap: (){},
                child: Icon(
                  Icons.search
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.