简体   繁体   中英

flutter - Is it possible to make submit button from another class?

I am making instagram clone project.

I haven't made it yet, but I wonder if this function is possible.

enter image description here

I want to make submit button on the appbar

I could find done button source in Keyboard but i want to make appbar submit button.

cuz Appbar submit button function seems to have to use the access method of another class.

So I want to learn that. Is it related gloabalkey? hm....so confused.....

I'd really appreciate it if anyone could help me out.

Yes You can do this: You can use Getx for this to observe the EditText and when button click it will take the action as per the EditText. Here is the Getx Example:

dependencies:
  get: ^4.6.1

Example: In this example you will learn the basics of GetX. You will see how much easier it is to code with this framework, and you will know what problems GetX proposes to solve.

If the default Flutter application were rewritten with Getx, it would have only a few lines of code. The Getx state manager is easier than using setState. You just need to add a ".obs" at the end of your variable, and wrap the widget you want to change within a Obx().

void main() => runApp(MaterialApp(home: Home()));

class Home extends StatelessWidget {
  var count = 0.obs;
  @override
  Widget build(context) => Scaffold(
      appBar: AppBar(title: Text("counter")),
      body: Center(
        child: Obx(() => Text("$count")),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () => count ++,
      ));
}

hope its Helpful to you thanks

You can use VoidCallback to custom class, and listen tap event from app bar.

appBar: CustomAppBar(
  callback: () {
    print("Save");
  },
),

Custom App bar will be

class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
  const CustomAppBar({
    Key? key,
    required this.callback,
  }) : super(key: key);
  final VoidCallback callback;

  @override
  Widget build(BuildContext context) {
    return AppBar(
      actions: [
        IconButton(
          onPressed: callback,
          icon: const Icon(Icons.save),
        )
      ],
    );
  }

  @override
  Size get preferredSize => const Size.fromHeight(kToolbarHeight);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM