Jua*_*oza 1 flutter flutter-widget
在 Flutter 中聚焦时如何在 TextFormField 中添加阴影?我希望该字段具有以下外观:
到目前为止,我设法在对焦时应用了边框,但我没有看到任何应用阴影的选项:
TextFormField(
decoration: InputDecoration(
fillColor: Colors.white,
hoverColor: Colors.white,
filled: true,
enabledBorder: OutlineInputBorder(borderSide: BorderSide.none),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey, width: 1))),
);
Run Code Online (Sandbox Code Playgroud)
关于如何获得这种效果的任何想法?
小智 5
重要:TextField用Form小部件包装您并分配_formKey
它有助于防止在之后关闭键盘setState()
完整示例:
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(),
body: Home(),
),
);
}
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
static GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
FocusNode focusNode;
@override
void initState() {
super.initState();
focusNode = FocusNode();
focusNode.addListener(() => setState(() {}));
}
@override
Widget build(BuildContext context) {
return AnimatedContainer(
duration: Duration(seconds: 5),
margin: const EdgeInsets.all(16),
decoration: focusNode.hasFocus ? BoxDecoration(boxShadow: [BoxShadow(blurRadius: 6)]) : null,
child: Form(
key: _formKey,
child: TextFormField(
focusNode: focusNode,
decoration: InputDecoration(
fillColor: Colors.white,
hoverColor: Colors.white,
filled: true,
enabledBorder: OutlineInputBorder(borderSide: BorderSide.none),
focusedBorder: OutlineInputBorder(borderSide: BorderSide(color: Colors.grey, width: 1))),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2384 次 |
| 最近记录: |