-
-
Save DaisukeNagata/e563b634ee269a4702f694838d390125 to your computer and use it in GitHub Desktop.
Variable Horizontal Size Field...
This file contains 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 MaterialApp( | |
title: 'Flutter EveryDaySoft', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const MyHomePage( | |
title: "Example", | |
), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({super.key, required this.title}); | |
final String title; | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
Size size = Size.zero; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: SizedBox( | |
height: 50, | |
width: 100 < size.width ? size.width * 1.2 : 120, | |
child: TextFormField( | |
decoration: const InputDecoration(hintText: 'TextFormField'), | |
onChanged: (value) { | |
setState(() { | |
size = _sizeWith(value); | |
}); | |
}, | |
), | |
), | |
), | |
); | |
} | |
Size _sizeWith(String text) { | |
return (TextPainter( | |
text: TextSpan( | |
text: text, | |
), | |
maxLines: 1, | |
textScaleFactor: MediaQuery.of(context).textScaleFactor, | |
textDirection: TextDirection.ltr) | |
..layout()) | |
.size; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment