Skip to content

Instantly share code, notes, and snippets.

@andreacioni
Created July 9, 2022 11:34
Show Gist options
  • Save andreacioni/28beb93080463df4915cd4df752dffd9 to your computer and use it in GitHub Desktop.
Save andreacioni/28beb93080463df4915cd4df752dffd9 to your computer and use it in GitHub Desktop.
sliding_text_field_error
import 'dart:math';
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Persistence Example'),
),
body: const ColorBoxPage(
key: PageStorageKey<String>('pageTwo'),
),
);
}
}
class ColorBoxPage extends StatelessWidget {
const ColorBoxPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return ListView.builder(
itemExtent: 100.0,
itemBuilder: (BuildContext context, int index) {
final rnd = Random().nextInt(34532);
return TxtField('$rnd', key: ValueKey('$index'));
});
}
}
class TxtField extends StatefulWidget {
final String initialValue;
const TxtField(this.initialValue, {Key? key}) : super(key: key);
@override
State<TxtField> createState() => _MyWidgetState();
}
class _MyWidgetState extends State<TxtField> {
late final TextEditingController _controller;
@override
void initState() {
_controller = TextEditingController(text: widget.initialValue);
super.initState();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return TextField(
controller: _controller,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment