Search code examples
flutterdarttextwidgetspace

Extra Spacing Inside My Text Widget In Flutter Application


I have created a mobile application and I was using a font family called Baloo Paaji 2 from google and I have faced an issue of extra spacing created between my 2 text widgets. Below you can see the image of the view.

enter image description here

The text I am talking about is the Welcome! Lakshya Jain. The space between the 2 is way too much. There is no SizedBox or Padding added to the text widget. I tried using 2 different methods to see if the method was the problem. The 2 different methods are shown below.

Method 1

                      Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            "Welcome !",
                            style: TextStyle(
                              color: Colors.white,
                              fontFamily: "BalooPaaji",
                              fontSize: 18.0,
                              fontWeight: FontWeight.w600,
                            ),
                          ),
                          Text(
                            userData.fullName,
                            style: TextStyle(
                              color: Colors.white,
                              fontFamily: "BalooPaaji",
                              fontSize: 24.0,
                              fontWeight: FontWeight.w900,
                            ),
                          ),
                        ],
                      ),

Method 2

                      Text.rich(
                        TextSpan(
                          text: "Welcome !\n",
                          style: TextStyle(
                            color: Colors.white,
                            fontFamily: "BalooPaaji",
                            fontSize: 18.0,
                            fontWeight: FontWeight.w600,
                          ),
                          children: [
                            TextSpan(
                              text: userData.fullName,
                              style: TextStyle(
                                color: Colors.white,
                                fontFamily: "BalooPaaji",
                                fontSize: 24.0,
                                fontWeight: FontWeight.w900,
                              ),
                            ),
                          ],
                        ),
                      ),

Now I used another method which fixed this issue but created another one.

The Method 3 Screenshot is as follows. enter image description here

Now the spacing issue is fixed but it created as the text has moved down a little. I want it to be center without the huge gap.

The code for this method is as followed.

                      Stack(
                        alignment: Alignment.topLeft,
                        clipBehavior: Clip.none,
                        children: [
                          Text(
                            "Welcome !",
                            style: TextStyle(
                              color: Colors.white,
                              fontFamily: "BalooPaaji",
                              fontSize: 18.0,
                              fontWeight: FontWeight.w600,
                            ),
                          ),
                          Positioned(
                            top: 20.0,
                            child: Text(
                              userData.fullName,
                              style: TextStyle(
                                color: Colors.white,
                                fontFamily: "BalooPaaji",
                                fontSize: 24.0,
                                fontWeight: FontWeight.w900,
                              ),
                            ),
                          ),
                        ],
                      ),

The full code for the whole header is as followed

class HomeHeader extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // Get User UID
    final user = Provider.of<MyAppUser>(context);

    return Stack(
      clipBehavior: Clip.none,
      children: [
        Container(
          padding: EdgeInsets.symmetric(horizontal: 15.0),
          width: MediaQuery.of(context).size.width,
          height: 230.0,
          decoration: BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [
                Color.fromRGBO(255, 18, 54, 1.0),
                Color.fromRGBO(255, 164, 29, 1.0),
              ],
            ),
            borderRadius: BorderRadius.only(
              bottomRight: Radius.circular(59.0),
            ),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              StreamBuilder(
                stream: DatabaseService(uid: user.uid).userData,
                builder: (context, snapshot) {
                  UserDataCustomer userData = snapshot.data;
                  return Row(
                    children: [
                      ClipOval(
                        child: CachedNetworkImage(
                          height: 70,
                          width: 70,
                          imageUrl: userData.profilePicture,
                        ),
                      ),
                      SizedBox(
                        width: 10.0,
                      ),
                      Stack(
                        alignment: Alignment.topLeft,
                        clipBehavior: Clip.none,
                        children: [
                          Text(
                            "Welcome !",
                            style: TextStyle(
                              color: Colors.white,
                              fontFamily: "BalooPaaji",
                              fontSize: 18.0,
                              fontWeight: FontWeight.w600,
                            ),
                          ),
                          Positioned(
                            top: 20.0,
                            child: Text(
                              userData.fullName,
                              style: TextStyle(
                                color: Colors.white,
                                fontFamily: "BalooPaaji",
                                fontSize: 24.0,
                                fontWeight: FontWeight.w900,
                              ),
                            ),
                          ),
                        ],
                      ),
                      // Column(
                      //   mainAxisAlignment: MainAxisAlignment.center,
                      //   crossAxisAlignment: CrossAxisAlignment.start,
                      //   children: [
                      //     Text(
                      //       "Welcome !",
                      //       style: TextStyle(
                      //         color: Colors.white,
                      //         fontFamily: "BalooPaaji",
                      //         fontSize: 18.0,
                      //         fontWeight: FontWeight.w600,
                      //       ),
                      //     ),
                      //     Text(
                      //       userData.fullName,
                      //       style: TextStyle(
                      //         color: Colors.white,
                      //         fontFamily: "BalooPaaji",
                      //         fontSize: 24.0,
                      //         fontWeight: FontWeight.w900,
                      //       ),
                      //     ),
                      //   ],
                      // ),
                      // Text.rich(
                      //   TextSpan(
                      //     text: "Welcome !\n",
                      //     style: TextStyle(
                      //       color: Colors.white,
                      //       fontFamily: "BalooPaaji",
                      //       fontSize: 18.0,
                      //       fontWeight: FontWeight.w600,
                      //     ),
                      //     children: [
                      //       TextSpan(
                      //         text: userData.fullName,
                      //         style: TextStyle(
                      //           color: Colors.white,
                      //           fontFamily: "BalooPaaji",
                      //           fontSize: 24.0,
                      //           fontWeight: FontWeight.w900,
                      //         ),
                      //       ),
                      //     ],
                      //   ),
                      // ),
                    ],
                  );
                },
              ),
              StreamBuilder(
                stream: FirebaseFirestore.instance
                    .collection("Users Database")
                    .doc(user.uid)
                    .collection("Cart")
                    .snapshots(),
                builder: (context, snapshot) {
                  int totalItems = 0;
                  if (snapshot.connectionState == ConnectionState.active) {
                    List documents = snapshot.data.docs;
                    totalItems = documents.length;
                  }
                  return GestureDetector(
                    onTap: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => CartScreen(),
                        ),
                      );
                    },
                    child: Container(
                      height: 40.0,
                      width: 40.0,
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(
                          10.0,
                        ),
                      ),
                      child: Center(
                        child: Text(
                          "$totalItems" ?? "0",
                          style: TextStyle(
                            fontSize: 20.0,
                            fontFamily: "BalooPaaji",
                            fontWeight: FontWeight.w600,
                            color: Colors.black,
                          ),
                        ),
                      ),
                    ),
                  );
                },
              ),
            ],
          ),
        ),
        Positioned(
          top: 200.0,
          child: SearchBar(
            width: MediaQuery.of(context).size.width * 0.83,
            hintText: "Location",
          ),
        ),
      ],
    );
  }
}

Someone please help me and fix this issue as soon as possible.


Solution

  • I guess you are trying to reduce gap / padding between the 2 lines. If that is so, then the easiest way is to wrap them inside container and give it a fixed height. Check below.

    Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          height: 20,  // assign height as per your choice
          child: Text(
            "Welcome !",
            style: TextStyle(
              color: Colors.white,
              fontFamily: "BalooPaaji",
              fontSize: 18.0,
              fontWeight: FontWeight.w600,
            ),
          ),
        ),
        Container(
          height: 26,  // assign height as per your choice
          child: Text(
            userData.fullName,
            style: TextStyle(
              color: Colors.white,
              fontFamily: "BalooPaaji",
              fontSize: 24.0,
              fontWeight: FontWeight.w900,
            ),
          ),
        ),
      ],
    ),
    
    
    
    NOTE: you can also give height inside the Text widget
    Text(
      userData.fullName,
      style: TextStyle(
        color: Colors.white,
        fontFamily: "BalooPaaji",
        fontSize: 24.0,
        fontWeight: FontWeight.w900,
        height: 1.2,   // assign height to fontSize ratio  as per your choice
      ),
    ),