Created
July 13, 2023 11:29
-
-
Save rocboronat/b955cb03b3881a1f2be387d7d9e554c0 to your computer and use it in GitHub Desktop.
Keep the text inputs values when changing the layout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return const MaterialApp(title: 'Playground', home: MyHomePage()); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
const MyHomePage({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
final TextEditingController controller = TextEditingController(); | |
return Scaffold( | |
appBar: AppBar(title: const Text("Playground")), | |
body: ResponsiveBuilder( | |
small: Column( | |
children: [ | |
const Text("Small"), | |
Center( | |
child: SizedBox( | |
width: 200, | |
child: TextField( | |
controller: controller, | |
decoration: const InputDecoration( | |
hintText: 'Small screen hint', | |
), | |
), | |
), | |
), | |
], | |
), | |
large: Column( | |
children: [ | |
const Text("Large"), | |
Container( | |
constraints: const BoxConstraints(maxWidth: 400), | |
child: TextField( | |
controller: controller, | |
decoration: const InputDecoration( | |
hintText: 'Large screen hint', | |
), | |
), | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
class ResponsiveBuilder extends StatelessWidget { | |
const ResponsiveBuilder({ | |
Key? key, | |
required this.small, | |
required this.large, | |
}) : super(key: key); | |
final Widget small; | |
final Widget large; | |
@override | |
Widget build(BuildContext context) { | |
if (MediaQuery.of(context).size.width > 650) return large; | |
return small; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment