Skip to content

Instantly share code, notes, and snippets.

@DaisukeNagata
Last active December 6, 2022 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaisukeNagata/e563b634ee269a4702f694838d390125 to your computer and use it in GitHub Desktop.
Save DaisukeNagata/e563b634ee269a4702f694838d390125 to your computer and use it in GitHub Desktop.
Variable Horizontal Size Field...
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;
}
}
@DaisukeNagata
Copy link
Author

2022-11-30.22.57.22.mov

@DaisukeNagata
Copy link
Author

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;
  }
}

@DaisukeNagata
Copy link
Author

default.mov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment