Как мне добавить bottomNavigationBar на один экран, но не на другие? - PullRequest
0 голосов
/ 04 августа 2020

У меня есть 3 экрана для моего приложения: экран ввода, вход в систему и главный экран. Я хочу, чтобы панель навигации была только на главном экране. Какой виджет я должен вернуть на своей странице HomeScreen() или что мне нужно на моей странице main.dart?

Вот код для main.dart:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Doctor Consultation App',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
          primarySwatch: Colors.blue,
          scaffoldBackgroundColor: Colors.white,
          visualDensity: VisualDensity.adaptivePlatformDensity),
      home: EnterScreen(),
    );
  }
}

home_screen.dart:

class MainScreen extends StatefulWidget {
  @override
  _MainScreenState createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      
    );
  }
}

bottom_nav_bar.dart:

class BottomNavScreen extends StatefulWidget {
  @override
  _BottomNavScreenState createState() => _BottomNavScreenState();
}

class _BottomNavScreenState extends State<BottomNavScreen> {
  final List _screens = [
    MainScreen(),
    Scaffold(),
    Scaffold(),
    Scaffold(),
  ];

  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _screens[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentIndex,
          onTap: (index) => setState(() => _currentIndex = index),
          type: BottomNavigationBarType.fixed,
          backgroundColor: Colors.white,
          showSelectedLabels: false,
          showUnselectedLabels: false,
          selectedItemColor: Palette.primaryRed,
          unselectedItemColor: Colors.grey,
          elevation: 0.0,
          items: [
            Icons.home,
            Icons.chat_bubble,
            Icons.calendar_today,
            Icons.person
          ]
              .asMap()
              .map((key, value) => MapEntry(
                  key,
                  BottomNavigationBarItem(
                      title: Text(''),
                      icon: Container(
                          padding: const EdgeInsets.symmetric(
                              vertical: 6.0, horizontal: 16.0),
                          decoration: BoxDecoration(
                              color: _currentIndex == key
                                  ? Palette.primaryRed
                                  : Colors.transparent,
                              borderRadius: BorderRadius.circular(20.0)),
                          child: Icon(value)))))
              .values
              .toList()),
    );
  }
}

Я уже пробовал делать bottomNavigationBar: BottomNavScreen(), на home_screen.dart, но получаю массу ошибок из MaterialApp виджет.

1 Ответ

1 голос
/ 04 августа 2020

Измените build в ButtomNavScreen на это:

@override
  Widget build(BuildContext context) {
    return BottomNavigationBar(
          currentIndex: _currentIndex,
...

И измените build из MainScreen на что-то вроде этого:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: BottomNavScreen(),
      body: SafeArea(
        child: Center(
          child: Container(
            width: 200,
            height: 100,
            color: Colors.redAccent,
            child: Center(child: Text("sample text")),
          ),
        )
      ),
    );
  }
...