flutter 全选,如何在Flutter中更改文本选择选项?

I tried to add text editing format option like in the gmail app. But when highlight the text there' is not a format option. Is it possible to handle selecting alert? (Copy/cut/paste). Or is there a way to add format bar like gmail?

TextField(

controller: _categoryController,

decoration: InputDecoration(

border: InputBorder.none,

hintText: "Enter Category Name",

),

),

I added screenshot and gif files to better understanding my question.

Selecting option on my Flutter application

9abd78971e022fb484a3578797379d70.png

Selecting option on Gmail App

a54b85b05853ff0aded2279a144b949d.gif

解决方案

Output:

94f216b0846994021ee91deafdcedcad.png

You can check the code below:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

// This widget is the root of your application.

@override

Widget build(BuildContext context) {

return MaterialApp(

title: 'Flutter Demo',

theme: ThemeData(

primarySwatch: Colors.blue,

),

home: MyHomePage(title: 'Flutter Demo Home Page'),

);

}

}

class MyHomePage extends StatefulWidget {

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

final String title;

@override

_MyHomePageState createState() => _MyHomePageState();

}

class _MyHomePageState extends State {

final _controller = new TextEditingController();

final _textfieldFocusNode = new FocusNode();

@override

Widget build(BuildContext context) {

return Scaffold(

appBar: AppBar(

title: Text(widget.title),

),

body: Center(

child: Column(

mainAxisAlignment: MainAxisAlignment.center,

children: [

Padding(

padding: EdgeInsets.all(20.0),

child: GestureDetector(

// intercept all pointer calls

behavior: HitTestBehavior.opaque,

onTap: () {

FocusScope.of(context).requestFocus(_textfieldFocusNode);

},

onLongPress: () {

showMenu(

context: context,

// TODO: Position dynamically based on cursor or textfield

position: RelativeRect.fromLTRB(0.0, 300.0, 300.0, 0.0),

items: [

PopupMenuItem(

child: Row(

children: [

// TODO: Dynamic items / handle click

PopupMenuItem(

child: Text(

"Paste",

style: Theme.of(context)

.textTheme

.body2

.copyWith(color: Colors.red),

),

),

PopupMenuItem(

child: Text("Select All"),

),

],

),

),

],

);

},

child: IgnorePointer(

// ensures textfield doesn't overrule GestureDetector

child: TextField(

focusNode: _textfieldFocusNode,

controller: _controller,

),

),

),

)

],

),

),

);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值