Prevent widget tree from gaining focus

Viewed 560

Is there a way to prevent a widget tree gaining focus ?

I want to prevent Child column or any nested children (Field 1, Field 2) from gaining focus without disabling the fields, Field 3 should still be focusable. how to achieve it ?

Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text('Example'),
    ),
    body: Column(
      key: Key("Parent column"),
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Column(
          key: Key("Child column"),
          children: <Widget>[
            TextField(
              key: Key("Field 1"),
            ),
            TextField(
              key: Key("Field 2"),
            )
          ],
        ),
        TextField(
          key: Key("Field 3"),
        )
      ],
    ),
  );
}
2 Answers

Thanks to @pskink suggestion.

This is actually very easy implement: Just wrap the widget tree inside the IgnorePointer. shouldIgnore is the variable controlling if Child column and it's children should get any touch events.

bool shouldIgnore = true;

Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text('Example'),
    ),
     body: Column(
      key: Key("Parent column"),
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        IgnorePointer(
          ignoring: shouldIgnore,
          child: Column(
            key: Key("Child column"),
            children: <Widget>[
              TextField(
                key: Key("Field 1"),
              ),
              TextField(
                key: Key("Field 2"),
              )
            ],
          ),
        ),
        TextField(
          key: Key("Field 3"),
        )
      ],
    ),
  );
}
Related