Home > Back-end >  How to refer to add a Widget element to a list in flutter?
How to refer to add a Widget element to a list in flutter?

Time:11-30

not sure if it's possible to have a widget added as an element in a list. Can someone take a look at what might be wrong? I have no clue how I can refer to this sample widget. Code

I've tried to add this widget to a class and tried to take apart the widget but without any result.

CodePudding user response:

I tried to replicate your problem. It seems it works. Try this:

import 'package:flutter/material.dart';

const Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(FirstWidget(title: 'Test', widget: MyWidget()));
}

class FirstWidget extends StatelessWidget {
  final String title;
  final Widget widget;
  
  FirstWidget({required this.title, required this.widget});
  
  final List<FirstWidget> contents = [FirstWidget(title: 'Test', widget: MyWidget())];
  
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: darkBlue,
      ),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

  • Related