Skip to content

Instantly share code, notes, and snippets.

@bkayfitz-cara
Created September 19, 2018 18:56
Show Gist options
  • Save bkayfitz-cara/8da2afd964c7f4417435e9df49b4cd9e to your computer and use it in GitHub Desktop.
Save bkayfitz-cara/8da2afd964c7f4417435e9df49b4cd9e to your computer and use it in GitHub Desktop.
FocusNodes and Navigator problem
import 'package:flutter/material.dart';
void main() {
runApp(TestApp());
}
class TestApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(),
home: TestScreen(),
);
}
}
class TestScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Center(
child: RaisedButton(
onPressed: () => _pushSecondScreen(context),
child: Text('Push to a new screen')),
),
));
}
void _pushSecondScreen(BuildContext context) {
Navigator.of(context).push(MaterialPageRoute(builder: (_) => SecondTest()));
}
}
class SecondTest extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Screen'),
),
body: ListView(
padding: EdgeInsets.all(20.0),
children: List<Widget>.generate(10, (i) => _buildTextField(i)),
),
);
}
Widget _buildTextField(int index) {
// The custom focus node is thrown in here for example, but in a real world app,
// a reference to the node would be kept so the app can manage focus between the nodes
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: TextField(
focusNode: FocusNode(),
decoration: InputDecoration(
labelText: 'Field $index',
filled: true,
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(5.0)))),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment