dart showDialog不会显示

ffx8fchx  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(105)

我试图创建一个托管用户配置文件。我已经成功地检索用户信息从消防商店。当我使用Pixel 3a模拟器运行代码时,当用户按下修改其数据时应该显示对话框的按钮在我尝试修改用户配置文件时没有响应。Flutter SDK 2.7.0是我正在使用的。

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:image_picker/image_picker.dart';
import 'package:jomasak/views/screens/auth/welcome_page.dart';
import 'package:jomasak/views/utils/AppColor.dart';
import 'package:jomasak/views/widgets/user_info_tile.dart';

class ProfilePage extends StatelessWidget {
  final currentUser = FirebaseAuth.instance.currentUser;
  final userCollection = FirebaseFirestore.instance.collection('users');
  String defaultImageURL =
      'https://pixabay.com/get/g066bc81cdaafbd755a118bbffa3223a1bd405dc2b5e972b603bf51a2bac5ad9c328e204c50642e1cb2b8c5574e9f5126aa31ca57206186d4ac7e15b8403ff8a61ae4a75edb280f4e1a9b572606dc6cf0_1280.png';
  String selectedFileName = '';
  XFile file;

  Future<void> editField(BuildContext context, String field) async {
    String newValue = "";
    TextEditingController textEditingController = TextEditingController();

    await showDialog(
      context: context,
      builder: (context) {
        return Dialog(
          backgroundColor: Colors.grey[900],
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Padding(
                padding: const EdgeInsets.all(16.0),
                child: Text(
                  "Edit $field",
                  style: TextStyle(color: Colors.white),
                ),
              ),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 16.0),
                child: TextField(
                  controller: textEditingController,
                  autofocus: true,
                  style: TextStyle(color: Colors.white),
                  decoration: InputDecoration(
                    hintText: "Enter new $field",
                    hintStyle: TextStyle(color: Colors.grey),
                  ),
                  onChanged: (value) {
                    newValue = value;
                  },
                ),
              ),
              ButtonBar(
                children: [
                  TextButton(
                    onPressed: () {
                      Navigator.pop(context);
                    },
                    child: Text(
                      'Cancel',
                      style: TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                  TextButton(
                    onPressed: () {
                      newValue = textEditingController.text;
                      Navigator.of(context).pop(newValue);
                    },
                    child: Text(
                      'Save',
                      style: TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        );
      },
    );

    // Update the user's document in Firestore
    if (newValue.trim().isNotEmpty) {
      await userCollection.doc(currentUser.email).update({field: newValue});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        brightness: Brightness.dark,
        backgroundColor: AppColor.primary,
        elevation: 0,
        centerTitle: true,
        title: const Text(
          'My Profile',
          style: TextStyle(
            fontFamily: 'inter',
            fontWeight: FontWeight.w400,
            fontSize: 16,
          ),
        ),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios, color: Colors.white),
          onPressed: () {
            Navigator.of(context).pop();
          },
        ),
        actions: [
          TextButton(
            onPressed: () {
              FirebaseAuth.instance.signOut();
              print("Signed Out");
              Navigator.of(context).pushAndRemoveUntil(
                MaterialPageRoute(builder: (context) => WelcomePage()),
                (Route<dynamic> route) => false,
              );
            },
            child: Icon(
              Icons.logout,
              color: Colors.white,
              size: 20,
            ),
            style: TextButton.styleFrom(
              primary: Colors.white,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(100),
              ),
            ),
          ),
        ],
      ),
      body: StreamBuilder<DocumentSnapshot>(
        stream: FirebaseFirestore.instance
            .collection("users")
            .doc(currentUser.email)
            .snapshots(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            final userData = Map<String, dynamic>.from(snapshot.data.data());
            return ListView(
              shrinkWrap: true,
              physics: BouncingScrollPhysics(),
              children: [
                // Section 1 - Profile Picture Wrapper
                Container(
                  color: AppColor.primary,
                  padding: EdgeInsets.symmetric(vertical: 24),
                  child: GestureDetector(
                    onTap: () {
                      print('Code to open file manager');
                    },
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Container(
                          width: 130,
                          height: 130,
                          margin: EdgeInsets.only(bottom: 15),
                          decoration: BoxDecoration(
                            color: Colors.grey,
                            borderRadius: BorderRadius.circular(100),
                            // Profile Picture
                            image: DecorationImage(
                              image: AssetImage('assets/images/profile.jpeg'),
                              fit: BoxFit.cover,
                            ),
                          ),
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text(
                              'Change Profile Picture',
                              style: TextStyle(
                                fontFamily: 'inter',
                                fontWeight: FontWeight.w600,
                                color: Colors.white,
                              ),
                            ),
                            SizedBox(width: 8),
                            SvgPicture.asset(
                              'assets/icons/camera.svg',
                              color: Colors.white,
                            ),
                          ],
                        )
                      ],
                    ),
                  ),
                ),

                // Section 2 - User Info Wrapper
                Container(
                  margin: EdgeInsets.only(top: 24),
                  width: MediaQuery.of(context).size.width,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      UserInfoTile(
                        margin: EdgeInsets.only(bottom: 16),
                        label: 'Email',
                        value: userData['email'],
                        onPressed: () => editField(context, 'email'),
                      ),
                      UserInfoTile(
                        margin: EdgeInsets.only(bottom: 16),
                        label: 'Full Name',
                        value: userData['fullname'],
                        onPressed: () => editField(context, 'fullname'),
                      ),
                      UserInfoTile(
                        margin: EdgeInsets.only(bottom: 16),
                        label: 'Username',
                        value: userData['username'],
                        onPressed: () => editField(context, 'username'),
                      ),
                      UserInfoTile(
                        margin: EdgeInsets.only(bottom: 16),
                        label: 'Bio',
                        value: userData['bio'],
                        onPressed: () => editField(context, 'bio'),
                        valueBackground: AppColor.secondary,
                      ),
                    ],
                  ),
                ),
              ],
            );
          } else if (snapshot.hasError) {
            return Center(
              child: Text('Error ${snapshot.error}'),
            );
          }
          return Center(
            child: CircularProgressIndicator(),
          );
        },
      ),
    );
  }
}

我想编辑用户配置文件和更新的细节内消防商店

gtlvzcf8

gtlvzcf81#

Screenshot
我试了你的代码,对话出现了。可能问题出在UserInfoTile。尝试检查您正在调用UserInfoTile类中的onPressed函数。如果没有任何工作,然后尝试重新启动应用程序。

flutter clean 
 flutter pub get
 flutter run

相关问题