flutter 模态中的TextField

wnavrhmk  于 2023-04-07  发布在  Flutter
关注(0)|答案(3)|浏览(159)

如果我们使用列表视图,当键盘出现时,文本字段会上升,但在模态中不会。对不起,我不知道如何正确解释它,我只会用图像向你展示。
模态码

showModalBottomSheet(
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.vertical(
        top: Radius.circular(25.0),
      ),
    ),
    backgroundColor: Colors.white,
    context: context,
    builder: (context) => Wrap(
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [

            // ... another widget
            // ...
            // ...
            
            // Write review text area
            Container(
              margin: EdgeInsets.fromLTRB(24, 8, 24, 0),
              padding: EdgeInsets.fromLTRB(16, 4, 16, 4),
              width: double.infinity,
              height: 100,
              decoration: BoxDecoration(
                color: Color(0xffF7F7F7),
                borderRadius: BorderRadius.circular(20),
              ),
              child: TextField(
                expands: true,
                maxLines: null,
                decoration: InputDecoration(
                  hintText: languageCode == 'en'
                      ? 'How is your overall experience?'
                      : languageCode == 'id'
                          ? 'Bagaimana pengalaman Anda secara keseluruhan?'
                          : '',
                  hintStyle: TextStyle(
                    color: Color(0xffB2B2B2),
                  ),
                  border: InputBorder.none,
                ),
              ),
            ),
          ],
        ),
      ],
    ),
  );

无键盘

当键盘出现时

b5lpy0ml

b5lpy0ml1#

尝试在showModalBottomSheet内添加**isScrollControlled: true,**
您的showModalBottomSheet方法:

bottomSheet(BuildContext context) {
    showModalBottomSheet(
      isScrollControlled: true,
      context: context,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
            topLeft: Radius.circular(30.0), topRight: Radius.circular(30.0)),
      ),
      builder: (BuildContext context) {
        return Padding(
          padding: MediaQuery.of(context).viewInsets,
          child: Container(
            padding: EdgeInsets.all(16.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                SizedBox(
                  height: 20,
                ),
                Container(
                  margin: const EdgeInsets.all(8.0),
                  height: 100,
                  color: Colors.red,
                ),
                TextFormField(
                  keyboardType: TextInputType.phone,
                  validator: (value) {
                    if (value.isEmpty) {
                      return 'Please Enter Mobile Number';
                    }
                    return null;
                  },
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    hintText: 'Enter  Number',
                    labelText: ' Number',
                  ),
                ),
                Container(
                  margin: const EdgeInsets.all(8.0),
                  height: 100,
                  color: Colors.red,
                ),
                Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: ElevatedButton(
                    onPressed: () async {},
                    child: Text(
                      'Submit',
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

您的Widget:

TextButton(
        onPressed: () {
          bottomSheet(context);
        },
        child: Text('data'),
      ),

结果屏幕:

7lrncoxx

7lrncoxx2#

这是我的试用示例代码。
尝试使用“SingleChildScrollView”换行以避免垂直溢出,
并添加底部填充作为键盘高度的“viewInsets.bottom”值。

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: _buildBody(),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showModalBottomSheet(
            isScrollControlled: true,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.vertical(
                top: Radius.circular(25.0),
              ),
            ),
            backgroundColor: Colors.white,
            context: context,
            builder: (context) => SingleChildScrollView(
              child: Container(
                padding: EdgeInsets.only(
                    bottom: MediaQuery.of(context).viewInsets.bottom),
                child: Wrap(
                  children: [
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        // ... another widget
                        // ...
                        // ...

                        // Write review text area
                        Container(
                          margin: EdgeInsets.fromLTRB(24, 8, 24, 0),
                          padding: EdgeInsets.fromLTRB(16, 4, 16, 4),
                          width: double.infinity,
                          height: 100,
                          decoration: BoxDecoration(
                            color: Color(0xffF7F7F7),
                            borderRadius: BorderRadius.circular(20),
                          ),
                          child: TextField(
                            expands: true,
                            maxLines: null,
                            decoration: InputDecoration(
                              hintText:
                                  'Bagaimana pengalaman Anda secara keseluruhan?',
                              hintStyle: TextStyle(
                                color: Color(0xffB2B2B2),
                              ),
                              border: InputBorder.none,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          );
        },
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }

  Widget _buildBody() {
    return Container();
  }
}
velaa5lx

velaa5lx3#

尝试将isScrollControlled = true添加到BottomSheetDialog
添加填充,否则工作表将覆盖整个屏幕。

相关问题