Home > Software design >  Search feature in AppBar causes Exception
Search feature in AppBar causes Exception

Time:10-28

I am trying to implement a search feature in my app bar but keep getting this exception. I have no idea what is wrong here. Everything works fine if I remove TextFormField or if I use any other widget excpe TextFormField or TextField. For example if i use another Text widget instead of TextFormField then it works fine. The problem is with only TextField. What should I do?

Exception:

enter image description here

What I want to archive:

enter image description here

My code:

  import 'package:flutter/material.dart';
    import 'package:get/get.dart';
    import 'package:votefun/Menu/main_menu_shop.dart';
    import 'package:votefun/Menu/main_menu_user.dart';
    import 'package:votefun/Menu/main_menu_visiter.dart';
    import 'package:votefun/Utils/app_pages.dart';
    
    class MyAppBar extends StatelessWidget with PreferredSizeWidget {
      @override
      final Size preferredSize;
      final String title;
      final bool drawer;
      final int drawerNumber;
      final bool search;
      final String hint;
    
      MyAppBar(
          {Key? key,
          required this.title,
          required this.drawer,
          required this.drawerNumber,
          required this.search,
          required this.hint})
          : preferredSize = const Size.fromHeight(50.0),
            super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return SafeArea(
          child: ClipPath(
            clipper: MyClipper(),
            child: Container(
              decoration: const BoxDecoration(
                color: Colors.white,
              ),
              //width: double.infinity,
              height: 55,
              child: Material(
                elevation: 20,
                child: Row(
                  children: [
                    const SizedBox(
                      width: 8.0,
                    ),
                    Visibility(
                      visible: (drawer == true) ? false : true,
                      child: IconButton(
                          onPressed: () {
                            Navigator.pop(context);
                          },
                          icon: const Icon(Icons.arrow_back_ios)),
                    ),
                    Visibility(
                      visible: (drawer == true) ? true : false,
                      child: IconButton(
                          onPressed: () {
                            if (drawerNumber == 1)
                              (Navigator.of(context).push(PageRouteBuilder(
                                opaque: false,
                                  pageBuilder: (BuildContext context, _, __) =>
                                      MainMenuUser())));
                            else if (drawerNumber == 2)
                              (Navigator.of(context).push(PageRouteBuilder(
                                opaque: false,
                                  pageBuilder: (BuildContext context, _, __) =>
                                      MainMenuShop())));
                            else {
                              Navigator.of(context).push(PageRouteBuilder(
                                opaque: false,
                                  pageBuilder: (BuildContext context, _, __) =>
                                      MainMenuVisiter()));
                            }
                          },
                          icon: const Icon(Icons.menu)),
                    ),
                    Visibility(visible: (search == true) ? false : true,
                      child: Expanded(
                        child: Text(
                          title,
                          style: const TextStyle(
                              fontSize: 17, fontWeight: FontWeight.bold),
                        ),
                      ),
                    ),
                    Visibility(visible: (search == true) ? true : false,
                      child: 
                      TextFormField(decoration: InputDecoration(hintText: 'hint'),
                      maxLines: 1,
                      minLines: 1,),
                      ),
                    Container(
                        padding: const EdgeInsets.only(right: 5),
                        child: IconButton(
                            onPressed: () {
                              Get.offAllNamed(Routes.SignIn);
                            },
                            icon: Image.asset('images/homeicon.png')))
                  ],
                ),
              ),
            ),
          ),
        );
      }
    }
    
    class MyClipper extends CustomClipper<Path> {
      @override
      Path getClip(Size size) {
        var path = Path();
        path.lineTo(0.0, size.height);
        path.lineTo(size.width, size.height / 1.18);
        path.lineTo(size.width, 0.0);
        path.close();
        return path;
      }
    
      @override
      bool shouldReclip(covariant CustomClipper<Path> oldClipper) {
        return false;
      }
    }

CodePudding user response:

I got it, the cause of the exception was the unlimited width of the TextField.

  • Related