Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
633 views
in Technique[技术] by (71.8m points)

flutter 使用替换数组的方式渲染出了TextField 该怎么获取到其中的输入的值呢,特别是多个的时候

想要的效果就是点击提交后控制台能够打印出上面输入的值,因为是将字符串拆分替换的所以不知道怎么获取,该怎么改写呢?
image.png

import 'package:douban_flutter_app/textTest/bloc/bloc_input.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

void main() {
  runApp(BlocProvider<InputBloc>(
    create: (BuildContext ctx) => InputBloc(),
    child: MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final Test = "babaca";
    return MaterialApp(
      title: '文本拆分',
      home: Scaffold(
        appBar: AppBar(
          title: Text("文本拆分"),
        ),
        body: Container(
          child: Center(child: Column(
            children: [
              Spans(Test),
              RaisedButton(
                child: Text("提交"),
                onPressed: (){
                    print("inbloc");
                },
              ),
            ],
          )),
        ),
      ),
    );
  }
}


class Spans extends StatefulWidget {
  final aa;
  Spans(this.aa);
  @override
  _SpansState createState() => _SpansState();
}

class _SpansState extends State<Spans> {
  @override
  Widget build(BuildContext context) {
    List<Widget> spans = [];
    for (var item in widget.aa.runes.map((rune) {
      return new String.fromCharCode(rune);
    }).toList()) {
      if (item == 'a') {
        spans.add(Container(
          width: 60,
          child: textField(),
        ));
      } else {
        spans.add(Text(item));
      }
    }
    return Wrap(
      children: spans,  //返回数组Widget
    );
  }
}

class textField extends StatefulWidget {
  @override
  _textFieldState createState() => _textFieldState();
}

class _textFieldState extends State<textField> {
  @override
  Widget build(BuildContext context) {
    return TextField(
      decoration:
          InputDecoration(isDense: true, contentPadding: EdgeInsets.zero),
      onChanged: (value) {
        setState(() {
        });
      },
    );
  }
}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

为它们分配 TextEditingController.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...