rtf_textfield 1.0.1 rtf_textfield: ^1.0.1 copied to clipboard
Flutter custom widget to create Rich TextField. It includes textspan for hint, label. Control the text style with RichTextController.
import 'package:flutter/material.dart';
import 'package:rtf_textfield/rtf_textfield.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final RTFTextFieldController controller = RTFTextFieldController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Image.asset(
'../assets/images/rich_text_field.png',
height: 50,
),
),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: <Widget>[
RTFTextField(
onTapOutside: (event) {
FocusManager.instance.primaryFocus?.unfocus();
},
decoration: const RichInputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(16)),
),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.grey,
),
borderRadius: BorderRadius.all(Radius.circular(16)),
),
labelTextSpan: TextSpan(
text: 'Enter your name',
children: [
TextSpan(
text: ' *',
style: TextStyle(
color: Colors.red,
),
),
],
),
hintTextSpan: TextSpan(
text: 'Yelaman',
),
),
controller: controller,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
controller.toggleBold();
},
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}