Задать вопрос
@trickster2019

Как создать виджет в отдельном файле?

Добрый день. Хочу поместить виджет кнопки в отдельный файл, но получил ошибку Error: A value of type 'Type' can't be assigned to a variable of type 'Widget'.

Код основной страницы.
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"),
                    ],
                  ),
                ))));
  }
}


код файла с кнопкой.
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,
  });
}
  • Вопрос задан
  • 184 просмотра
Подписаться 1 Простой Комментировать
Решения вопроса 1
@Neonoviiwolf
Flutter developer
скорее всего
6250420326b35304242325.png

плиз, почитайте как правильно именовать классы, так нельзя
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы
Hermon Group Калининград
от 100 000 ₽
DevTeam.Space Москва
от 1 500 до 2 500 $
Crystal Service Integration Санкт-Петербург
от 200 000 ₽