在不使用 MaterialApp Flutter 的情况下在屏幕上添加按钮

Add button on the screen without using MaterialApp Flutter

我正在尝试做一个简单的例子,在 Flutter 的屏幕中央有一个可点击的按钮。我可以使用 MaterialApp 小部件来做到这一点,但我想在不使用 MaterialApp.

的情况下做到这一点

此示例适用于文本小部件

import 'package:flutter/material.dart';

void main() {
  runApp(
      Center(
      child: Text(
        'Hello, world!',
        textDirection: TextDirection.ltr,
      ),
    ),
  ) ;
}

但是我尝试使用 FlatButton 或 RaisedButton 更改文本小部件的所有尝试都失败了,因为它们依赖于 Material 小部件

我也试过了:

import 'package:flutter/material.dart';

void main() {
  runApp(
      Container(
          decoration: BoxDecoration(color: Colors.white),
          child:Center(
      child: RaisedButton(
            child: const Text('Press meh', textDirection: TextDirection.ltr),
            onPressed: () {
            },
          ),
          )
      )
  );
}

问题是:有什么方法可以让Button不依赖于Material Widget吗?

你必须使用 - WidgetsApp 然后。

void main() {
  runApp(SomeText());
}

class SomeText extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return WidgetsApp(
      builder: (context, int) {
        return Center(
          child: RaisedButton(
            onPressed: () {},
            child: Text(
              'Hello, world!',
              textDirection: TextDirection.ltr,
            ),
          ),
        );
      },
      color: Colors.blue,
    );
  }
}

根据文档 -

WidgetsApp, which defines the basic app elements but does not depend on the material library.

更新-无其他Class:

如果你使用 home: 那么你需要传递 - onGenerateRoutepageRouteBuilder

If neither builder nor onGenerateRoute are provided, the pageRouteBuilder must be specified so that the default handler will know what kind of PageRoute transition to build.

void main() {
  runApp(WidgetsApp(
    builder: (context, int) {
      return Center(
        child: RaisedButton(
          onPressed: () {},
          child: Text(
            'Hello, world!',
            textDirection: TextDirection.ltr,
          ),
        ),
      );
    },
    color: Colors.blue,
  ));
}