custom_error_text_form_field 0.0.1 custom_error_text_form_field: ^0.0.1 copied to clipboard
A TextFormField widget that lets you show the error message as an overlay.
import 'package:custom_error_text_form_field/custom_error_text_form_field.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@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> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Column(
children: List.generate(
10,
(_) => CustomErrorTextFormField(
validator: (val) {
if (val != null && val.isNotEmpty) {
return 'Text is not empty';
}
return null;
},
autovalidateMode: AutovalidateMode.onUserInteraction,
errorTextBuilder: (text) {
return Container(
decoration: BoxDecoration(
color: Theme.of(context)
.colorScheme
.error
.withOpacity(0.1),
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: Theme.of(context).colorScheme.error,
width: 1,
),
),
padding: const EdgeInsets.symmetric(
horizontal: 4,
),
child: Text(
text,
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
color: Theme.of(context).colorScheme.error,
),
),
);
},
errorTextAnchor: Alignment.centerRight,
textFieldAnchor: Alignment.centerRight,
errorTextOffset: const Offset(-8, 0),
)),
),
);
}
}