【发布时间】:2018-12-31 22:39:31
【问题描述】:
我正在尝试使用 Flutter 的“EditableText”类。 我对此有 3 个问题。
“enableInteractiveSelection”设置为“true”,但我没有看到“剪切、复制、粘贴”。
'onSelectionChanged' 似乎不起作用。即使更改选择,我也看不到控制台中的任何内容。
谁能解释一下如何使用“selectionControls”?
以下是我的“可编辑文本”代码
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Editable Text'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
TextEditingController _mainContentController;
TextSelectionControls _mainContentSelectionController;
FocusNode _textFieldFocusNode;
@override
void initState() {
_textFieldFocusNode = FocusNode();
_mainContentController = TextEditingController();
super.initState();
}
@override
void dispose() {
_mainContentController.dispose();
_textFieldFocusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: <Widget>[
Text('Editable Text Below'),
Container(
padding: EdgeInsets.all(10),
child: EditableText(
focusNode: _textFieldFocusNode,
controller: _mainContentController,
selectionColor: Colors.blue,
style: TextStyle(color: Colors.black, fontSize: 17),
cursorColor: Colors.blue,
textInputAction: TextInputAction.newline,
maxLines: null,
onChanged: (text) {
print(text);
},
enableInteractiveSelection: true,
onSelectionChanged:
(TextSelection textSelection, SelectionChangedCause cause) {
print('working?');
print(textSelection.start);
print(cause);
},
selectionControls: _mainContentSelectionController,
),
)
],
),
);
}
}
【问题讨论】: