Flutter rowにTextFieldを含む複数のwidgetを並べる - ぶやかー

rowにTextFieldを含む複数のオブジェクトを並べたい

基本、TextFieldはLength指定してないと横に並べられないのでエラーになる。

幅はサイズ決めるのもあれなんでとりあえず、Flexible widgetに入れておけばいける。

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData.dark(),
      home: const MyHomePage(title: 'Flutter Rank Beginner'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Column(
        children: [
          Text('top'),
          Row(
            children: [
              Flexible(
                child: TextField(
                  keyboardType: TextInputType.multiline,
                  maxLines: 10,
                ),
              ),
              ElevatedButton(
                child: Text('Button'),
                onPressed: () async {},
              ),
            ],
          ),
          Text('bottom'),
        ],
      ),
    );
  }
}

この記事を書いた人 Wrote this article

kmatsunuma

TOP