我的浮动操作按钮没有从一个屏幕更改为另一个屏幕

时间:2021-04-20 15:53:18

标签: flutter screen messaging floating-action-button

我制作了一个带有两个屏幕的 Flutter 应用程序,如下所示:

聊天画面:

import 'package:media_chat_app/constants.dart';
import 'package:flutter/material.dart';
import 'package:media_chat_app/components/drawer.dart';
import 'package:media_chat_app/screens/profile.dart';
import 'components/body.dart';
import 'package:firebase_auth/firebase_auth.dart';

class ChatScreen extends StatefulWidget {
  static String id = 'chat_screen';
  @override
  _ChatScreenState createState() => _ChatScreenState();
}

class _ChatScreenState extends State<ChatScreen> {
  final List<Widget> _children = [
    Body(),
    Profile(),
  ];

  int _selectedIndex = 0;

  final _auth = FirebaseAuth.instance;
  User loggedinUser;

  @override
  void initState() {
    super.initState();
    getCurrentUser();
  }

  void getCurrentUser() {
    try {
      final user = _auth.currentUser;
      if (user != null) {
        loggedinUser = user;
      }
    } catch (e) {
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: buildAppBar(),
      drawer: MainDrawer(
        userEmail: loggedinUser.email,
      ),
      body: _children[_selectedIndex],
      floatingActionButton: FloatingActionButton(
        elevation: 0,
        focusElevation: 0,
        onPressed: () {
          // Add a friend
        },
        backgroundColor: kPrimaryColor,
        child: Icon(
          Icons.person_add_alt_1,
          color: Colors.white,
        ),
      ),
      bottomNavigationBar: buildBottomNavigationBar(),
    );
  }

  BottomNavigationBar buildBottomNavigationBar() {
    return BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      currentIndex: _selectedIndex,
      onTap: (value) {
        setState(() {
          _selectedIndex = value;
        });
      },
      items: [
        BottomNavigationBarItem(icon: Icon(Icons.messenger), label: "Chats"),
        // BottomNavigationBarItem(icon: Icon(Icons.people), label: "Friends"),
        BottomNavigationBarItem(
          icon: CircleAvatar(
            radius: 14,
            backgroundImage: AssetImage("assets/images/user_2.png"),
          ),
          label: "Profile",
        ),
      ],
    );
  }

  AppBar buildAppBar() {
    return AppBar(
      title: Text("Messages"),
      actions: [
        IconButton(
          icon: Icon(Icons.search),
          onPressed: () {},
        ),
      ],
    );
  }
}

个人资料屏幕:

import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:media_chat_app/constants.dart';

class Profile extends StatefulWidget {
  static String id = 'profile';
  @override
  _ProfileState createState() => _ProfileState();
}

class _ProfileState extends State<Profile> {
  final _auth = FirebaseAuth.instance;
  User loggedinUser;

  @override
  void initState() {
    super.initState();
    getCurrentUser();
  }

  void getCurrentUser() {
    try {
      final user = _auth.currentUser;
      if (user != null) {
        loggedinUser = user;
      }
    } catch (e) {
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Theme.of(context).scaffoldBackgroundColor,
      body: SafeArea(
          child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          CircleAvatar(
            radius: 50.0,
            backgroundImage: AssetImage("assets/images/user_2.png"),
          ),
          Text(
            'NATASHA ROMANOFF',
            style: TextStyle(
              fontFamily: 'Pacifico',
              fontSize: 30,
              color: kPrimaryColor,
              fontWeight: FontWeight.bold,
            ),
          ),
          Text(
            'S.H.I.E.L.D. Agent',
            style: TextStyle(
              fontFamily: 'Source Sans Pro',
              color: kPrimaryColor.withOpacity(0.5),
              fontSize: 20.0,
              letterSpacing: 2.5,
              fontWeight: FontWeight.bold,
            ),
          ),
          SizedBox(
            height: 20.0,
            width: 150.0,
            child: Divider(
              color: kPrimaryColor.withOpacity(0.3),
            ),
          ),
          Card(
              margin: EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
              child: ListTile(
                leading: Icon(
                  Icons.phone,
                  color: kPrimaryColor,
                ),
                title: Text(
                  '+91 123 456 789',
                  style: TextStyle(
                    fontFamily: 'Source Sans Pro',
                    fontSize: 20.0,
                  ),
                ),
              )),
          Card(
              margin: EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
              child: ListTile(
                leading: Icon(
                  Icons.email,
                  color: kPrimaryColor,
                ),
                title: Text(
                  loggedinUser.email,
                  style:
                      TextStyle(fontSize: 20.0, fontFamily: 'Source Sans Pro'),
                ),
              ))
        ],
      )),
      floatingActionButton: FloatingActionButton(
        elevation: 0,
        focusElevation: 0,
        onPressed: () {
          // Add a friend
        },
        backgroundColor: kPrimaryColor,
        child: Icon(
          Icons.edit,
          color: Colors.white,
        ),
        // label: Text("Edit"),
      ),
    );
  }
}

即使我定义了两个浮动操作按钮,当我进入配置文件屏幕时,编辑 FAB 不会出现,聊天屏幕中的旧 FAB 仍然存在。我使用了 FloatingActionButton.extended 然后我看到它出现在聊天屏幕的 FAB 下,并且两者一起出现。我不想要那个。我想让 FAB 变成新的。请帮忙。

0 个答案:

没有答案