Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

only rebuild part of page with callbacks

I give two buttons to choose a gender to the user, and I want the uso can ,choose but when not only row rebuild, the gender value will update. I can do itthe with provider but I know this can do with callbackfunctions. I read, I tried but I coldn’t , in addition can you give me a sources about this to learn. If title is bad, feel free to change

import 'package:flutter/material.dart';

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

  @override
  State<UserCreateView> createState() => UserCreateViewState();
}

class UserCreateViewState extends State<UserCreateView> {
  bool? gender;

  void changeGender(bool genderChoose) {
    gender = genderChoose;
    setState(() {});
  }

  final _formKey = GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Form(
          key: _formKey,
          child: Column(
            children: [
              ElevatedButton(
                onPressed: () {},
                child: const Text('I dont want to be rebuild, fix it'),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ElevatedButton(
                    onPressed: () {
                      changeGender(true);
                    },
                    style: ElevatedButton.styleFrom(
                      primary: gender != true
                          ? const Color.fromARGB(255, 92, 161, 207)
                          : const Color.fromARGB(255, 37, 131, 41),
                    ),
                    child: const Text(
                      'man',
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(12.0),
                    child: ElevatedButton(
                      onPressed: () {
                        changeGender(false);
                      },
                      style: ElevatedButton.styleFrom(
                        primary: gender == true
                            ? const Color.fromARGB(255, 92, 161, 207)
                            : const Color.fromARGB(255, 37, 131, 41),
                      ),
                      child: const Text(
                        'woman',
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use ValueNotifier with ValueListenableBuilder in this case.

class UserCreateView extends StatelessWidget {
  const UserCreateView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final _formKey = GlobalKey<FormState>();

    final ValueNotifier<bool?> gender = ValueNotifier(null);
    return SafeArea(
      child: Scaffold(
        body: Form(
          key: _formKey,
          child: Column(
            children: [
              ElevatedButton(
                onPressed: () {},
                child: const Text('I dont want to be rebuild, fix it'),
              ),
              ValueListenableBuilder(
                valueListenable: gender,
                builder: (context, value, child) => Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    ElevatedButton(
                      onPressed: () {
                        gender.value = true;
                      },
                      style: ElevatedButton.styleFrom(
                        primary: gender.value != true
                            ? const Color.fromARGB(255, 92, 161, 207)
                            : const Color.fromARGB(255, 37, 131, 41),
                      ),
                      child: const Text(
                        'man',
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(12.0),
                      child: ElevatedButton(
                        onPressed: () {
                          gender.value = false;
                        },
                        style: ElevatedButton.styleFrom(
                          primary: gender.value == true
                              ? const Color.fromARGB(255, 92, 161, 207)
                              : const Color.fromARGB(255, 37, 131, 41),
                        ),
                        child: const Text(
                          'woman',
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading