GotJimmy / accordion

Other
46 stars 43 forks source link

When I focus the TextFormField the keyboard disappears and the Accordion closes. #68

Closed gundogdutekin closed 5 days ago

gundogdutekin commented 6 days ago

` import 'package:accordion/accordion.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:plan/controller/school_setting_controller.dart'; import 'package:plan/utils/constants/colors.dart'; import 'package:plan/widgets/template.dart/custom_appbar.dart'; import 'package:plan/widgets/template.dart/custom_card.dart'; import 'package:plan/widgets/template.dart/form/custom_dropdown_button_form_field.dart'; import 'package:plan/widgets/template.dart/form/custom_elevated_button.dart'; import 'package:plan/widgets/template.dart/form/custom_text_form_field.dart';

class GeneralSettingScreen extends StatelessWidget { const GeneralSettingScreen({super.key});

static const double headerLeftIconSize = 28; static const double headerRightIconSize = 33; static const double headerPaddingVertical = 16.0; static const double headerPaddingHorizontal = 14.0; static const Color leftIconColor = PlanColors.light; static const Color rightIconColor = PlanColors.light; static const Color headerTextColor = Colors.white; static const Color contentBorderColor = Colors.transparent; static const Color headerBackgroundColor = PlanColors.bgDark; static const Color contentBackgroundColor = PlanColors.light; @override Widget build(BuildContext context) { final GlobalKey formKey = GlobalKey();

return Scaffold(
  backgroundColor: const Color(0xFF333A47),
  appBar: const CustomAppBar(
    title: 'Ayarlar',
  ),
  body: CustomCard(
      child: Accordion(
    children: [
      AccordionSection(
        contentBorderColor: contentBorderColor,
        headerBackgroundColor: headerBackgroundColor,
        contentBackgroundColor: contentBackgroundColor,
        leftIcon: const Icon(Icons.school,
            color: leftIconColor, size: headerLeftIconSize),
        rightIcon: const Icon(Icons.keyboard_arrow_down_outlined,
            color: rightIconColor, size: headerRightIconSize),
        headerPadding: const EdgeInsets.symmetric(
            vertical: headerPaddingVertical,
            horizontal: headerPaddingHorizontal),
        header: Text('Okul Ayarları',
            style: Theme.of(context).textTheme.titleLarge?.copyWith(
                  color: headerTextColor,
                )),
        content: GetBuilder<SchoolSettingController>(
          init: Get.find<SchoolSettingController>(),
          builder: (controllerSchool) {
            return controllerSchool.schoolList.isNotEmpty
                ? SingleChildScrollView(
                    physics: const BouncingScrollPhysics(),
                    child: SizedBox(
                      height: MediaQuery.of(context).size.height / 12,
                      child: Scrollbar(
                        child: ListView.separated(
                          shrinkWrap: true,
                          itemCount: controllerSchool.schoolList.length,
                          itemBuilder: (context, index) {
                            return ListTile(
                              leading: IconButton(
                                onPressed: () async {
                                  final schoolId =
                                      controllerSchool.schoolList[index].id;
                                  if (schoolId != null) {
                                    await controllerSchool
                                        .deleteSchool(schoolId);
                                  }
                                },
                                icon: const Icon(Icons.delete),
                                color: Colors.red,
                              ),
                              title: Text(
                                  controllerSchool.schoolList[index].name),
                              trailing: IconButton(
                                  onPressed: () {},
                                  icon: const Icon(Icons.edit),
                                  color: Colors.green),
                            );
                          },
                          separatorBuilder:
                              (BuildContext context, int index) {
                            return const Divider(
                              color: PlanColors.lightDarkGrey,
                              thickness: 0.4,
                            );
                          },
                        ),
                      ),
                    ),
                  )
                : SingleChildScrollView(
                    child: Container(
                      padding: const EdgeInsets.all(20),
                      child: Form(
                        key: formKey,
                        child: Column(
                          children: [
                            CustomTextFormField(
                              hintText: 'Okul Adı',
                              validator: (value) {
                                if (value == null || value.isEmpty) {
                                  return 'Lütfen okul adını giriniz';
                                }
                                return null;
                              },
                              onSaved: (value) async {
                                await controllerSchool
                                    .setSchool(value.toString());
                              },
                            ),
                            CustomDropdownButtonFormField(
                              hint: 'Sınıf Seçiniz',
                              validator: (value) {
                                if (value == null || value.isEmpty) {
                                  return 'Lütfen sınıf seçiniz';
                                }
                                return null;
                              },
                              items: const [
                                DropdownMenuItem(
                                  value: '1',
                                  child: Text('1.Sınıf'),
                                ),
                                DropdownMenuItem(
                                  value: '2',
                                  child: Text('2.Sınıf'),
                                ),
                                DropdownMenuItem(
                                  value: '3',
                                  child: Text('3.Sınıf'),
                                ),
                                DropdownMenuItem(
                                  value: '4',
                                  child: Text('4.Sınıf'),
                                ),
                              ],
                              onSaved: (value) async {
                                await controllerSchool
                                    .setClassroom(value.toString());
                              },
                              onChanged: (value) async {
                                await controllerSchool
                                    .setClassroom(value.toString());
                              },
                            ),
                            CustomElevatedButton(
                              icon: Icons.save,
                              onPressed: () async {
                                if (formKey.currentState!.validate()) {
                                  formKey.currentState!.save();
                                  await controllerSchool.createSchool();
                                  await controllerSchool.getSchool();
                                }
                              },
                            ),
                          ],
                        ),
                      ),
                    ),
                  );
          },
        ),
      ),
    ],
  )
      ),
);

} } `

Controller page : ` import 'package:get/get.dart'; import 'package:plan/local_database.dart'; import 'package:plan/model/school_model.dart'; import 'package:plan/widgets/template.dart/custom_snackbar.dart';

class SchoolSettingController extends GetxController { RxString schoolName = ''.obs; RxString classroom = ''.obs; List schoolList = [];

@override void onInit() { getSchool(); super.onInit(); }

Future setSchoolList(List value) async { schoolList = value; update(); }

Future setSchool(String value) async { schoolName.value = value; }

Future setClassroom(String value) async { classroom.value = value; }

Future createSchool() async { try { final school = SchoolModel( name: schoolName.value, classroom: classroom.value, ); final result = await LocalDatabase().createSchool(school); if (result != -1) { CustomSnackBar.show( title: 'Başarılı', message: 'Okul ayarları oluşturuldu', state: SnackState.SUCCESS, ); } else { CustomSnackBar.show( title: 'HATA!', message: 'Okul ayarları oluşturulamadı', state: SnackState.ERROR, ); } update(); } catch (e) { print(e); } }

Future getSchool() async { try { final school = await LocalDatabase().getSchool(); if (school.isNotEmpty) { schoolList = school; update(); } } catch (e) { print(e); } }

Future deleteSchool(int id) async { try { final result = await LocalDatabase().deleteSchool(id); if (result != -1) { schoolList = []; /setSchool(''); setClassroom('');/ CustomSnackBar.show( title: 'Başarılı', message: 'Okul ayarları silindi', state: SnackState.SUCCESS, ); } else { CustomSnackBar.show( title: 'HATA!', message: 'Okul ayarları silindi', state: SnackState.ERROR, ); } update(); } catch (e) { print(e); } } } ` Ekran Resmi 2024-09-15 15 40 54

After deleting the recorded data from the screen, when I focus on the TextFormField field in the form that appears, the keyboard closes and the accordion becomes closed? I have researched a lot, what do you think is the problem? I would be very happy if you could help. The screen video link is below.

https://github.com/user-attachments/assets/7ca38bdf-0b9f-4682-b04e-162dcdcf8d01

gundogdutekin commented 6 days ago

I found the error. The widget I assigned to the Accordion Section's content property should have been a separate widget that handled its own state.

GotJimmy commented 5 days ago

Great!