ensure_visible_when_focused 1.2.0 ensure_visible_when_focused: ^1.2.0 copied to clipboard
A handy Flutter widget to scroll to a certain widget every time this widget gains Focus.
import 'package:ensure_visible_when_focused/ensure_visible_when_focused.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({
Key? key,
}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late GlobalKey<FormState> _formKey;
late FocusNode _focusNode;
@override
void initState() {
_formKey = GlobalKey<FormState>();
_focusNode = FocusNode();
super.initState();
}
@override
void dispose() {
_focusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Form(
key: _formKey,
child: Padding(
padding: const EdgeInsets.all(18),
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
EnsureVisibleWhenFocused(
focusNode: _focusNode,
child: TextFormField(
focusNode: _focusNode,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: '* Form Field',
),
validator: (value) => 'Required Field',
),
),
...List.generate(
30,
(i) => Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: TextFormField(
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Form Field ${i + 1}',
),
),
),
).toList(),
],
),
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
final _form = _formKey.currentState!;
_form.validate();
_focusNode.requestFocus();
},
child: Icon(Icons.check),
),
),
);
}
}