Search code examples
flutterdartflutter-dependenciesdart-null-safety

TextFormField and Keyboard in flutter


Good morning, all, I've a problem. I've a text Form Field in my application when I tapped on it to type, the keyboard didn't show this video explain what happened exactly

Explain GIF

I read questions that have same problem and try to do its answer but no way solutions that I tried it. 1- convert the stateless to stateful and the oppsite 2- declare controller global (after imports - in cubit file - in shared file) 3-don't use Form Widget 4- don't use onFieldSubmitted properity 5- try to run flutter run --release but nothing shown in terminal 6- check crashes in google play console
probably I tried most of answers, but no one is working for me,

this is the code of my search screen

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';





class SearchScreen extends StatefulWidget {
  const SearchScreen({Key? key}) : super(key: key);

  @override
  State<SearchScreen> createState() => _SearchScreenState();
}

class _SearchScreenState extends State<SearchScreen> {

  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
    var width = size.width;
    return BlocConsumer<HomeCubit, HomeStates>(
      listener: (context, state) {},
      builder: (context, state) {
        var cubit = HomeCubit.get(context);
        return Directionality(
          textDirection: TextDirection.rtl,
          child: Scaffold(
            body: SingleChildScrollView(
              child: Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 10.0),
                    child: Row(
                      children: [
                        // which zone text
                        SizedBox(
                          width: width * .30,
                          child: FittedBox(child: Text('اختر المنطقة',style: TextStyle(fontSize: 24,fontWeight:FontWeight.normal))))
                        const SizedBox(width: 25.0),
                        Expanded(
                          child: Center(
                            child: DropdownButton(
                              alignment: Alignment.center,
                              value: cubit.zonePopupValue,
                              hint: const Text('كل المناطق', textAlign: TextAlign.center),
                              style: TextStyle(
                                color: HexColor('#ECB365'),
                                fontSize:  24,
                                fontWeight: FontWeight.normal,
                              ),
                              items: cubit.list,
                              onChanged: (value) => cubit.changePopupZoneValue(int.parse(value.toString())),
                            ),
                          ),
                        ),
                      ],
                    )
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 10),
                    child: TextFormField(
                      textInputAction: TextInputAction.search,
                      onFieldSubmitted: (_) async => cubit.search(),
                      controller: cubit.searchController,
                      keyboardType: TextInputType.text,
                      maxLines: 1,
                      maxLength: 100,
                      textAlign: TextAlign.right,
                      textDirection: TextDirection.rtl,
                      decoration: InputDecoration(
                        contentPadding: const EdgeInsets.only(right: 20, left: 10),
                        labelText: "إبحث",
                        alignLabelWithHint: true,
                        labelStyle: TextStyle(
                          color: HexColor('#ECB365'),
                          fontSize: 24,
                          fontWeight: FontWeight.normal,
                        ),
                        suffixIcon: cubit.isLoadingAuth? CircularProgressIndicator(color: HexColor('#ECB365')): IconButton(
                          onPressed: () => cubit.search(),
                          icon: Icon(Icons.search,color: HexColor('#ECB365')),
                          iconSize: 35,
                          color: HexColor('#081C31'),
                        ),
                        border: OutlineInputBorder(borderSide:BorderSide(color: HexColor('#ECB365'))),
                        focusedBorder: OutlineInputBorder(borderSide: BorderSide(color: HexColor('#ECB365'))),
                        enabledBorder: OutlineInputBorder(borderSide:BorderSide(color: HexColor('#ECB365'))),
                        fillColor: HexColor('#ECB365')
                      ),
                    )
                  ),
                  Container(
                    margin: const EdgeInsets.only(left: 10.0, right: 20.0),
                    child: const Divider(color: Colors.black,height: 36)
                  ),
                  cubit.responseBodySearch == null
                    ? const Center(child: Text("أبدأ البحث"))
                    : cubit.responseBodySearch.isEmpty
                    ? const Center(child: Text("غير متوفر حاليا"))  
                    : ListView.builder(
                      physics: const NeverScrollableScrollPhysics(),
                      shrinkWrap: true,
                      itemCount: cubit.responseBodySearch!.length,
                      itemBuilder: (context, index) => buildSearchItem(cubit, context, index)
                    )
                ],
              ),
            ),
          )
        );
      }
    );
  }
}

this is the code of shared component

class HomeCubit extends Cubit<HomeStates> {
  HomeCubit() : super(HomeInitState());

  static HomeCubit get(context) => BlocProvider.of(context);

  String api = 'https://hadayekhof.com/dalel/api/';

  var searchController = TextEditingController();

  int? zonePopupValue;
  void changePopupZoneValue(int value) {
    zonePopupValue = value;
    getCategoriesData(zoneIds[value].toString());
    emit(HomeChangePopupButtonState());
  }

  var responseBodySearch;
  Future search() async {
    emit(HomeSearchLoadingState());
    responseBodySubCategories = null;
    var data = {
      'zone_id':  zonePopupValue == null ? '' : zoneIds[zonePopupValue!].toString(),
      // 'parent_cat': categoryPopupValue == null ? '' : categoryIds[categoryPopupValue!].toString(),
      'term': searchController.text.toString()
    };
    var uri = Uri.parse('${api}stores/searchStores');
    var header = {'Authorization': 'Bearer $ciphertext'};
    if (searchController.text == '') {
      Fluttertoast.showToast(msg: 'من فضلك اكتب شئ');
    } else {
      await http.post(uri, body: data, headers:header ).then((value) async {
        responseBodySearch = await jsonDecode(value.body);
        if (value.body.toString().contains('[') == false) {
          responseBodySearch = null;
          Fluttertoast.showToast(msg: 'no stores found');
        }
        emit(HomeSearchSuccessState());
      }).catchError((error) {
        debugPrint("search error is : $error");
        emit(HomeSearchErrorState());
      });
    }
  }
}

how can I solve this problem?


Solution

  • finally, I solve It by write this code in the path android\app\src\main\AndroidManifest.xml

    <application
     ...
     android: labelwareAccelerated="true"
     ...>
    

    and this

    <activity
     ...
     android:hardwareAccelerated="true"
     ...>
    

    It's working now