身体输出上的Flutter控件列表

siotufzp  于 2022-11-25  发布在  Flutter
关注(0)|答案(1)|浏览(146)

我正在尝试动态加载内容(小部件)(通过索引)。
但是,如果我不使用List,则一切都按预期工作:

class _MyHomePageState extends State<MyHomePage> {
  final titleController = TextEditingController();
  String titolo = '';
  late Widget display; //This is the future widget
  //List<Widget> display = <Widget>[];
  //int displayIndex = 1;

初始化它:

@override
  Widget build(BuildContext context) {
    //display.add(calculator());
    display = calculator();

并将其用于身体特性:
body: display,
尝试使用列表时:

class _MyHomePageState extends State<MyHomePage> {
  final titleController = TextEditingController();
  String titolo = '';
  //late Widget display;
  List<Widget> display = <Widget>[];
  //int displayIndex = 1;

初始化:

@override
  Widget build(BuildContext context) {
    display.add(calculator());
    //display = calculator();

并将其用于身体特性:

body: display.first,

我得到这个错误:

发生例外状况。_TypeError(类型'TabContainer'不是'函式结果'之类型'List'的子类型)

请注意TabContainer是计算器的第一个Widget():

Widget calculator() => TabContainer(
       selectedTextStyle: const TextStyle(
           fontFamily: 'ThousandSunny',

整个代码如下:

import 'package:cookedcalories/utils.dart';
import 'package:flutter/material.dart';
import 'package:convex_bottom_bar/convex_bottom_bar.dart';
import 'package:tab_container/tab_container.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: 'Cooked Calories & Macros',
      theme: ThemeData(
        primarySwatch: Colors.pink,
      ),
      home: const MyHomePage(title: 'Cooked Calories & Macros'),
    );
  }
}

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> {
  final titleController = TextEditingController();
  String titolo = '';
  //late Widget display;
  List<Widget> display = <Widget>[];
  //int displayIndex = 1;

  @override
  void initState() {
    titleController.addListener(() => setState(() {}));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    display.add(calculator());
    //display = calculator();
    return Scaffold(
      backgroundColor: Colors.yellow,
      bottomNavigationBar: ConvexAppBar(
        style: TabStyle.react,
        items: const [
          TabItem(icon: Icons.info_outline),
          TabItem(icon: Icons.receipt_outlined),
          TabItem(icon: Icons.calculate_outlined),
          TabItem(icon: Icons.monetization_on_outlined),
          TabItem(icon: Icons.settings_outlined),
        ],
        initialActiveIndex: 1,
        onTap: (int i) => print('click index=$i'),
      ),
      appBar: AppBar(
        title: Text(
          widget.title,
          style: const TextStyle(
            fontFamily: 'ThousandSunny',
            fontSize: 35,
          ),
        ),
      ),
      body: display.first,
    );
  }

  Widget calculator() => TabContainer(
        selectedTextStyle: const TextStyle(
            fontFamily: 'ThousandSunny',
            fontSize: 35,
            fontWeight: FontWeight.bold),
        unselectedTextStyle: const TextStyle(
          fontFamily: 'ThousandSunny',
          fontSize: 35,
        ),
        color: Colors.white,
        radius: 50,
        tabEdge: TabEdge.left,
        tabs: const [
          'A',
          'B',
          'C',
        ],
        children: [
          Align(
            alignment: Alignment.topCenter,
            child: SingleChildScrollView(
                padding: const EdgeInsets.all(10),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Row(
                      children: [
                        Expanded(
                          child: Padding(
                              padding: const EdgeInsets.fromLTRB(5, 30, 0, 0),
                              child: createTitleField()),
                        ),
                        const Padding(
                          padding: EdgeInsets.fromLTRB(10, 30, 5, 0),
                          child: Image(
                              width: 50,
                              height: 50,
                              image: AssetImage('assets/images/clean.png')),
                        ),
                      ],
                    ),
                  ],
                )),
          ),
          const Text('Child 2'),
          const Text('Child 3'),
        ],
      );

  Widget createTitleField() => TextFormField(
        style: const TextStyle(
          fontFamily: 'ThousandSunny',
          fontSize: 25,
        ),
        controller: titleController,
        validator: (value) {
          if (value == null || value.trim().isEmpty) {
            showSnackBar(
                context,
                "Attenzione: non hai inserito il Titolo dell'oggetto.",
                Colors.pinkAccent.shade400);
            return 'Inserisci il Titolo per questo oggetto';
          } else if (value.trim().length < 3) {
            showSnackBar(
                context,
                "Attenzione: Il Titolo deve contenere almeno 3 caratteri.",
                Colors.pinkAccent.shade400);
            return 'Lunghezza minima 3 caratteri';
          } else if (value.trim().length > 30) {
            showSnackBar(
                context,
                "Attenzione: Il Titolo non può essere più lungo di 30 caratteri.",
                Colors.pinkAccent.shade400);
            return 'Lunghezza massima 30 caratteri';
          }
          titolo = value;
          return null;
        },
        decoration: InputDecoration(
            border: const OutlineInputBorder(),
            hintText: 'Nome Ricetta',
            labelText: 'Nome Ricetta',
            labelStyle: const TextStyle(
              fontFamily: 'ThousandSunny',
              fontSize: 30,
            ),
            hintStyle: const TextStyle(
              fontFamily: 'ThousandSunny',
              fontSize: 25,
            ),
            suffixIcon: titleController.text.isEmpty
                ? Container(
                    width: 0,
                  )
                : IconButton(
                    onPressed: () => titleController.clear(),
                    icon: const Icon(Icons.close),
                  )),
        keyboardType: TextInputType.text,
        textInputAction: TextInputAction.next,
      );
}
aiqt4smr

aiqt4smr1#

尝试停止项目,运行flutter pub get,然后再次启动项目。

相关问题