Created
June 11, 2021 16:03
-
-
Save KammererTob/8488f470b166e4235b64d3ba568b6ba6 to your computer and use it in GitHub Desktop.
Flutter Focus/Builder usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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