Ausgabe
Ich habe einen Verifizierungsbenutzer erstellt und navigiere ihn zum Startbildschirm. Dann möchte ich BottomNavigationBar
nach der Anmeldung in der App anzeigen, also habe ich BottomNavigationBar
ein aufrufbares Widget erstellt, damit ich es auf jeder Seite aufrufen kann. Ich möchte nicht verwenden CurvedNavigationBar
und versuchen, BottomNavigationBar
ohne Routen zu verwenden. Soll ich Routen verwenden?
Dies ist mein Code, der mit main.dart funktioniert, aber nicht funktioniert, wenn ich ihn in ein anderes Widget ändere. Ich bekomme die Items nicht definiert und weiß nicht warum.
// import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
// import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_core/firebase_core.dart';
import '../page/group_screen.dart';
import '../page/home_screen.dart';
import '../page/post_screen.dart';
import '../page/profile_screen.dart';
import '../page/search_screen.dart';
class BottomNavigationBar extends StatefulWidget {
@override
State<BottomNavigationBar> createState() => _BottomNavigationBarState();
}
class _BottomNavigationBarState extends State<BottomNavigationBar> {
int currentIndex = 0;
final screens = [
HomePage(),
SearchPage(),
Post(),
Group(),
Profile(),
];
@override
Widget build(BuildContext context) => Scaffold(
body: IndexedStack(index: currentIndex, children: screens),
bottomNavigationBar: BottomNavigationBar(
fixedColor: Colors.black,
showSelectedLabels: false,
showUnselectedLabels: false,
items: [
BottomNavigationBarItem(
label: 'Home',
icon: Icon(Icons.home),
backgroundColor: Colors.grey,
),
BottomNavigationBarItem(
backgroundColor: Colors.grey,
label: 'Search',
icon: Icon(Icons.settings),
),
BottomNavigationBarItem(
backgroundColor: Colors.grey,
label: 'Post',
icon: Icon(Icons.post_add),
),
BottomNavigationBarItem(
backgroundColor: Colors.grey,
label: 'Group',
icon: Icon(Icons.group),
),
BottomNavigationBarItem(
backgroundColor: Colors.grey,
label: 'Profile',
icon: Icon(Icons.person),
),
],
currentIndex: currentIndex,
onTap: (int index) {
setState(() {
currentIndex = index;
});
},
),
);
}
Hier die Fehlermeldungen:
Lösung
Das Problem ist, dass Sie das Widget benennen, das BottomNavigationBar
das von Flutter bereitgestellte Standard-Widget (gleicher Name) ist.
Ändern Sie Ihren Klassennamen in etwas anderes wie
class MyBottomNavigationBar extends StatefulWidget { //this class name
@override
State<MyBottomNavigationBar> createState() => _MyBottomNavigationBarState();
}
class _MyBottomNavigationBarState extends State<MyBottomNavigationBar> {
int currentIndex = 0;
final screens = [];
@override
Widget build(BuildContext context) => Scaffold(
bottomNavigationBar: BottomNavigationBar(
fixedColor: Colors.black,
showSelectedLabels: false,
showUnselectedLabels: false,
Beantwortet von – Yeasin Sheikh
Antwort geprüft von – Mary Flores (FixError Volunteer)