flutter Placeholder(占位文字和图片等)

上代码

复制代码
import 'package:flutter/material.dart';

void main() {
  runApp( MaterialApp(
    title: 'Flutter gesture',
//    home: TutorialHome(),
    home: _home(),
  ));
}

class _home extends StatelessWidget{

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Text Demo',
      theme: ThemeData(
          primarySwatch: Colors.green
      ),
      home: MyHomePage(title: 'Text Demo'),
    );
  }
}


class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>{
  @override
  Widget build(BuildContext context) {
    var _name = "flutter ";
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        child: ListView(
          children: <Widget>[
        TextField(//提示缩小在输入上方
        decoration:
        InputDecoration(
            fillColor: Colors.blue.shade100,
            filled: true,
            labelText: 'Hello'),
      ),
      TextField(//输入后提示消失,如果输入不符合要求就可以报相应错误(自定义提示语)
        decoration: InputDecoration(
            fillColor: Colors.blue.shade100,
            filled: true,
            hintText: 'Hello',
            errorText: 'error'),
      ),
      TextField(//添加图标
        decoration: InputDecoration(

            fillColor: Colors.blue.shade100,
            filled: true,
            helperText: 'help',
            prefixIcon: Icon(Icons.local_airport),
            suffixText: 'airport'),
      ),
      TextField(//输入框添加圆切角
        decoration: InputDecoration(
            contentPadding: EdgeInsets.all(10.0),
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(15.0),
//            borderSide: BorderSide(color: Colors.red, width: 3.0, style: BorderStyle.solid)//没什么卵效果
            )),
      ),
      Theme(//利用ThemeData改变TextField的边框样色
        data: new ThemeData(primaryColor: Colors.red, hintColor: Colors.blue),
        child: TextField(
          decoration: InputDecoration(
              contentPadding: EdgeInsets.all(10.0),
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(15.0),
//            borderSide: BorderSide(color: Colors.red, width: 3.0, style: BorderStyle.solid)//没什么卵效果
              )),
        ),
      ),
            Container(//改变边框的粗细,这些TextField的decoration彻底不能满足要求了,需要重构成这种方式,InputDecoration.collapsed可以禁用装饰线,而是使用外面包围的Container的装饰线
              padding: const EdgeInsets.all(8.0),
              alignment: Alignment.center,
              height: 60.0,
              decoration: new BoxDecoration(
                  color: Colors.blueGrey,
                  border: new Border.all(color: Colors.black54, width: 4.0),
                  borderRadius: new BorderRadius.circular(12.0)),
              child: new TextFormField(
                decoration: InputDecoration.collapsed(hintText: 'hello'),
              ),
            ),
            Icon(
              Icons.access_alarm,//设置使用哪种图标
              size: 300,//设置图标大小
              color: Colors.yellow,//设置图标颜色
              textDirection:TextDirection.rtl ,//设置用于渲染图标的文本方向
              semanticLabel: "语义标签",//设置用于渲染图标的文本方向
            ),

            Placeholder(
              color: Colors.blue,// 设置占位符颜色 defalutBlue Grey 70
              strokeWidth: 5,//设置画笔宽度
              fallbackHeight: 200,//设置占位符宽度
              fallbackWidth: 200,//设置占位符高度
            ),

            Icon(
              Icons.build,
              size: 300,
              color: Colors.green,
              textDirection:TextDirection.ltr ,
            ),
          ],



        ),
      ),
    );
  }
}
复制代码

见到就是干

posted on   高彰  阅读(4147)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
历史上的今天:
2018-12-05 iOS tableview cell点击无反应 (无高亮选中),出现一条白线怎么办?
2016-12-05 Storyboard中segue(转场)使用
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示