Flutter: невозможно открыть класс CustomPainter - PullRequest
0 голосов
/ 04 марта 2020

У меня проблема с реализацией класса CustomPainter, я не знаю своей ошибки в каждом уроке, они делали то же самое, что и я, надеюсь, кто-то знает решение.

class _MyHomepageState extends State<MyHomepage> {
  var _name;

  final nameCon = new TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'App',
          style: TextStyle(fontWeight: FontWeight.w900),
        ),
        backgroundColor: Colors.brown[500],
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(10),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              TextField(
                controller: nameCon,
                decoration: InputDecoration(hintText: "Name"),
              ),
              RaisedButton(
                onPressed: () {
                  setState(() {


                    _name = nameCon.text;
                  });
                },
                child: Text("Los"),
              ),
              Text("hier $_name "),
             Expanded(
                child: LayoutBuilder(
                  builder: (_, constraints) => Container(
                    width: constraints.widthConstraints().maxWidth,
                    height: constraints.heightConstraints().maxHeight,
                    color: Colors.yellow,
                    child: CustomPaint(painter: Thermometer()),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class Thermometer extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {

final paint = Paint()
   ..color = Colors.brown
    ..style = PaintingStyle.stroke
    ..style=PaintingStyle.fill
    ..strokeWidth = 4;

    canvas.drawRRect(

    RRect.fromRectAndRadius(Rect.fromLTWH(-140, -60, 40, 290),
    Radius.circular(20)),
    paint,);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => false ;

  }

1 Ответ

0 голосов
/ 05 марта 2020

Вы можете скопировать полный код вставки ниже

, пожалуйста, измените left с -140 на 0 или другое значение

    canvas.drawRRect(
      RRect.fromRectAndRadius(
          Rect.fromLTWH(0, -60, 40, 290), Radius.circular(20)),
      paint,
    );

рабочая демонстрация

enter image description here

рабочая демоверсия

полный код

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomepage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomepage extends StatefulWidget {
  MyHomepage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomepageState extends State<MyHomepage> {
  var _name;

  final nameCon = new TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'App',
          style: TextStyle(fontWeight: FontWeight.w900),
        ),
        backgroundColor: Colors.brown[500],
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(10),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              TextField(
                controller: nameCon,
                decoration: InputDecoration(hintText: "Name"),
              ),
              RaisedButton(
                onPressed: () {
                  setState(() {
                    _name = nameCon.text;
                  });
                },
                child: Text("Los"),
              ),
              Text("hier $_name "),
              Expanded(
                child: LayoutBuilder(
                  builder: (_, constraints) => Container(
                    width: constraints.widthConstraints().maxWidth,
                    height: constraints.heightConstraints().maxHeight,
                    color: Colors.yellow,
                    child: CustomPaint(painter: Thermometer()),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class Thermometer extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = Colors.brown
      ..style = PaintingStyle.stroke
      ..style = PaintingStyle.fill
      ..strokeWidth = 4;

    canvas.drawRRect(
      RRect.fromRectAndRadius(
          Rect.fromLTWH(0, -60, 40, 290), Radius.circular(20)),
      paint,
    );

  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => false;
}
...