У меня есть следующий код, который работает правильно: import 'package: flutter / material.dart'; import 'screen_curiosities.dart'; import 'screen_movies.dart'; import 'screen_releases.dart'; импорт '../...

0
DiChrist 22 Мар 2021 в 04:05

1 ответ

Лучший ответ

Вы можете скопировать и вставить два полных кода ниже
Вы можете передать selectedIndex и onClicked в BottomMenu
фрагмент кода

bottomNavigationBar: BottomMenu(
          selectedIndex: selectedIndex,
          onClicked: onClicked,
        ));
        
...     
class BottomMenu extends StatelessWidget {
  final selectedIndex;
  ValueChanged<int> onClicked;
  BottomMenu({this.selectedIndex, this.onClicked});

Рабочая демонстрация

enter image description here

Полный код main.dart

import 'package:flutter/material.dart';

import 'bottom.dart';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  int selectedIndex = 0;

  List screens = [ScreenMovies(), ScreenReleases(), ScreenCuriosities()];

  void onClicked(int index) {
    setState(() {
      selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Movies'),
          backgroundColor: Colors.black,
        ),
        body: Center(
          child: screens.elementAt(selectedIndex),
        ),
        //drawer: SideMenu(),
        bottomNavigationBar: BottomMenu(
          selectedIndex: selectedIndex,
          onClicked: onClicked,
        ));
  }
}

class ScreenMovies extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text("ScreenMovies"),
    );
  }
}

class ScreenReleases extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text("ScreenReleases"),
    );
  }
}

class ScreenCuriosities extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text("ScreenCuriosities"),
    );
  }
}

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Home(),
    );
  }
}

Полный код bottom.dart

import 'package:flutter/material.dart';

class BottomMenu extends StatelessWidget {
  final selectedIndex;
  ValueChanged<int> onClicked;
  BottomMenu({this.selectedIndex, this.onClicked});

  @override
  Widget build(BuildContext context) {
    return BottomNavigationBar(
      items: [
        BottomNavigationBarItem(
          icon: Icon(Icons.home),
          label: 'Movies',
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.new_releases),
          label: 'Releases',
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.question_answer),
          label: 'Curiosities',
        )
      ],
      currentIndex: selectedIndex,
      onTap: onClicked,
      selectedItemColor: Colors.red[800],
      backgroundColor: Colors.black,
      unselectedItemColor: Colors.white,
    );
  }
}
1
chunhunghan 22 Мар 2021 в 01:57