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; | |
} | |
} |
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;
Size size2 = Size.zero;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
_textField(true),
SizedBox(
width: 24,
),
_textField(false),
],
),
),
),
);
}
Widget _textField(bool flg) {
var length = flg ? size : size2;
return Container(
height: 50,
width: 60 < length.width ? length.width * 1.2 : 70,
child: TextFormField(
scrollPhysics: NeverScrollableScrollPhysics(),
decoration: const InputDecoration(hintText: 'TextFormField'),
onChanged: (value) {
setState(() {
if (flg) {
size = _sizeWith(value);
} else {
size2 = _sizeWith(value);
}
});
},
),
);
}
Size _sizeWith(String text) {
return (TextPainter(
text: TextSpan(
text: text,
),
maxLines: 1,
textScaleFactor: MediaQuery.of(context).textScaleFactor,
textDirection: TextDirection.ltr)
..layout())
.size;
}
}
default.mov
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2022-11-30.22.57.22.mov