rgi*_*isi 2 focus textfield flutter
我有一个文本字段和一个单独的按钮,我想在文本字段聚焦时更改按钮的颜色。
天真的我想这样写:FlatButton(..., backgroundColor: myFocusNode.hasFocus ? Colors.red : Colors.green)。当然,这不起作用,因为按钮不会收到有关焦点更改的通知。或者,我尝试添加Listenerto myFocusNode,但如果我尝试setState在其中调用以重建按钮,则文本字段会再次失去焦点。
我修改了相应食谱中的示例代码来进行说明,您可以复制下面的代码并粘贴并运行以查看我的意思。单击文本字段时如何使按钮变成红色?
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Text Field Focus',
home: MyCustomForm(),
);
}
}
// Define a custom Form widget.
class MyCustomForm extends StatefulWidget {
@override
_MyCustomFormState createState() => _MyCustomFormState();
}
// Define a corresponding State class.
// This class holds data related to the form.
class _MyCustomFormState extends State<MyCustomForm> {
// Define the focus node. To manage the lifecycle, create the FocusNode in
// the initState method, and clean it up in the dispose method.
FocusNode myFocusNode;
@override
void initState() {
super.initState();
myFocusNode = FocusNode();
}
@override
void dispose() {
// Clean up the focus node when the Form is disposed.
myFocusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Text Field Focus'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
// The first text field is focused on as soon as the app starts.
TextField(
autofocus: true,
),
// The second text field is focused on when a user taps the
// FloatingActionButton.
TextField(
focusNode: myFocusNode,
),
],
),
),
floatingActionButton: FloatingActionButton(
// When the button is pressed,
// give focus to the text field using myFocusNode.
onPressed: () => myFocusNode.requestFocus(),
tooltip: 'Focus Second Text Field',
backgroundColor: myFocusNode.hasFocus ? Colors.red : Colors.green,
child: Icon(Icons.edit),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
Run Code Online (Sandbox Code Playgroud)
请检查下面的代码,我已经使用了Focus它的小部件,它有onFocusChange指示TextField是否选择了
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutterlearningapp/colors.dart';
class HomeScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _HomeScreen();
}
}
class _HomeScreen extends State<HomeScreen> {
bool isTextFiledFocus = false;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Demo",
home: Scaffold(
appBar: AppBar(
title: Text("List"),
),
body: Container(
margin: EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Focus(
child: TextFormField(
textInputAction: TextInputAction.next,
decoration: InputDecoration(labelText: "Input 1"),
),
onFocusChange: (hasFocus) {
setState(() {
isTextFiledFocus = hasFocus;
});
},
),
TextFormField(
textInputAction: TextInputAction.next,
decoration: InputDecoration(labelText: "Input 1"),
),
RaisedButton(
color: isTextFiledFocus ? Colors.pink : Colors.blue,
child: Text("Ok"),
onPressed: () {
print("I am clicked");
},
)
],
),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7310 次 |
| 最近记录: |