blob: 0dee82bdd1eb3b72e3e18c431d96ce510a7dbff7 [file]
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Template: dev/snippets/config/templates/stateful_widget_material.tmpl
//
// Comment lines marked with "▼▼▼" and "▲▲▲" are used for authoring
// of samples, and may be ignored if you are just exploring the sample.
// Flutter code sample for Focus
//
//***************************************************************************
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
// This example shows how to focus a newly-created widget immediately after it
// is created.
//
// The focus node will not actually be given the focus until after the frame in
// which it has requested focus is drawn, so it is OK to call
// [FocusNode.requestFocus] on a node which is not yet in the focus tree.
//* ▲▲▲▲▲▲▲▲ description ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
//***************************************************************************
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 const MaterialApp(
title: _title,
home: MyStatefulWidget(),
);
}
}
/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
//********************************************************************
//* ▼▼▼▼▼▼▼▼ code ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
int focusedChild = 0;
List<Widget> children = <Widget>[];
List<FocusNode> childFocusNodes = <FocusNode>[];
@override
void initState() {
super.initState();
// Add the first child.
_addChild();
}
@override
void dispose() {
super.dispose();
for (final FocusNode node in childFocusNodes) {
node.dispose();
}
}
void _addChild() {
// Calling requestFocus here creates a deferred request for focus, since the
// node is not yet part of the focus tree.
childFocusNodes
.add(FocusNode(debugLabel: 'Child ${children.length}')..requestFocus());
children.add(Padding(
padding: const EdgeInsets.all(2.0),
child: ActionChip(
focusNode: childFocusNodes.last,
label: Text('CHILD ${children.length}'),
onPressed: () {},
),
));
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Wrap(
children: children,
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
focusedChild = children.length;
_addChild();
});
},
child: const Icon(Icons.add),
),
);
}
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
//********************************************************************
}