T
T
trickster20192022-04-08 16:36:12
Dart
trickster2019, 2022-04-08 16:36:12

How to create a widget in a separate file?

Good afternoon. I want to put the button widget in a separate file, but got the error Error: A value of type 'Type' can't be assigned to a variable of type 'Widget'.

Main page code.

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_/style.dart';
import 'package:flutter_/widjets/button.dart';

class home_page_schedule extends StatefulWidget {
  @override
  State<home_page_schedule> createState() => _home_page_scheduleState();
}

class _home_page_scheduleState extends State<home_page_schedule> {


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: s.backgroundColor,
        appBar: AppBar(
          centerTitle: true,
          backgroundColor: s.appBarColor,
          title:
              Text("Главная страница", style: TextStyle(color: Colors.white)),
        ),
        body: Center(
            child: Container(
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image: AssetImage(
                          'lib/assets/images/bb.jpg',
                        ),
                        fit: BoxFit.fill)),
                child: SizedBox(
                  width: 500,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      but_wid buttons =but_wid(text: "Задача к определённой дате", linc:"/task_by_date"),
                    ],
                  ),
                ))));
  }
}


button file code.
import 'package:flutter/material.dart';
import '../style.dart';

class but_wid extends StatelessWidget {
  String linc;
  String text;

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      style: ElevatedButton.styleFrom(
          primary: s.buttonColor, minimumSize: Size(300, 100)),
      onPressed: () {
        Navigator.pushNamed(context, linc);
      },
      child: Text("${text}", style: TextStyle(color: Colors.white)),
    );
  }

  but_wid({
    required this.text,
    required this.linc,
  });
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Neonoviiwolf, 2022-04-08
@trickster2019

most likely
6250420326b35304242325.png
pliz, read how to name classes correctly, it’s impossible

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question