4

I'm trying to make simple example that has a clickable button in center of screen in Flutter. I can do that using MaterialApp widget but I want to do it without using MaterialApp.

This example works with Text widget

import 'package:flutter/material.dart';

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

But all my trials to change the Text widget with FlatButton or RaisedButton failed because they depend on Material Widget

I also tried that:

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: () {
            },
          ),
          )
      )
  );
}

Question is: Is there any way to have Button that does not depend on Material Widget?

MSaudi
  • 4,442
  • 2
  • 40
  • 65
  • Like that ? import 'package:flutter/material.dart'; void main() { runApp( Card( child: Center( child: FlatButton( child: Text( 'Hello, world!', textDirection: TextDirection.ltr, ), ), ) ) ); } not working – MSaudi Feb 06 '19 at 07:59

1 Answers1

4

You have to use - WidgetsApp then.

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,
    );
  }
}

As per Doc -

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

enter image description here

Update - Without Other Class:

If you use home: then you need to pass - onGenerateRoute or pageRouteBuilder

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,
  ));
}
anmol.majhail
  • 48,256
  • 14
  • 136
  • 105
  • Can we do it without separate class ? I mean I tried the following but I get an exception: void main() { runApp( WidgetsApp( home: Center( child: RaisedButton( onPressed: () {}, child: Text( 'Hello, world!', textDirection: TextDirection.ltr, ), ), ) ) ); } – MSaudi Feb 06 '19 at 08:28
  • Thank you it works. But I don't understand why we need this part " builder: (context, int) { return " Because in the case of the Text, we did not need that. – MSaudi Feb 06 '19 at 08:35
  • because - This Widget has condition - `'builder != null || onGenerateRoute != null || pageRouteBuilder != null':` so you have define any one of them to work. – anmol.majhail Feb 06 '19 at 08:38
  • i used `builder` simply – anmol.majhail Feb 06 '19 at 08:38