0

I would like to make an extra file 'extra.dart' where I would like to add my top container and the four TextButtonWidgets. 1 ColorWidget, 4 TextButtonWidgets with each one having colorwidget as their child.

I would name the top container widget ColorWidget, and reuse that widget in the text button widgets as their child.

!! I need to set color as a required named parameter in ColorWidget(), but there it always goes wrong for me.

So in extra.dart should be all widgets, and in main.dart just the basic layout where I call all widgets from extra.dart.

Don't worry about the switchlisttiles. If you'd like to add them, they mean create border, and make container bigger in size, all applied to top container.

import 'package:flutter/material.dart';
import 'package:flutter/gestures.dart';
import 'extra.dart';

// ONLY USE STATEFUL WIDGETS IF CONTENT ON SCREEN HAS TO CHANGE DYNAMICALLY!!
void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  final String title = 'Test';
  final String name = 'John Doe';

  @override
  //
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Home(),
      ),
    );
  }
}

// var colorChange = true;

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

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

class _HomeState extends State<Home> {
  Color color = Colors.amber;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Container color")),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Center(
            child: Container(
              decoration: BoxDecoration(
                  color: color,
                  borderRadius: const BorderRadius.all(Radius.circular(10))),
              height: 100,
              width: 100,
            ),
          ),
          const SizedBox(
            height: 20,
          ),
          Column(
            children: [
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    Center(
                      child: TextButton(
                        onPressed: () {
                          setState(() {
                            color = Colors.amber;
                          });
                        },
                        child: Container(
                          decoration: const BoxDecoration(
                              color: Colors.amber,
                              borderRadius:
                                  BorderRadius.all(Radius.circular(10))),
                          height: 100,
                          width: 100,
                        ),
                      ),
                    ),
                    Center(
                      child: TextButton(
                        onPressed: () {
                          setState(() {
                            color = Colors.blue;
                          });
                        },
                        child: Container(
                          decoration: const BoxDecoration(
                              color: Colors.blue,
                              borderRadius:
                                  BorderRadius.all(Radius.circular(10))),
                          height: 100,
                          width: 100,
                        ),
                      ),
                    )
                  ],
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    Center(
                      child: TextButton(
                        onPressed: () {
                          setState(() {
                            color = Colors.red;
                          });
                        },
                        child: Container(
                          decoration: const BoxDecoration(
                              color: Colors.red,
                              borderRadius:
                                  BorderRadius.all(Radius.circular(10))),
                          height: 100,
                          width: 100,
                        ),
                      ),
                    ),
                    Center(
                      child: TextButton(
                        onPressed: () {
                          setState(() {
                            color = Colors.green;
                          });
                        },
                        child: Container(
                          decoration: const BoxDecoration(
                              color: Colors.green,
                              borderRadius:
                                  BorderRadius.all(Radius.circular(10))),
                          height: 100,
                          width: 100,
                        ),
                      ),
                    )
                  ],
                ),
              )
            ],
          )
        ],
      ),
    );
  }
}

Screenshot:

giannik28
  • 403
  • 2
  • 6
  • 14
  • Are you trying to separate the body on a new widget? – Md. Yeasin Sheikh Jan 13 '22 at 11:36
  • Hey, I think so yeah. I just try to make all components of the body into separate classes on another extra.dart file, and then assign them to the body on main.dart. 1. also when I reuse the ColorWidget() (top container) as a child of the TextButtons(), when I tap the switch for resizing, those buttons also resize and idk how to prevent that. And 2. I need to get the textbuttons as a whole to become separate classes also. – giannik28 Jan 13 '22 at 12:53
  • I have the problem once I tap the switches that the bottom containers also increase in size and gain a border, because their child is the colorwidget (upper box) – giannik28 Jan 13 '22 at 15:25

0 Answers0