Skip to content

Instantly share code, notes, and snippets.

@KammererTob
Created June 11, 2021 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KammererTob/8488f470b166e4235b64d3ba568b6ba6 to your computer and use it in GitHub Desktop.
Save KammererTob/8488f470b166e4235b64d3ba568b6ba6 to your computer and use it in GitHub Desktop.
Flutter Focus/Builder usage
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
/// This is the main application widget.
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: const MyStatelessWidget(),
),
);
}
}
/// This is the private State class that goes with MyStatefulWidget.
class MyStatelessWidget extends StatelessWidget {
const MyStatelessWidget();
@override
Widget build(BuildContext context) {
return FocusScope(
debugLabel: 'Scope',
autofocus: true,
child: Form(
child: Column(
children: [
Focus(
debugLabel: 'TextField1',
child: Builder(
builder: (BuildContext context) {
final FocusNode focusNode = Focus.of(context);
final bool hasFocus = focusNode.hasFocus;
return TextField(
decoration: InputDecoration(
fillColor: hasFocus ? Colors.green : Colors.white,
filled: true
)
);
},
),
),
Focus(
debugLabel: 'TextField2',
child: Builder(
builder: (BuildContext context) {
final FocusNode focusNode = Focus.of(context);
final bool hasFocus = focusNode.hasFocus;
return TextField(
decoration: InputDecoration(
fillColor: hasFocus ? Colors.green : Colors.white,
filled: true
)
);
}
)
)
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment