探索未来文本编辑的可能:Flutter Markdown 编辑器
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MarkdownEditorScreen(),
);
}
}
class MarkdownEditorScreen extends StatefulWidget {
@override
_MarkdownEditorScreenState createState() => _MarkdownEditorScreenState();
}
class _MarkdownEditorScreenState extends State<MarkdownEditorScreen> {
final _controller = TextEditingController();
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Markdown Editor'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: <Widget>[
Expanded(
child: TextField(
controller: _controller,
maxLines: null,
decoration: InputDecoration(hintText: 'Enter Markdown'),
),
),
SizedBox(height: 16.0),
Expanded(
child: Markdown(
data: _controller.text,
selectable: true,
),
),
],
),
),
);
}
}
这段代码创建了一个简单的Markdown编辑器,用户可以在上面输入Markdown格式的文本,并实时预览。这个例子展示了如何在Flutter应用中整合Markdown解析器,并使用TextField
来接收用户输入。
评论已关闭