如何在GridView.builder中设置textform字段并在flutter中将12个textform字段作为一个字符串?

yrdbyhpb  于 2023-01-31  发布在  Flutter
关注(0)|答案(1)|浏览(105)

所以,上面的图片是在网格视图中显示12个文本。但我想textformfield而不是文本(a,b,c,d,..),如果用户点击下一步所有的12个文本应该在一个字符串。我怎么做呢?

pu3pd22g

pu3pd22g1#

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:world_virtual_coin/services/app_utils.dart';
import 'package:world_virtual_coin/utils/en_extensions.dart';
import 'package:world_virtual_coin/widget/back_button.dart';
import 'package:world_virtual_coin/widget/colors.dart';
import 'package:world_virtual_coin/widget/custom_container.dart';
import '../../services/theme.dart';
import '../../widget/custom_button.dart';

class TwelvePhrase extends StatefulWidget {
  const TwelvePhrase({Key? key}) : super(key: key);

  @override
  State<TwelvePhrase> createState() => _TwelvePhraseState();
}

class _TwelvePhraseState extends State<TwelvePhrase> {
  List<TwelvePhraseModel> phraseList = [
    TwelvePhraseModel(""),
    TwelvePhraseModel(""),
    TwelvePhraseModel(""),
    TwelvePhraseModel(""),
    TwelvePhraseModel(""),
    TwelvePhraseModel(""),
    TwelvePhraseModel(""),
    TwelvePhraseModel(""),
    TwelvePhraseModel(""),
    TwelvePhraseModel(""),
    TwelvePhraseModel(""),
    TwelvePhraseModel(""),
  ];

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        child: Scaffold(
      backgroundColor: Colors.white,
      body: CustomContainer(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const CustomBackButton(title: 'Twelve phrase'),
            35.heightBox,
            GridView.builder(
              primary: false,
              shrinkWrap: true,
              // padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
              padding: EdgeInsets.symmetric(horizontal: 0, vertical: 12.h),
              gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 3,
                  mainAxisSpacing: 8,
                  childAspectRatio: 6 / 2,
                  crossAxisSpacing: 8),
              itemCount: phraseList.length,
              itemBuilder: (context, i) {
                return Row(
                  // mainAxisSize: MainAxisSize.max,
                  children: [
                    Text(
                      "${i + 1}",
                      style: TextStyle(
                          fontSize: 11.sp,
                          fontFamily: fontRegular,
                          color: Colors.black),
                    ),
                    8.widthBox,
                    Expanded(
                      child: Center(
                        child: TextField(
                          inputFormatters: [
                            FilteringTextInputFormatter.deny(RegExp(r'\s')),
                          ],
                          style: const TextStyle(
                              fontSize: 15.0, color: Colors.black),
                          decoration: InputDecoration(
                            contentPadding: const EdgeInsets.symmetric(
                                horizontal: 7, vertical: 7),
                            enabledBorder: OutlineInputBorder(
                              borderSide: const BorderSide(
                                  width: 3, color: colorPrimary),
                              //<-- SEE HERE
                              borderRadius: BorderRadius.circular(30.0),
                            ),
                          ),
                          onChanged: (value) {
                            setState(() {
                              phraseList[i].strKey = value.toString();
                              phraseList[i].checker = i + 1;
                            });
                          },
                        ),
                      ),
                    ),
                  ],
                );
              },
            ),
            20.heightBox,
            CustomButton(
              onPressed: () {
                String result = "";
                // Get.offNamed('/otpVerification');
                for (int i = 0; i < phraseList.length; i++) {
                  TwelvePhraseModel model = phraseList[i];
                  result = result + model.strKey.toString();
                }
                if (result.isNotEmpty) {
                  AppUtils.showSuccessSnackBar(result);
                } else {
                  AppUtils.showErrorSnackBar("Please Fill all fields");
                }

              },
              label: 'Next',
            ),
          ],
        ),
      ),
    ));
  }
}

class TwelvePhraseModel {
  String strKey;
  int checker = 1;

  TwelvePhraseModel(this.strKey);
}

相关问题