简体   繁体   English

Flutter 文本框内的复选框

[英]Flutter checkbox inside textfield

I was wondering how I could recreate the Samsung Notes App.我想知道如何重新创建 Samsung Notes 应用程序。 To be more precise I want to have the checkbox functionality within the textfield.更准确地说,我希望在文本字段中具有checkbox功能。 See picture below:见下图:

复选框

Thanks for your support!谢谢你的支持!

I've just tried it myself and this looks like how you want it to look:我刚刚自己尝试过,这看起来像你想要的样子:

TextField(
  decoration: InputDecoration(
    prefixIcon: Checkbox(value: value, onChanged: (value) {//bool magic here}),
    hintText: 'TextField'
  ),
),

Check out https://api.flutter.dev/flutter/material/InputDecoration-class.html for more InputDecoration() options to prettify things.查看https://api.flutter.dev/flutter/material/InputDecoration-class.html以获得更多 InputDecoration() 选项来美化事物。

if I understood you correctly you can use Textformfield.如果我理解正确的话,你可以使用 Textformfield。 You can add the checkbox widget to the textformfield prefix parameter.您可以将复选框小部件添加到 textformfield 前缀参数。 I hope this gives you an idea and helps.我希望这能给你一个想法并有所帮助。

For Example:例如:

class MyHomePage extends StatefulWidget {
  final String title;

  const MyHomePage({
    Key? key,
    required this.title,
  }) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
 
    bool isChecked = false;
    TextEditingController textController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: TextField(
          controller:textController,
          style:TextStyle(decoration: isChecked ? TextDecoration.lineThrough : TextDecoration.none),
  decoration: InputDecoration(
    prefixIcon: Checkbox(value:isChecked, onChanged: (value) {
       setState(() { isChecked=value!;});
     
    }),
    hintText: 'To Do'
  ),
          ),
        ),
);
  }
}

Dartpad => https://dartpad.dev/?id=e75b493dae1287757c5e1d77a0dc73f1 Dartpad => https://dartpad.dev/?id=e75b493dae1287757c5e1d77a0dc73f1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM