Home > Blockchain >  im using dart 2.16.2 Raisedbutton onpressed Change page no affect
im using dart 2.16.2 Raisedbutton onpressed Change page no affect

Time:05-18

i want to changed my page when i press my RaisedButton.

here's my code :

Padding( padding: const EdgeInsets.fromLTRB(56, 0, 56, 24),          

child: RaisedGradientButton(
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => UserDetailPage()),
                );
                // ignore: avoid_print
                print("Button Click");
              },
              child: Text(
                'Got It',
                style: TextStyle(
                    color: Colors.white,
                    fontFamily: 'Nunito',
                    fontWeight: FontWeight.bold,
                    fontSize: 20.0),
              ),
              gradient: LinearGradient(
                colors: const <Color>[Colors.purple, Colors.blue],
              ),
)

)

My model not change to page, Please help me.

Preview error no Affect with click

CodePudding user response:

Modify the following code in your RaisedGradientButton.dart

InkWell(
            onTap: () => onPressed,
            child: Center(
              child: child,
            )),

With:

  InkWell(
            onTap: onPressed,
            child: Center(
              child: child,
            )),

Be aware that if you have functions in your widget's parameters you should always use "() =>" either where you built your widget or where you use your widget only once.

CodePudding user response:

here my full code

import 'package:flutter/material.dart';
import 'package:bodymeasure/user_detail_page.dart';
import 'package:bodymeasure/RaisedButtonGradient.dart';

class InstructionPage extends StatefulWidget {
  static String tag = 'instruction-page';

  const InstructionPage({Key? key}) : super(key: key);
  @override
  _InstructionPageState createState() => _InstructionPageState();
}

class _InstructionPageState extends State<InstructionPage>
    with SingleTickerProviderStateMixin {
  late TabController controller;

  @override
  void initState() {
    super.initState();
    controller = TabController(vsync: this, length: 2);
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('lib/assets/splash_bg.png'),
            fit: BoxFit.cover,
          ),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.fromLTRB(24, 56, 24, 24),
              child: Column(
                children: const <Widget>[
                  Text(
                    "Bagaimana cara mengambil foto?\n",
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 20.0,
                        fontFamily: 'Nunito'),
                  ),
                  Text(
                    "Harap baca instruksi dengan benar sebelum melanjutkan dan bersiaplah",
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 15.0,
                        fontFamily: 'Nunito'),
                    textAlign: TextAlign.center,
                  ),
                ],
              ),
            ),
            Expanded(
              child: Padding(
                padding: const EdgeInsets.fromLTRB(24.0, 8.0, 24.0, 8.0),
                child: Column(
                  children: <Widget>[
                    TabBar(
                      controller: controller,
                      tabs: const <Widget>[
                        Tab(text: "Front"),
                        Tab(
                          text: "Side",
                        ),
                      ],
                    ),
                    Expanded(
                      child: Padding(
                        padding:
                            const EdgeInsets.fromLTRB(56.0, 8.0, 56.0, 8.0),
                        child: TabBarView(
                          controller: controller,
                          children: <Widget>[
                            DecoratedBox(
                              decoration: BoxDecoration(
                                image: DecorationImage(
                                    image: AssetImage(
                                        'lib/assets/front_instruction.jpg'),
                                    fit: BoxFit.fill),
                                borderRadius: BorderRadius.circular(10.0),
                              ),
                            ),
                            DecoratedBox(
                              decoration: BoxDecoration(
                                image: DecorationImage(
                                    image: AssetImage(
                                        'lib/assets/side_instruction.jpg'),
                                    fit: BoxFit.fill),
                                borderRadius: BorderRadius.circular(10.0),
                              ),
                            ),
                          ],
                        ),
                      ),
                    )
                  ],
                ),
              ),
            ),
            Padding(
                padding: const EdgeInsets.fromLTRB(56, 0, 56, 24),
                child: RaisedGradientButton(
                  onPressed: () {
                    Navigator.of(context).push(MaterialPageRoute(
                        builder: (context) => UserDetailPage()));
                    // ignore: avoid_print
                    print("Button Click");
                  },
                  child: Text(
                    'Got It',
                    style: TextStyle(
                        color: Colors.white,
                        fontFamily: 'Nunito',
                        fontWeight: FontWeight.bold,
                        fontSize: 20.0),
                  ),
                  gradient: LinearGradient(
                    colors: const <Color>[Colors.purple, Colors.blue],
                  ),
                )),
          ],
        ),
      ),
    );
  }
}

CodePudding user response:

I use custom gradient. but when click this button no affect with button.

import 'package:flutter/material.dart';

class RaisedGradientButton extends StatelessWidget {
  final Widget child;
  final Gradient gradient;
  final double width;
  final double height;
  final Function onPressed;

  const RaisedGradientButton({
    Key? key,
    required this.child,
    required this.gradient,
    this.width = double.infinity,
    this.height = 50.0,
    required this.onPressed,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width,
      height: 50.0,
      decoration: BoxDecoration(
          gradient: gradient,
          boxShadow: const [
            BoxShadow(
              color: Colors.grey,
              offset: Offset(0.0, 1.5),
              blurRadius: 1.5,
            ),
          ],
          borderRadius: BorderRadius.circular(50.0)),
      child: Material(
        color: Colors.transparent,
        child: InkWell(
            onTap: () => onPressed,
            child: Center(
              child: child,
            )),
      ),
    );
  }
}
  • Related